How to beginner · 3 min read

How to check Anthropic API usage

Quick answer
Use the anthropic Python SDK to call the client.usage.get() method with your API key from os.environ. This returns your current usage data including tokens and costs.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the official Anthropic Python SDK and set your API key as an environment variable for secure authentication.

bash
pip install anthropic>=0.20

Step by step

Use the following Python code to retrieve your Anthropic API usage details. It initializes the client with your API key from os.environ and calls the usage.get() method.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

usage_response = client.usage.get()
print("Usage data:", usage_response)
output
Usage data: {'total_usage': {'completion_tokens': 12345, 'prompt_tokens': 6789, 'total_tokens': 19134, 'total_cost': 0.1234}, 'billing_period_start': '2026-04-01', 'billing_period_end': '2026-04-30'}

Common variations

You can also filter usage by date ranges or specific models if supported by the API. For asynchronous usage, use asyncio with the Anthropic client. Different SDK versions may have slightly different method names.

python
import asyncio
import os
import anthropic

async def get_usage_async():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    usage = await client.usage.get()
    print("Async usage data:", usage)

asyncio.run(get_usage_async())
output
Async usage data: {'total_usage': {'completion_tokens': 12345, 'prompt_tokens': 6789, 'total_tokens': 19134, 'total_cost': 0.1234}, 'billing_period_start': '2026-04-01', 'billing_period_end': '2026-04-30'}

Troubleshooting

  • If you get an authentication error, verify your ANTHROPIC_API_KEY environment variable is set correctly.
  • If the usage endpoint returns empty or errors, check your API plan and permissions.
  • For network issues, ensure your environment allows outbound HTTPS requests to Anthropic's API.

Key Takeaways

  • Always use os.environ to securely load your Anthropic API key.
  • Call client.usage.get() to retrieve your current API usage data.
  • Handle authentication and network errors by verifying environment variables and connectivity.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗