What is Metadata?
Metadata allows you to attach custom context to each LLM call. This additional information helps you:- Debug issues - Add request IDs, user IDs, and error context
- Filter traces - Find specific calls by feature, environment, or user
- Analyze patterns - Group traces by tenant, version, or experiment
- Track business metrics - Associate calls with revenue, user segments, etc.
Use Cases
User Context
Track which users make which calls
user_id, tenant_id, organizationFeature Tracking
Identify which features are being used
feature, experiment_id, variantEnvironment Info
Separate different deployment environments
environment, version, build_idRequest Tracing
Correlate with other systems
request_id, trace_id, span_idBusiness Context
Track business metrics
plan_type, revenue, priorityDebugging
Add context for troubleshooting
error_type, retry_count, sourceVercel AI SDK
Pass metadata viaproviderOptions.observ.metadata:
Provider SDKs
Chain.withMetadata() (TypeScript) or .with_metadata() (Python) to attach data.
Basic Example
- TypeScript
- Python
All Providers
Metadata works with all supported providers:- TypeScript
- Python
Anthropic
Anthropic
OpenAI
OpenAI
Mistral
Mistral
xAI
xAI
OpenRouter
OpenRouter
Common Metadata Fields
Here are commonly used metadata fields and their purposes:user_id
user_id
Purpose: Track which user made the requestExample values:
"user_123", "customer_abc", "anonymous_xyz"Useful for debugging user-specific issues and analyzing usage patterns.feature
feature
Purpose: Identify which feature or flow triggered the callExample values:
"chat", "documentation", "support", "autocomplete"Useful for feature analytics and A/B testing.environment
environment
Purpose: Separate traces by deployment environmentExample values:
"production", "staging", "development", "test"Useful for debugging environment-specific issues. Also available as a top-level configuration option.version
version
Purpose: Track which version of your app made the callExample values:
"1.0.0", "2.3.1", "2024-01-08"Useful for identifying regressions and comparing versions.request_id
request_id
Purpose: Correlate with your application’s request tracingExample values:
"req_abc123", "trace_xyz789"Useful for end-to-end debugging across systems.tenant_id
tenant_id
Purpose: Track multi-tenant applicationsExample values:
"tenant_456", "org_acme", "company_123"Useful for per-tenant analytics and billing.Best Practices
Use consistent field names
Use consistent field names
Standardize your metadata fields across your application for easier filtering and analysis.
Don't include sensitive data
Don't include sensitive data
Avoid adding PII, passwords, or other sensitive information to metadata.
Keep values simple
Keep values simple
Use strings and numbers for metadata values. Avoid complex objects.
Use metadata for filtering
Use metadata for filtering
Design metadata fields that will be useful for filtering in the dashboard.Common filters: user_id, feature, environment, version, tenant_id
Combining with Sessions
You can use both metadata and session tracking together:- TypeScript
- Python
