Overview
Lava’s forward proxy sits transparently between your application and AI providers, automatically tracking usage and costs without requiring provider-specific integrations. This guide shows you how to route requests through the proxy with working code examples for multiple providers. What you’ll learn:- How to construct proxy URLs with URL encoding
- Provider-specific request examples (OpenAI, Anthropic, Google)
- Extracting request IDs for debugging
- Error handling and troubleshooting strategies
Prerequisites: You need a Lava forward token. See Quickstart: Route Your First Request to get started.
URL Construction
The Forward Proxy Pattern
Lava uses a query parameter (?u=) to specify the upstream provider endpoint:
- The provider URL must be URL-encoded
- Your forward token goes in the
Authorizationheader - Request body remains unchanged from provider’s API spec
URL Encoding Helper
Always URL-encode the provider endpoint to handle special characters correctly:Provider Examples
OpenAI Chat Completions
Route OpenAI chat requests through Lava:- ✅ URL:
https://api.openai.com/v1/chat/completions→ Lava proxy URL with encoded parameter - ✅ Authorization: OpenAI API key → Lava forward token
- ❌ Request body: No changes (same as OpenAI’s API)
Anthropic Messages API
Route Anthropic Claude requests through Lava:anthropic-version: Required version header (forward to provider)- Provider-specific headers are passed through Lava unchanged
Google Gemini
Route Google Gemini requests through Lava:- Google’s API typically uses
?key=query parameter for authentication - Lava handles provider authentication automatically using your forward token
- No need to manage provider-specific API keys
Response Header Parsing
Request ID Header
Lava adds a request ID header to every response for debugging and tracking:Usage tracking: Lava doesn’t add usage information to response headers. Instead, usage data comes from the provider’s response body (e.g., OpenAI’s
usage field) and is tracked automatically in your Lava dashboard at Monetize > Explore.Usage Tracking Helper
Create a reusable helper for consistent request tracking:Error Handling
Common Error Scenarios
Handle different error types with appropriate retry logic:Retry Logic with Exponential Backoff
Implement retry logic for transient errors:Troubleshooting
Invalid URL encoding error
Invalid URL encoding error
Cause: Provider URL not properly encoded in the
?u= parameterSolution:- Always use
encodeURIComponent()on the full provider URL - Check that the encoded URL doesn’t have double encoding
- Verify the URL structure:
https://api.lavapayments.com/v1/forward?u=<ENCODED_URL>
Provider-specific errors pass through
Provider-specific errors pass through
Cause: The provider (OpenAI, Anthropic, etc.) returned an errorSolution:
- Check the error message for provider-specific issues
- Common provider errors: invalid model name, missing required fields, malformed request body
- Lava passes through provider error messages unchanged
- Verify your request body matches the provider’s API specification
- Look at the error response body for provider details
- Check provider’s API documentation for required fields
- Test the same request directly with the provider to isolate Lava vs provider issues
Missing request ID header
Missing request ID header
Cause: Request ID header not being read correctlySolution:
- Request ID appears on all responses (successful or failed)
- Check that you’re reading headers from the Response object, not the parsed JSON
- Header name is lowercase:
x-lava-request-id
CORS errors in browser
CORS errors in browser
Cause: Attempting to call Lava proxy from client-side JavaScriptSolution:This keeps your forward token secure on the server.
- Lava blocks direct browser requests to prevent token exposure
- Always call Lava from your backend server
- Create a backend API route that proxies requests to Lava
- Your frontend calls your backend, your backend calls Lava
Understanding usage tracking
Understanding usage tracking
How usage tracking works:
- Usage data comes from the provider’s response body, not Lava headers
- Each provider has their own usage format in the response
- Lava automatically tracks all usage in your dashboard at Monetize > Explore
- OpenAI:
usage.total_tokensin response body - Anthropic:
usage.input_tokens+usage.output_tokensin response body - Google: Token counts in
usageMetadatafield
- Real-time: Parse the provider’s native
usagefield from response body - Historical: View all requests in Lava dashboard at Monetize > Explore
- Request correlation: Use
x-lava-request-idheader to match API responses to dashboard entries
What’s Next?
Authentication Guide
Learn how to generate and manage forward tokens
Streaming Responses
Handle real-time streaming responses from LLM providers
Multi-Provider Routing
Abstract provider logic and implement fallback strategies
LLM Providers Reference
Complete provider endpoint reference and features
Deep Dive
Ready to understand how Lava works internally?Architecture Overview
Learn about Lava’s transparent proxy architecture, request flow, and system design