Skip to main content

LLM context

The llm context object is available in CEL expressions when Agentgateway is proxying requests to an AI backend (backend.type == "ai"). It exposes information about the LLM request and response, including the model used, token counts, and the raw prompt and completion.
The llm object is only present when using an ai backend. Token count fields such as llm.inputTokens and llm.outputTokens are only populated after the response is received from the LLM provider.

Core fields

boolean
required
Whether the LLM response is being streamed.
string
required
The model name requested by the client. This may differ from the model that actually served the response (see llm.responseModel).
string
The model that actually served the LLM response. May differ from llm.requestModel if the provider mapped the requested model to a different version.
string
required
The name of the LLM provider handling the request.

Token counts

Token count fields are populated from the LLM provider’s response and are available after the response is received (for example, in logging and post-response authorization policies).
integer
The number of tokens in the input prompt as reported by the provider.
integer
The number of input tokens served from the provider’s prompt cache. These represent cost savings.
integer
The number of tokens written to the provider’s prompt cache. These represent additional cost for cache creation.
llm.cacheCreationInputTokens is not present when using OpenAI. It is specific to providers that support explicit cache creation (such as Anthropic).
integer
The number of tokens in the LLM response.
integer
The number of reasoning tokens in the output. Only populated for models that support extended reasoning (such as o1 or Claude with thinking enabled).
integer
The total number of tokens for the request (input + output).
integer
The number of tokens counted when using the token counting endpoint. These are not counted as input tokens since the token counting endpoint does not consume tokens.

Prompt and completion

object[]
The prompt sent to the LLM as an array of chat messages. Each message has role and content fields.
Accessing llm.prompt has performance implications for large prompts, as the data must be retained in memory. Only reference this field when necessary.
string[]
The completion returned by the LLM as an array of strings.
Accessing llm.completion has performance implications for large responses, as the data must be retained in memory. Only reference this field when necessary.

Parameters

The llm.params object contains the inference parameters from the LLM request.
object
required
The parameters for the LLM request.

llmRequest

object
The raw LLM request before any LLM policy processing. This is only available during LLM policy evaluation. Policies that run after the LLM policy — such as logging policies — will not have this field even for LLM requests.Use llmRequest when you need access to the unmodified request before transformations are applied.

Examples

Use token counts as the rate limiting key to enforce per-user token budgets. In a rate limiting policy, you can use CEL to select the dimension to limit on:
Combined with threshold checks in authorization policies:
In a logging policy, define named fields using CEL expressions:
Reject requests with a temperature above a threshold in an authorization policy:
Reject requests that request more tokens than your policy allows:
Only allow requests for approved models:
Check prompt messages for sensitive patterns. Use with prompt guard policies or custom authorization:
Sample only requests that exceed a token count threshold for detailed tracing:
Apply different policies based on the provider: