{
  "question": "How do I see how much each AI agent spends on LLM API calls?",
  "slug": "how-to-see-how-much-each-ai-agent-spends-on-llm-api-calls",
  "url": "https://www.rightshold.com/answers/how-to-see-how-much-each-ai-agent-spends-on-llm-api-calls",
  "short_answer": "Attribute cost by key. If each agent has its own API key (or its own project or workspace), the providers' admin reporting APIs can split spend for you. OpenAI's Costs API (GET /v1/organization/costs) groups daily cost by project_id, api_key_id and line_item, and its Completions usage endpoint gives token counts by key in 1-minute, 1-hour or 1-day buckets. Anthropic's Usage and Cost Analytics API (GET /v1/organizations/analytics/cost, beta) groups cost by api_key_id and workspace_id; its older cost_report groups by workspace only. OpenRouter's GET /api/v1/key returns a key's usage for the current UTC day, week and month. These reports lag (Anthropic says its analytics data typically refreshes within 4 hours and can take up to 24), so use them for accounting and use per-key caps, not reports, to stop a runaway agent.",
  "checklist": [
    "Give each agent its own key, and ideally its own OpenAI project or Claude workspace. Every report below splits by key or project, not by which agent happened to use a shared key.",
    "Create an admin credential for reporting only (OpenAI Admin API key; Anthropic Admin API key) and keep it away from the agents themselves.",
    "OpenAI: call GET /v1/organization/costs with group_by=project_id,api_key_id for dollars (daily buckets only), and GET /v1/organization/usage/completions with group_by=api_key_id,model for tokens and request counts.",
    "Anthropic: call GET /v1/organizations/analytics/cost with group_by[]=api_key_id (send the anthropic-beta: analytics-usage-cost-2026-08-24 header). A request can span at most 31 days, starting no earlier than January 1, 2026.",
    "OpenRouter: call GET /api/v1/key with each agent's key for usage_daily, usage_weekly and usage_monthly plus limit_remaining, or read the same numbers for all keys through the management API.",
    "Map key IDs to agent names in your own table when you create the keys, and record when a key is retired, so old spend still has an owner.",
    "Reconcile monthly: Anthropic says values can be revised until about 7 days after a calendar month ends, so close a month's per-agent numbers only after that.",
    "Count the calls these reports can't see: playground or console usage (Anthropic reports it with a null api_key_id) and paid tools outside the model provider, such as data APIs or x402 payments."
  ],
  "sections": [
    {
      "heading": "What each report can split by",
      "points": [
        "OpenAI's Costs endpoint returns daily buckets and accepts group_by project_id, line_item, api_key_id (plus user_id and api_source in current SDKs) and filters such as project_ids and api_key_ids. Its Completions usage endpoint adds model, batch and service_tier grouping and finer time buckets.",
        "Anthropic's newer analytics cost endpoint groups by dimensions including api_key_id, workspace_id, model and actor, and supports 1-minute, 1-hour and 1-day buckets. Its docs say api_key_id applies only to Claude Console organizations, that the default workspace shows up as a null workspace_id in the legacy reports, and that requests made without an API key have api_key_id null.",
        "OpenRouter attributes spend to the key's creator, and a workspace-owned agent key has no creator, so check usage per key rather than per person. Its key endpoint reports usage counters for the current UTC day, week (from Monday) and month."
      ]
    },
    {
      "heading": "Why reports aren't a brake",
      "points": [
        "Anthropic says analytics data typically refreshes within 4 hours of the usage and can take up to 24 hours, and every response carries a data_refreshed_at timestamp after which numbers are partial. A loop can spend a lot in four hours.",
        "So pair reporting with enforcement: per-project, per-workspace or per-key spend caps at the provider, and a pre-call check for spending the provider doesn't cap."
      ]
    }
  ],
  "checks": [],
  "ask_rightshold": {
    "start": "https://www.rightshold.com/start?ref=seo",
    "mcp_endpoint": "https://www.rightshold.com/mcp",
    "ask": "Record this agent's $0.12 call to api.openai.com against its own spending policy, tagged with my run ID, so its daily total stays separate from my other agents.",
    "mcp_call": {
      "tool": "authorize_spend",
      "arguments": {
        "credential_id": "<this agent's credential id>",
        "amount_minor": 12,
        "currency": "USD",
        "merchant_domain": "api.openai.com",
        "category": "llm_api",
        "reference": "<run id>"
      }
    },
    "also": [
      "Each decision is a signed receipt on Rightshold's ledger with the agent's credential, amount, merchant domain, category and your reference, so per-agent spend can be checked later by anyone you give the receipts to.",
      "GET /v1/spend/policies/{credential_id} (owner account key) shows that agent's policy and spent_today_minor (allowed amounts since 00:00 UTC). Set the policy first with set_spend_policy (toolset=trust).",
      "verify_spend_authorization is free and needs no key, for anyone checking a receipt."
    ],
    "scope": "Rightshold only counts amounts your agent reports to authorize_spend before spending; it doesn't read OpenAI, Anthropic or OpenRouter billing, meter tokens or know the final price of a call. Use the providers' reports for what was actually billed. Not accounting or financial advice."
  },
  "sources": [
    {
      "title": "OpenAI API reference: Costs (GET /v1/organization/costs)",
      "url": "https://developers.openai.com/api/reference/resources/admin/subresources/organization/subresources/usage/methods/costs/"
    },
    {
      "title": "OpenAI API reference: Completions usage (GET /v1/organization/usage/completions)",
      "url": "https://developers.openai.com/api/reference/python/resources/admin/subresources/organization/subresources/usage/methods/completions"
    },
    {
      "title": "Claude docs: Usage and Cost Analytics API (beta)",
      "url": "https://platform.claude.com/docs/en/manage-claude/usage-cost-analytics-api"
    },
    {
      "title": "Claude docs: Usage and Cost API (legacy cost_report, null workspace_id and api_key_id cases)",
      "url": "https://platform.claude.com/docs/en/manage-claude/usage-cost-api"
    },
    {
      "title": "OpenRouter docs: Limits (GET /api/v1/key usage fields)",
      "url": "https://openrouter.ai/docs/api_reference/limits"
    },
    {
      "title": "OpenRouter docs: Capping shared agent keys (spend attribution by key creator)",
      "url": "https://openrouter.ai/docs/guides/best-practices/capping-shared-agent-keys"
    }
  ],
  "related": [
    {
      "title": "How to set a separate budget for each AI agent calling paid APIs",
      "url": "https://www.rightshold.com/answers/how-to-set-a-separate-budget-for-each-ai-agent-calling-paid-apis"
    },
    {
      "title": "How to stop an AI agent from looping forever and burning tokens",
      "url": "https://www.rightshold.com/answers/how-to-stop-an-ai-agent-from-looping-forever-and-burning-tokens"
    },
    {
      "title": "How to rate limit an AI agent's API calls per key",
      "url": "https://www.rightshold.com/answers/how-to-rate-limit-an-ai-agents-api-calls-per-key"
    },
    {
      "title": "How to give an AI agent a virtual card with a spending limit",
      "url": "https://www.rightshold.com/answers/how-to-give-an-ai-agent-a-virtual-card-with-a-spending-limit"
    }
  ],
  "computed_from": "Curated guidance; license verdicts computed from Rightshold's license profiles at render time. Not legal advice.",
  "updated": "2026-10-31"
}