The Portkey x Swarm integration brings advanced AI gateway capabilities, full-stack observability, and reliability features to build production-ready AI agents.
Swarm is an experimental framework by OpenAI for building multi-agent systems. It showcases the handoff & routines pattern, making agent coordination and execution lightweight, highly controllable, and easily testable. Portkey integration extends Swarm’s capabilities with production-ready features like observability, reliability, and more.
To build Swarm Agents with Portkey, you’ll need two keys:
Portkey API Key: Sign up on the Portkey app and copy your API key.
Virtual Key: Virtual Keys are a secure way to manage your LLM API KEYS in one place. Instead of handling multiple API keys in your code, you can store your LLM provider API Keys securely in Portkey’s vault
Create a Virtual Key in the Portkey app
In this example we are building a simple Weather Agent using OpenAI Swarm with Portkey.
Here’s a complete example showing function calling and agent interaction:
The current temperature in New York City is 67°F.
By routing your OpenAI Swarm requests through Portkey, you get access to the following production-grade features:
Call various LLMs like Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, and AWS Bedrock with minimal code changes.
Speed up agent responses and save costs by storing past responses in the Portkey cache. Choose between Simple and Semantic cache modes.
Set up fallbacks between different LLMs, load balance requests across multiple instances, set automatic retries, and request timeouts.
Get comprehensive logs of agent interactions, including cost, tokens used, response time, and function calls. Send custom metadata for better analytics.
Access detailed logs of agent executions, function calls, and interactions. Debug and optimize your agents effectively.
Implement budget limits, role-based access control, and audit trails for your agent operations.
Capture and analyze user feedback to improve agent performance over time.
When building with Swarm, you might want to experiment with different LLMs or use specific providers for different agent tasks. Portkey makes this seamless - you can switch between OpenAI, Anthropic, Gemini, Mistral, or cloud providers without changing your agent code.
Instead of managing multiple API keys and provider-specific configurations, Portkey’s Virtual Keys give you a single point of control. Here’s how you can use different LLMs with your Swarm agents:
Agent operations often involve repetitive queries or similar tasks. Every time your agent makes an LLM call, you’re paying for tokens and waiting for responses. Portkey’s caching system can significantly reduce both costs and latency.
Portkey offers two powerful caching modes:
Simple Cache: Perfect for exact matches - when your agents make identical requests. Ideal for deterministic operations like function calling or FAQ-type queries.
Semantic Cache: Uses embedding-based matching to identify similar queries. Great for natural language interactions where users might ask the same thing in different ways.
When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey’s reliability features ensure your agents keep running smoothly even when problems occur.
Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips.
Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes.
Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs.
Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability.
Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits.
Building agents is the first step - but how do you know they’re working effectively? Portkey provides comprehensive visibility into your agent operations through multiple lenses:
Metrics Dashboard: Track 40+ key performance indicators like:
Add trace IDs to track specific workflows:
Logs are essential for understanding agent behavior, diagnosing issues, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes.
Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata.
When deploying agents in production, security is crucial. Portkey provides enterprise-grade security features:
Set and monitor spending limits per Virtual Key. Get alerts before costs exceed thresholds.
Control who can access what. Assign roles and permissions for your team members.
Track all changes and access. Know who modified agent settings and when.
Configure data retention and processing policies to meet your compliance needs.
Configure these settings in the Portkey Dashboard or programmatically through the API.
Now that you know how to trace & log your Llamaindex requests to Portkey, you can also start capturing user feedback to improve your app!
You can append qualitative as well as quantitative feedback to any trace ID
with the portkey.feedback.create
method:
Many of these features are driven by Portkey’s Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs.
For more information on using these features and setting up your Config, please refer to the Portkey documentation.