Skip to main content

Token Optimization

The AI Gateway provides tools to understand, analyze, and optimize your AI token usage. Reduce costs while maintaining quality through data-driven decisions.

Understanding Your Usage

Token Analytics Dashboard

Token analytics dashboard
See exactly where your tokens are going:
  • By model - Compare costs across GPT-5.2, Claude, etc.
  • By feature - Which parts of your app use the most tokens
  • By user - Identify heavy users and usage patterns
  • Over time - Track trends and spot anomalies

Cost Breakdown

A/B Testing

Test different approaches to find the most cost-effective solution:
A/B test setup

What to Test

Models

GPT-5.2 vs GPT-5.2 vs Claude
  • Quality vs cost tradeoffs
  • Task-specific performance

Prompts

Different system prompts
  • Shorter vs detailed instructions
  • Different tones/styles

Context Length

How much context to include
  • Minimal vs comprehensive
  • Impact on quality

Temperature

Model creativity settings
  • Lower for consistent outputs
  • Higher for variety

Setting Up an A/B Test

Using Experiments in Code

Tracking Outcomes

Analyzing Results

A/B test results
The dashboard shows:
  • Statistical significance - Is the difference real?
  • Cost comparison - Savings per variant
  • Quality metrics - User satisfaction scores
  • Recommendation - Which variant to choose

Competitor Insights

Learn from how others optimize:
Industry benchmarks

Benchmarking

Compare your usage to industry averages:
  • Tokens per request - Are your prompts too long?
  • Model distribution - Are you using expensive models unnecessarily?
  • Error rates - Are you making inefficient retries?

Learning from Patterns

Aggregated, anonymized insights from the platform help you understand best practices without exposing anyone’s specific implementation.
Common optimizations we’ve identified:
  • 60% of GPT-5.2 usage can use GPT-5.2 with minimal quality loss
  • Shorter system prompts often perform equally well
  • Caching common queries reduces costs by 30-40%

Optimization Strategies

1. Right-Size Your Models

Not every request needs GPT-5.2:
Smart model routing

2. Optimize Prompts

Only include information the model actually needs. More context = more tokens.
“You are a helpful coding assistant” works as well as a 500-word description for most tasks.
Use max_tokens to prevent unnecessarily long responses.
“Reply in JSON format” or “Answer in one sentence” reduces output tokens.

3. Implement Caching

Cache identical or similar requests:

4. Batch Similar Requests

Combine multiple small requests:

Cost Alerts and Limits

Budget Controls

Alert Configuration

Cost alert notification

Reporting

Usage Reports

Generate detailed reports:

Export for Analysis

ROI Calculator

Understand the value of optimization:
ROI calculator

Best Practices

You can’t optimize what you don’t measure. Set up tracking before making changes.
Run isolated A/B tests to understand the impact of each change.
The cheapest option isn’t always the best. Track quality metrics alongside cost.
Usage patterns change. Schedule monthly reviews of your optimization strategies.
Budget alerts prevent surprise bills and catch issues quickly.

Next Steps

Getting Started

Set up the AI Gateway

Full Integration Guide

Complete code examples

Ready? Open your Observability Dashboard →

View your token usage and AI request logs at app.leanmcp.com/observability