How to build AI agent with Composio tools
Quick answer
Use the
composio_openai package to access Composio tools and integrate them with the openai Python SDK. Instantiate ComposioToolSet with your API key, retrieve tools, and pass them in the tools parameter when calling client.chat.completions.create to build an AI agent that can invoke these tools.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)Composio API keypip install openai>=1.0 composio-core composio-openai
Setup
Install the required Python packages and set environment variables for your OpenAI and Composio API keys.
- Use
pip install openai composio-core composio-openaito install dependencies. - Set
OPENAI_API_KEYandCOMPOSIO_API_KEYin your environment.
pip install openai composio-core composio-openai output
Collecting openai Collecting composio-core Collecting composio-openai Successfully installed openai composio-core composio-openai
Step by step
This example shows how to create a Composio toolset, retrieve tools, and use them in an OpenAI chat completion to build an AI agent that can call Composio tools.
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
# Initialize Composio toolset with your Composio API key
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
# Get tools for specific actions (e.g., GitHub star repository)
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
# Initialize OpenAI client with your OpenAI API key
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Compose messages for the chat completion
messages = [{"role": "user", "content": "Star the openai/openai-python repo on GitHub"}]
# Call chat completion with Composio tools enabled
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=messages
)
print("AI response:", response.choices[0].message.content)
# Handle tool calls if any
if response.choices[0].finish_reason == "tool_calls":
result = toolset.handle_tool_calls(response)
print("Tool call results:", result) output
AI response: I have starred the openai/openai-python repository on GitHub for you.
Tool call results: {'success': True, 'details': 'Repository starred successfully.'} Common variations
You can use different models such as gpt-4o or gpt-4o-mini depending on your latency and cost requirements. The Composio toolset supports multiple actions; customize actions in get_tools to add more capabilities.
For asynchronous usage, wrap calls in async functions and use an async OpenAI client if available.
import asyncio
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
async def main():
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [{"role": "user", "content": "Star the openai/openai-python repo on GitHub"}]
response = await client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=messages
)
print("AI response:", response.choices[0].message.content)
if response.choices[0].finish_reason == "tool_calls":
result = toolset.handle_tool_calls(response)
print("Tool call results:", result)
asyncio.run(main()) output
AI response: I have starred the openai/openai-python repository on GitHub for you.
Tool call results: {'success': True, 'details': 'Repository starred successfully.'} Troubleshooting
- If you get authentication errors, verify that
COMPOSIO_API_KEYandOPENAI_API_KEYare correctly set in your environment. - If tools are not recognized, ensure you are using the latest
composio-coreandcomposio-openaipackages. - For unexpected API errors, check network connectivity and API usage limits.
Key Takeaways
- Use
ComposioToolSetto access and manage Composio tools for AI agents. - Pass tools via the
toolsparameter inclient.chat.completions.createto enable tool usage. - Handle tool call results with
toolset.handle_tool_calls()after the chat completion. - Customize tool actions to extend your AI agent's capabilities.
- Always set API keys via environment variables for secure authentication.