Passing a config
You can attach a config in two ways: via a request header, or inline in the Portkey client.Via the x-portkey-config header
Base64-encode your config object and pass it as the x-portkey-config header on any request.
Inline in the Portkey client
Pass the config directly when constructing the client, or usewith_options to scope it to specific calls.
Config schema
A config object supports the following top-level fields:Controls the routing strategy. See routing, fallbacks, and load balancing.
mode—"single"|"fallback"|"loadbalance"|"conditional"on_status_codes— HTTP status codes that trigger a fallback (fallback mode only)conditions— routing conditions evaluated against request metadata (conditional mode only)default— name of the default target when no condition matches
List of provider targets to route to. Each target can specify:
provider— provider name ("openai","anthropic","vertex-ai", etc.)api_key/virtual_key— authentication credentialsweight— routing weight for load balancing (0–1)override_params— model parameters to override for this targetretry— per-target retry settingscache— per-target cache settingsname— logical name used in conditional routing
Automatic retry configuration. See retries.
attempts— maximum retry attempts (up to 5)on_status_codes— status codes that trigger a retry (default:[429, 500, 502, 503, 504])
Response caching configuration. See caching.
mode—"simple"or"semantic"max_age— cache TTL in seconds
Guardrail checks applied to LLM responses. Requests that fail a guardrail check with
deny: true are automatically retried.Guardrail checks applied to incoming requests before they are forwarded to a provider.
Override the base URL used to reach the provider (useful for local proxies or custom deployments).
Example: retries + guardrails
This config retries failed requests up to 5 times and denies any response containing the word “Apple”:python
Example: fallback + load balancing combined
Related concepts
Routing
Route requests to different providers based on metadata conditions.
Fallbacks
Automatically switch to a backup provider on failure.
Load balancing
Distribute traffic across providers and API keys.
Retries
Automatically retry failed requests with exponential backoff.