Skip to main content
Combine the /v1/usage/costs endpoint with per-model token prices to estimate end-to-end workload costs before running large jobs. This example shows how to turn them into a simple cost calculator.
All cost values are returned in USD. Always re-fetch pricing in long‑lived services to reflect updates in real time.

Example: Cost Estimation Workflow

The following examples:
  • Fetch the flat fees and night-time discount from /v1/usage/costs, and each model’s token price from /v1/models/pricing
  • Estimate costs for:
    • Swarm completions (each agent’s tokens at its own model’s price + per‑agent fee)
    • Agent completions (input/output tokens at the agent’s model price)
    • Images, and auto_search, which is charged once per request however many searches the agent runs
    • Night-time discount multiplier, auto-detected the same way the API applies it: inside the window, and only for paid members
Token cost is tokens / 1,000,000 × listed price. The prices from /v1/models/pricing already include the 15% service fee, so don’t multiply by 1.15 again. Graph workflows and batched grid workflows are priced like a swarm (each agent at its own model, plus $0.01 per agent) but get no night-time discount. MCP servers and the web_scraper tool add no fee.
The night-time discount is not a client-side guess — it mirrors is_night_time in api/deductions_utils.py: off-peak is 8:00 PM through 5:59 AM in America/Los_Angeles (now.hour >= 20 or now.hour < 6, after converting a timezone-aware time to Pacific), and the discount multiplies the swarm completion token cost only — the per-agent fee is always charged in full, at any hour. The API checks the time when it bills, after the run finishes. The discount is for paid members only (tier pro, premium or ultra, or an active subscription): free accounts pay the full token price at any hour. GET /v1/rate/limits reports "tier": "premium" for a paid account and "free" otherwise.

Worked Example

Every tab prices the same workload; the Python tab picks day or night from the current Pacific time and checks your plan, since only paid members get the night-time discount. At the time of writing, gpt-5.4 costs $2.875 per 1M input tokens and $17.25 per 1M output tokens, and claude-sonnet-5 costs $2.30 and $11.50, service fee included. Look prices up rather than hard-coding them; they change with the providers’.
These are estimates. The bill uses the token counts the model provider reports for the real run, summed over every LLM call each agent makes (loops, re-sent history, reasoning tokens), so the amount charged can differ. Swarm types that add agents of their own, such as the HierarchicalSwarm director, also bill those agents’ tokens; the per-agent fee counts only the agents in your request.

When to Use This Pattern

  • Pre-flight cost estimation before running large batch jobs or swarms
  • Dashboards and billing UIs that surface live pricing alongside your own usage metrics
  • Guardrails that block workloads projected to exceed a configured budget