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.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).Direct Key
Supply a raw provider API key to use directly, bypassing the registered key pool. SetBifrostContextKeyDirectKey to a schemas.Key and Bifrost uses it as-is — there is no flag to enable in the SDK (the gateway’s allow_direct_keys setting only gates the HTTP header path that populates this same key).
Session Stickiness (Session ID)
Keep a session on what served it before: the provider that last served it, when routing still offers that provider, and the key that last served it within that provider. A request that names its provider is never reordered; only the key level applies to it. Useful for keeping provider prompt caches warm across a conversation, predictable rate-limit buckets, and cost attribution per user. Bindings are written when a request is served, never when it fails. A later request with the same session ID puts the bound provider first among the routes the hooks produced and uses the bound key while it remains eligible; a key that is no longer in the supported set (disabled, removed, or model support changed) is dropped and the next served request rebinds. A fallback that serves becomes the session’s new home. Decisions the session made are recorded in the request’s routing logs under thesession-affinity engine.
The policy that picks and keeps the key is
BifrostConfig.SessionAffinity. When it is unset, Bifrost’s own default applies: it needs a KVStore in BifrostConfig, binds the session to the key selected for its first request, and scopes bindings to the virtual key and user the request’s grant identity is attributed to, so a session id reused by another caller does not share them. A custom SessionAffinity may keep its state elsewhere and scope it differently.Session TTL
Controls how long the session bindings are kept. If not set, Bifrost uses a default TTL of 1 hour. The TTL is refreshed by each served request so active sessions do not expire.Session Affinity Switch
Whether a request lets its session decide where it goes. On by default;false leaves the provider to routing and the key to key selection, and writes no session state for the request.
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
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

