API Documentation: Costs & Budgets

Get a grip on your LLM usage in production environments

Why Monitor LLM Costs?

When scaling generative AI or RAG (Retrieval-Augmented Generation) applications, API costs can quickly add up. Without close monitoring of input (prompt) and output (completion) tokens per session, you risk budget overruns. A robust monitoring system breaks down costs per model, user, or project.

For an efficient setup, it is also essential to know which models offer the best price-to-performance ratio. To do so, consult our comprehensive LLM Benchmark.

Example Dashboard (Concept)

A good dashboard displays the "burn rate" at a glance. Below you can see a structural layout of the metrics aggregated by our API.

Costs this month

€342.50
75% of €450 budget

Token Usage (Top Models)

4.2M
60% Claude Pro | 40% DeepSeek

Active Alerts

2
Warning: User_ID_89 is approaching hard-cap

Implementation: Quotas and Logging (Pseudocode)

To control costs per user, the API gateway validates current usage before forwarding the request to the external LLM provider. After the response, the actual tokens consumed are logged.

# Pseudocode: LLM Request Middleware
def handle_llm_request(user_id, prompt_data, model="deepseek-chat"):
    # 1. Check budget limit
    current_spend = get_user_spend(user_id, current_month)
    if current_spend > USER_BUDGET_LIMIT:
        return {"error": "Budget exceeded. Please contact the administrator."}
        
    # 2. Execute the request via the LLMnet.nl aggregator
    response = llm_aggregator.call(model, prompt_data)
    
    # 3. Calculate costs based on token pricing
    cost = calculate_cost(
        model, 
        prompt_tokens=response.usage.prompt_tokens, 
        completion_tokens=response.usage.completion_tokens
    )
    
    # 4. Update the database & trigger any alerts
    log_usage(user_id, cost)
    check_and_trigger_alerts(user_id, current_spend + cost)
    
    return response.content

Best Practices for Budgeting