Portkey

Use Portkey AI Gateway for multi-provider routing with Kern 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

You need both a Portkey API key and a virtual key for model routing. Get them from Portkey here.

1export PORTKEY_API_KEY=***
2export PORTKEY_VIRTUAL_KEY=***
1setx PORTKEY_API_KEY ***
2setx PORTKEY_VIRTUAL_KEY ***

Example

Use Portkey with your Agent:

1from kern.agent import Agent
2from kern.models.portkey import Portkey
3
4agent = Agent(
5 model=Portkey(id="gpt-5-mini"),
6 markdown=True
7)
8
9# Print the response in the terminal
10agent.print_response("What is Portkey and why would I use it as an AI gateway?")

Advanced Configuration

You can configure Portkey with custom routing and retry policies:

1from kern.agent import Agent
2from kern.models.portkey import Portkey
3
4config = {
5 "strategy": {
6 "mode": "fallback"
7 },
8 "targets": [
9 {"virtual_key": "openai-key"},
10 {"virtual_key": "anthropic-key"}
11 ]
12}
13
14agent = Agent(
15 model=Portkey(
16 id="gpt-5-mini",
17 config=config,
18 ),
19)
Note View more examples here.

Params

ParameterTypeDefaultDescription
idstr"gpt-4o-mini"The id of the model to use through Portkey
namestr"Portkey"The name of the model
providerstr"Portkey"The provider of the model
portkey_api_keyOptional[str]NoneThe API key for Portkey (defaults to PORTKEY_API_KEY env var)
base_urlstr"https://api.portkey.ai/v1"The base URL for the Portkey API
virtual_keyOptional[str]NoneThe virtual key for the underlying provider
configOptional[Dict[str, Any]]NonePortkey configuration (routing, retries, etc.)

Portkey also supports the params of OpenAI.

Tip

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.