BifrostContext — a custom context.Context — to pass configuration and metadata through the request lifecycle. Context keys allow you to customize request behavior, pass request-specific settings, and read metadata set by Bifrost.
The idiomatic pattern is to create a BifrostContext and call SetValue (or the chainable WithValue) directly on it:
Request Configuration Keys
These keys can be set before making a request to customize behavior.Virtual Key
Pass a virtual key identifier to the governance plugin for budget and rate-limit enforcement.Extra Headers
Pass custom headers with individual requests. Headers are automatically propagated to the provider.See Custom Headers Per Request for detailed information on header handling and security restrictions.
API Key Selection
Bifrost supports selecting a specific key by ID or name. When both are present, ID takes priority.By ID
Explicitly select a key by its unique ID.By Name
Explicitly select a named API key from your configured keys.Direct Key
Provide credentials directly, bypassing Bifrost’s key selection entirely. Useful for dynamic or per-request key scenarios.Skip Key Selection
Skip key selection entirely and pass an empty key to the provider. Useful for providers that don’t require authentication or when using ambient credentials (e.g., IAM roles).Session Stickiness (Session ID)
Bind a session to a specific API key so that requests with the same session ID consistently use the same key. Useful for predictable rate-limit buckets, cost attribution per user, and consistent model routing per session. On the first request for a session ID, Bifrost selects a key (via weighted random) and caches the binding in the KV store. Subsequent requests with the same session ID reuse the cached key as long as it remains valid. If the cached key is no longer in the supported set (disabled, removed, or model support changed), Bifrost re-selects and overwrites the cache.Session stickiness requires a
KVStore to be configured in BifrostConfig.Session TTL
Controls how long the session-to-key binding is cached. If not set, Bifrost uses a default TTL of 1 hour. The TTL is refreshed on each request so active sessions do not expire.Request ID
Set a custom request ID for tracking and correlation.Custom URL Path
Append a custom path to the provider’s base URL. Useful for accessing provider-specific endpoints.Stream Idle Timeout
Set a per-chunk idle timeout for streaming responses. If no chunk arrives within this duration, the stream is considered stalled and cancelled.Raw Request Body
Send a raw request body instead of Bifrost’s standardized format. The provider receives your payload as-is. You must both set the context key AND populateRawRequestBody on the request.
When using raw request body, Bifrost bypasses its request conversion and sends your payload directly to the provider. You are responsible for ensuring the payload matches the provider’s expected format.
Send Back Raw Request/Response
Include the original request or response bytes inExtraFields for debugging.
Passthrough Extra Parameters
When enabled, any parameters inExtraParams are merged directly into the JSON body sent to the provider, bypassing Bifrost’s parameter filtering. Useful for provider-specific parameters that Bifrost doesn’t natively support.
- This feature only works for JSON requests, not multipart/form-data requests
- Parameters already handled by Bifrost are not duplicated — they appear in their proper location
- Nested parameters are merged recursively with existing nested structures
MCP Context Keys
These keys control MCP tool execution behavior on a per-request basis. Request-level filtering takes priority over client-level configuration.Include Clients
Restrict which MCP clients can provide tools for this request. Pass[]string{"*"} to include all clients, or an empty slice to exclude all.
Include Tools
Restrict which tools are available for this request. Use"clientName-toolName" format for individual tools or "clientName-*" as a wildcard for all tools from a client.
MCP Extra Headers
Forward additional headers to MCP servers during tool execution. Only headers present in the MCP client’s configured allowlist are forwarded.Response Metadata Keys
These keys are set by Bifrost and can be read from the context after a request completes. They are particularly useful in plugins and post-hooks.Selected Key Information
After Bifrost selects an API key, it stores the selection details in the context.Retry and Fallback Information
Track retry attempts and fallback progression.Stream End Indicator
For streaming responses, indicates when the stream has completed. Set by Bifrost automatically.Plugin developers: When implementing a short-circuit streaming response in
PreLLMHook or PostLLMHook, set BifrostContextKeyStreamEndIndicator to true on the last chunk to trigger proper cleanup.Integration Type
Identifies which SDK integration format is in use (useful in gateway plugins).Complete Example
Context Keys Reference
| Key | Type | Direction | Description |
|---|---|---|---|
BifrostContextKeyVirtualKey | string | Set | Virtual key identifier for governance |
BifrostContextKeyAPIKeyName | string | Set | Explicit API key name selection |
BifrostContextKeyAPIKeyID | string | Set | Explicit API key ID selection (priority over name) |
BifrostContextKeyRequestID | string | Set | Custom request ID for tracking |
BifrostContextKeyFallbackRequestID | string | Read | Request ID used for fallback attempt |
BifrostContextKeyDirectKey | schemas.Key | Set | Provide credentials directly, bypassing key selection |
BifrostContextKeySkipKeySelection | bool | Set | Skip key selection entirely |
BifrostContextKeySessionID | string | Set | Session ID for key stickiness (requires KV store) |
BifrostContextKeySessionTTL | time.Duration | Set | TTL for session-to-key cache (default: 1 hour) |
BifrostContextKeyExtraHeaders | map[string][]string | Set | Custom headers forwarded to the provider |
BifrostContextKeyURLPath | string | Set | Custom URL path appended to provider base URL |
BifrostContextKeyStreamIdleTimeout | time.Duration | Set | Per-chunk idle timeout for streaming responses |
BifrostContextKeyUseRawRequestBody | bool | Set | Send raw request body directly to provider |
BifrostContextKeySendBackRawRequest | bool | Set | Include raw request in ExtraFields |
BifrostContextKeySendBackRawResponse | bool | Set | Include raw provider response in ExtraFields |
BifrostContextKeyPassthroughExtraParams | bool | Set | Merge ExtraParams directly into provider request |
MCPContextKeyIncludeClients | []string | Set | Allowlist of MCP client names for this request |
MCPContextKeyIncludeTools | []string | Set | Allowlist of MCP tools ("client-tool" or "client-*") |
BifrostContextKeyMCPExtraHeaders | map[string][]string | Set | Extra headers forwarded to MCP servers during tool execution |
BifrostContextKeySelectedKeyID | string | Read | ID of the key selected by Bifrost |
BifrostContextKeySelectedKeyName | string | Read | Name of the key selected by Bifrost |
BifrostContextKeyNumberOfRetries | int | Read | Number of retry attempts made |
BifrostContextKeyFallbackIndex | int | Read | Current fallback index (0 = primary) |
BifrostContextKeyStreamEndIndicator | bool | Read | Whether the stream has completed |
BifrostContextKeyIntegrationType | string | Read | SDK integration format in use (e.g. "openai") |
BifrostContextKeyUserAgent | string | Read | User agent of the incoming request |
Next Steps
- Provider Configuration - Configure providers and keys
- Streaming Responses - Real-time response handling
- Tool Calling - Enable AI function calling
- Core Features - Advanced Bifrost capabilities

