Attribute requests to an app or service
Attribute requests to an app or service
Name each request so cost and performance slice by product, feature, or environment.
Group analytics by end user or tenant
Group analytics by end user or tenant
Attach an Identity so spend, latency, and error rates attribute to a user, team, or client, with optional per-identity budgets.
Group multi-turn conversations
Group multi-turn conversations
Tag each turn with a Thread ID so the full conversation groups together in observability.
Slice analytics by business context
Slice analytics by business context
Attach key-value metadata, such as tier, channel, or feature flag, and filter traces by those fields.
Overview
Every AI Gateway request can carry context through several mechanisms:name: marks the app or serviceidentity: marks the end user or tenantthread: groups a conversationmetadata: carries business contexttags: adds grouping labels
variables also travel with the request, but fill prompt templates instead of describing the request. The how-to for each mechanism lives in App Tracking, Identities, and Thread Management; the span attribute reference is in Metadata.
Which mechanism to use
Quick Start
Send one request with all five mechanisms attached.Configuration
On
/v3/router/chat/completions:metadatais limited to 16 key-value pairs with keys up to 64 characters and values up to 512 charactersthread,identity, andtagsare passed under theorqobject (orq.thread,orq.identity,orq.tags)nameis passed at the top level
Headers
Clients that cannot modify the request body, such as coding agents, attach metadata, identity, and thread context through headers instead.X-ORQ-IDENTITY-ID and X-ORQ-THREAD-ID are read on every AI Gateway request. X-ORQ-METADATA-<key> and X-ORQ-METADATA are read on the inference endpoints: /v3/router/responses, /v3/anthropic/v1/messages, /v3/google/v1beta/models/* and /v3/google/v1beta/interactions, and the /v3/router/* completions, embeddings, image, audio, moderation, OCR, and rerank endpoints.
When the
X-ORQ-METADATA value starts with {, it is parsed as a JSON object instead of the comma-separated form. String, number, and boolean values are kept; object, array, and null values are skipped.
On the Anthropic Messages and Google endpoints only, a fixed allowlist of headers (user-agent, originator, session-id, session_id, thread-id, x-app, x-claude-code-session-id, x-codex-beta-features, anthropic-beta, anthropic-version, anthropic-dangerous-direct-browser-access) is captured into metadata automatically, when present, to identify the calling coding assistant. No other endpoint captures these headers automatically.
Precedence when the same metadata key is set more than once: the body
metadata object wins, then X-ORQ-METADATA-<key> headers, then the X-ORQ-METADATA header, then, on the Anthropic Messages and Google endpoints only, the automatically-captured allowlist above.X-ORQ-METADATA and X-ORQ-METADATA-<key> combined. On those endpoints, the automatically-captured allowlist headers do not count against this limit. Keys must be 64 characters or fewer and match [a-z0-9._-]+. Values longer than 256 characters are truncated. Entries that fail these rules are dropped silently; the request still succeeds.
Header-derived metadata reaches Traces as metadata.<key> span attributes on every endpoint listed above, filterable the same way as body metadata. It is never forwarded to the model provider.
On endpoints that take a JSON body, X-ORQ-METADATA and X-ORQ-METADATA-<key> are also available to routing rules, guardrail rules, and budgets, so a caller able to set headers on a request can influence which of those rules match. Endpoints that take multipart uploads (transcription, translation, image edit, image variation) put header metadata on traces only. The automatically-captured allowlist above never reaches rule matching, on any endpoint.
Best Practices
- Keep app names low-cardinality: Use a small fixed set of app names (around 50 per workspace) with consistent patterns such as
Service-Environment. Avoid timestamps or dynamic values, which fragment analytics. - Use a fixed metadata key set: Define a small set of keys (
customer_tier,channel,region) and reuse them. High-cardinality keys, such as request IDs or timestamps, defeat filtering and increase storage. - Thread IDs: Use UUIDs or composite keys such as
user-{userId}-{sessionId}to avoid collisions across sessions. - Identity IDs: Use predictable patterns such as
user-{userId}ortenant-{tenantId}so identities stay consistent across requests. - One mechanism per question: If the value describes the app, use
name; if it describes the user, useidentity; if it is business context, usemetadata.
What not to store in request metadata
- PII: Do not put emails, phone numbers, or personal data in
metadata,name, ortags; they persist on stored traces. To keep sensitive values out of stored traces, include"metadata"insecurity.mask, or enable PII Redaction. - Secrets: Pass tokens and keys as template variables with
{"secret": true, "value": "..."}so they are redacted from traces. See Run Agents for the variable reference.