Request Lifecycle
When your application makes an AI API request through Lava, here’s the complete flow:1. Authentication
Your application sends a request with a forward token in the Authorization header. This token contains:- Your merchant secret key
- The connection identifier (links to a specific wallet)
- Optional product configuration (for custom pricing)
2. Balance Check
Before forwarding the request, Lava checks:- Does the wallet have sufficient credits?
- Is the connection active and not deleted?
- Are there any spending limits or quotas?
3. Request Forwarding
Lava forwards your request to the AI provider with:- Transparent proxying: The request body remains unchanged
- Provider authentication: Lava adds the appropriate API key for the provider
- Streaming support: Server-sent events (SSE) work seamlessly
?u= query parameter:
4. Response & Metering
When the AI provider responds:- Lava parses the response to extract usage data (tokens, characters, duration)
- Calculates costs based on the provider’s pricing and your configured fees
- Records the request in the database with full metadata
- Returns the response to your application with usage headers
x-lava-request-id header is added for request tracking. Usage and cost data come from the response body (data.usage), not headers.
5. Billing & Settlement
After the request completes:- Creates transfer records in the internal ledger
- Base cost transfer (to AI provider)
- Fee transfer (to merchant)
- Service charge transfer (to Lava)
- Deducts amounts from the wallet’s active balance
- Updates connection status if balance falls below threshold
Streaming Support
Lava fully supports Server-Sent Events (SSE) for real-time streaming responses:Supported Streaming Providers
Supported Streaming Providers
All major LLM providers support streaming:
- OpenAI (GPT models)
- Anthropic (Claude models)
- Google (Gemini models)
- Mistral, xAI, DeepSeek
- Groq, Fireworks, together.ai
- And all other LLM platforms
How Streaming Works Through Lava
How Streaming Works Through Lava
- Your request includes
"stream": truein the body - Lava detects streaming and forwards the request
- Lava streams the response back to you in real-time
- Usage data is extracted from the final SSE message
- Billing happens after the stream completes
Streaming Response Headers
Streaming Response Headers
Lava adds the request tracking header:Usage data comes from the response body’s final SSE message, not headers.