Skip to main content

Variables & Functions

Available Metrics

LangDB provides a rich set of real-time metrics for making dynamic, data-driven routing decisions. These metrics are aggregated at the provider level, giving you a live view of model performance.

Metric NameDescriptionExample ValueBusiness Value
ttftTime to First Token (ms)450Optimize for user-perceived speed
tpsTokens Per Second (output_tokens/latency)300Measure model generation speed
error_rateFraction of failed requests0.01Route around unreliable models
latencyAverage end-to-end response time (ms)1100Route based on overall performance
requestsTotal number of requests processed1500Monitor traffic and usage patterns
costEstimated request cost (USD)0.02Track and control spend in real time

Available Variables

Variables provide contextual information from the incoming request and user metadata. Unlike metrics, they are not performance indicators but are essential for conditional logic.

User Information

Variable PathDescriptionExample ValueBusiness Use
extra.user.idUser identifier from request"u-12345"Custom user identification
extra.user.nameUser name from request"john.doe"Personalization
extra.user.emailUser email from request"john@example.com"User-based routing
extra.user.tierUser tier from request"premium"SLA-based routing
extra.user.tiersArray of user tiers from request["premium", "beta"]Multi-tier access control
extra.user.*Any custom field in extra.userVariousCustom business logic

Request Metadata

Variable PathDescriptionExample ValueBusiness Use
metadata.ipIP address of the requester"203.0.113.42"Geo-fencing, fraud detection
metadata.regionGeographical region (based on IP)"Europe"Data residency, compliance
metadata.countryCountry code (based on IP)"DE"Country-specific routing
metadata.user_agentClient application"Google ADK/ CrewAI"Agentic library detection
metadata.user_idLangDB user ID"langdb-u-12345"Internal user identification
metadata.group_idLangDB cost group ID"grp-456"Cost group identification
metadata.group_nameLangDB cost group name"development"Cost group-based routing
metadata.tags.{tag}LangDB user tags"premium"User classification

Pre-Request Interceptor Results

Variable PathDescriptionExample ValueBusiness Use
pre_request.{interceptor_name}.passedWhether interceptor check passedtrueConditional routing based on checks
pre_request.rate_limiter.passedRate limit check resulttrueEnforce usage quotas
pre_request.semantic_guardrail.passedContent policy check resultfalseBlock policy violations
pre_request.{guardrail}.result.topicDetected topic from guardrail"billing"Topic-based model routing
pre_request.{guardrail}.result.*Any custom result from interceptorVariousCustom business logic

Provider Performance Metrics (for Filtering)

Variable PathDescriptionExample ValueBusiness Use
provider.error_rateProvider's current error rate0.02Avoid unreliable providers
model_capabilitiesSupported features (vision, code)["vision"]Feature-based routing
model_tagsModel classification tags["fast"]Tag-based filtering

Target Configuration

Configuration TypeDescriptionExample UsageWhen to Use
Simple ListDirect array of model names["openai/gpt-4o", "anthropic/claude-4-sonnet"]Fixed model selection
Provider WildcardsAll models from specific provider["openai/*", "anthropic/*"]Provider-level routing
Single TargetDirect model specification"openai/gpt-4o"Fixed routing to one model
$any PoolPool of models with selection logic{ "$any": [...], "sort_by": "price" }Dynamic model selection

Model Target Formats

FormatDescriptionExampleUse Case
Exact Model NameSpecific model identifier"openai/gpt-4o"Direct routing
Provider WildcardAll models from provider"openai/*"Provider-level failover

Sorting Options

Sort ConfigurationWhat It DoesWhen to UseExample JSON Usage
"sort_by": "price", "sort_order": "min"Picks the cheapest modelCost control, bulk/low-priority tasks"sort_by": "price", "sort_order": "min"
"sort_by": "price", "sort_order": "max"Picks the most expensive modelPremium users, highest quality needed"sort_by": "price", "sort_order": "max"
"sort_by": "ttft", "sort_order": "min"Picks the fastest model (time-to-first-token)VIP, real-time, or user-facing tasks"sort_by": "ttft", "sort_order": "min"
"sort_by": "latency", "sort_order": "min"Picks the lowest latency modelPerformance-critical applications"sort_by": "latency", "sort_order": "min"
"sort_by": "error_rate", "sort_order": "min"Picks the most reliable modelMission-critical or regulated workflows"sort_by": "error_rate", "sort_order": "min"
"sort_by": "tps", "sort_order": "max"Picks the fastest token generation modelBulk generation, high throughput"sort_by": "tps", "sort_order": "max"
"sort_by": "requests", "sort_order": "min"Picks the least loaded modelLoad balancing"sort_by": "requests", "sort_order": "min"

Complete Target Examples

Simple Array Targets

{
"targets": ["openai/gpt-4o", "anthropic/claude-4-sonnet"]
}

Provider Wildcard Targets

{
"targets": ["openai/*", "anthropic/*"]
}

Single Target

{
"targets": "openai/gpt-4o"
}

Advanced $any Pool with Filtering and Sorting

{
"targets": {
"$any": [
"openai/*",
"anthropic/claude-4-sonnet",
"mistral/mistral-large-latest"
],
"filter": {
"ttft": { "$lt": 1000 },
"provider.error_rate": { "$lt": 0.05 }
},
"sort_by": "price",
"sort_order": "min"
}
}

Filter Options

Filter TypeDescriptionOperators SupportedExample Usage
filter: { ttft: {...} }Filter models by time-to-first-token$lt, $gt, $lte, $gte, $eq"ttft": { "$lt": 1000 }
filter: { price: {...} }Filter models by cost$lt, $gt, $lte, $gte, $eq"price": { "$lt": 0.02 }
filter: { error_rate: {...} }Filter models by reliability$lt, $gt, $lte, $gte, $eq"error_rate": { "$lt": 0.05 }
filter: { provider.error_rate: {...} }Filter by provider reliability$lt, $gt, $lte, $gte, $eq"provider.error_rate": { "$lt": 0.02 }
filter: { model_tags: {...} }Filter by model classification tags$contains, $in"model_tags": { "$contains": "fast" }

Comparison Operators

OperatorDescriptionExample UsageUse Case
$eqEquals"tier": { "$eq": "premium" }Exact matching
$neNot equals"tier": { "$ne": "free" }Exclusion logic
$ltLess than"ttft": { "$lt": 1000 }Performance thresholds
$lteLess than or equal"price": { "$lte": 0.02 }Budget constraints
$gtGreater than"tps": { "$gt": 100 }Minimum performance requirements
$gteGreater than or equal"reliability": { "$gte": 0.95 }Quality standards
$inIn array"region": { "$in": ["US", "EU"] }Multiple value matching
$containsArray contains value"tags": { "$contains": "GDPR" }Tag-based filtering

Important Notes:

  • Targets: Can be a simple array, single string, or $any object with filtering/sorting
  • Wildcards: Use * for provider-level routing (e.g., "openai/*" = all OpenAI models)
  • Sorting: Use "sort_by" and "sort_order" fields separately
  • Operators: Always use $ prefix for comparison operators ($eq, $lt, etc.)
  • Values: Use lowercase for sort order ("min", "max")