Claude API Integration Guide
Report Claude API usage to OpsViz for spend tracking and analytics.
How it works in 30 seconds
- 1.Create an agent in OpsViz → get your webhook URL (10 seconds)
- 2.Add one step to your Claude API workflow → paste the URL (2 minutes)
- 3.See costs, tokens, and alerts in real time (instantly)
Privacy note: OpsViz receives only operational metadata — event type, token counts, model name, cost, and duration. Your prompts, responses, user data, and AI provider credentials are never sent to OpsViz.
Set up with AI — copy and paste
The fastest way to add OpsViz monitoring is to paste this prompt into your AI assistant. It sets everything up automatically.
Using Claude Code (recommended)
If you're using Claude Code to build or maintain your Claude integration, paste this:
I'm calling the Anthropic API in my application and want to add OpsViz cost monitoring. After every API call, send a POST request to [WEBHOOK_URL] with: - event_type: "task.completed" (or "task.failed" if the call failed) - model: the model parameter I used - tokens_in: usage.input_tokens from the response - tokens_out: usage.output_tokens from the response - cost_usd: calculated from token counts (use $3/1M input, $15/1M output for claude-sonnet-4-6) - duration_ms: time taken for the API call - agent_name: a descriptive name for this workflow Make the OpsViz request fire-and-forget using asyncio.create_task() or Promise without await — never let OpsViz being down affect my main application.
Using Claude.ai
Paste this into a Claude.ai conversation with your code attached:
[Attach your code file]
Add OpsViz monitoring to this code. After each Anthropic API call, fire a non-blocking POST request to [WEBHOOK_URL] with event_type ("task.completed", or "task.failed" if the call failed), agent_name (a label for this workflow), the model name, input tokens, output tokens, cost_usd, and duration. The request should never block or fail my main code. Show me the minimal change needed.Prerequisites
- Anthropic Python or Node.js SDK
- An OpsViz account with an agent created
- Your OpsViz webhook URL
1
Wrap your Anthropic API calls
import anthropic, requests, time
client = anthropic.Anthropic()
OPSVIZ_URL = "[WEBHOOK_URL]"
def claude_with_tracking(model, messages, max_tokens=1024,
agent_name="Claude Integration"):
start = time.time()
try:
response = client.messages.create(
model=model,
max_tokens=max_tokens,
messages=messages
)
duration_ms = int((time.time() - start) * 1000)
try:
requests.post(OPSVIZ_URL, json={
"event_type": "task.completed",
"agent_name": agent_name,
"model": model,
"tokens_in": response.usage.input_tokens,
"tokens_out": response.usage.output_tokens,
"duration_ms": duration_ms,
}, timeout=5)
except Exception:
pass # Never let monitoring block your agent
return response
except Exception as e:
try:
requests.post(OPSVIZ_URL, json={
"event_type": "task.failed",
"agent_name": agent_name,
"model": model,
"metadata": {"error": str(e)}
}, timeout=5)
except Exception:
pass
raise2
Usage
response = claude_with_tracking(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Summarise this report..."}],
agent_name="Support Bot"
)That's it!
Events will appear on your OpsViz dashboard as they arrive. Check the webhook reference for full payload options.