Portkey
Use Portkey AI Gateway for multi-provider routing with Agno agents.
Portkey is an AI Gateway that provides a unified interface to access multiple AI providers with advanced features like routing, load balancing, retries, and observability. Use Portkey to build production-ready AI applications with better reliability and cost optimization.
With Portkey, you can:
- Route requests across multiple AI providers
- Implement fallback mechanisms for better reliability
- Monitor and analyze your AI usage
- Cache responses for cost optimization
- Apply rate limiting and usage controls
Authentication
Get a Portkey API key from your workspace.
Create a provider integration in your Portkey Model Catalog, then copy an allowed model string in the form @provider_slug/model_name. Replace @your-provider-slug/gpt-5-nano in the example with that value. The slug is specific to your workspace. These catalog examples need PORTKEY_API_KEY; they do not require a separate virtual key. Pass the key explicitly as portkey_api_key, because the Agno adapter does not read that environment variable automatically.
Legacy virtual-key routing is also supported: configure virtual_key (or PORTKEY_VIRTUAL_KEY) when using that route. It is an alternative to the catalog model slug.
export PORTKEY_API_KEY=***Example
Install the portkey-ai and openai packages:
uv pip install -U agno portkey-ai openaiSave the following as agent.py and run python agent.py:
import os
from agno.agent import Agent
from agno.models.portkey import Portkey
agent = Agent(
model=Portkey(
id="@your-provider-slug/gpt-5-nano",
portkey_api_key=os.getenv("PORTKEY_API_KEY"),
),
markdown=True
)
# Print the response in the terminal
agent.print_response("What is Portkey and why would I use it as an AI gateway?")Advanced Configuration
For cross-provider fallback, configure each target with its own catalog model string. Replace both provider slugs below with providers configured in your workspace, and use models allowed by those integrations:
import os
from agno.agent import Agent
from agno.models.portkey import Portkey
config = {
"strategy": {
"mode": "fallback"
},
"targets": [
{"override_params": {"model": "@your-openai-provider/gpt-5-mini"}},
{"override_params": {"model": "@your-anthropic-provider/claude-sonnet-4-6"}}
]
}
agent = Agent(
model=Portkey(
id="@your-openai-provider/gpt-5-mini",
config=config,
portkey_api_key=os.getenv("PORTKEY_API_KEY"),
),
)Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | str | "gpt-4o-mini" | The id of the model to use through Portkey |
name | str | "Portkey" | The name of the model |
provider | str | "Portkey" | The provider of the model |
portkey_api_key | Optional[str] | None | The API key for Portkey. Set it explicitly on the model; there is no environment variable fallback |
base_url | str | "https://api.portkey.ai/v1" | The base URL for the Portkey API |
virtual_key | Optional[str] | None | The virtual key for the underlying provider. Falls back to the PORTKEY_VIRTUAL_KEY environment variable if not set |
config | Optional[Dict[str, Any]] | None | Portkey configuration (routing, retries, etc.) |
Portkey also supports the params of OpenAI.
The base_url parameter is the endpoint for the Portkey AI Gateway.
Default value is "https://api.portkey.ai/v1" for the standard managed Portkey service.
You only need to specify this if you are using a self-hosted instance of Portkey or a specific enterprise endpoint.