> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloudidr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Integration

> Learn how to integrate LLM Ops with Anthropic's Claude API for cost tracking and monitoring

## Overview

Track costs and monitor usage for Anthropic's Claude API by routing your requests through LLM Ops. This guide shows you how to integrate using Python, JavaScript, or cURL.

<Note>
  **Security Guarantee**: LLM Ops does not store your API keys, request prompts, or response content in the **analytics database**—only metadata needed for cost analytics. The proxy must forward request bodies to Anthropic to complete the call; optional operational logging may exist in your deployment environment.
</Note>

## Quick Start

Point the Anthropic SDK at the LLM Ops **API host** (do **not** append `/v1`—the SDK adds `/v1/messages` itself). For raw HTTP (cURL), use the full path `https://api.llm-ops.cloudidr.com/v1/messages`.

* **Original API host:** `https://api.anthropic.com`
* **LLM Ops API host (SDK `base_url` / `baseURL`):** `https://api.llm-ops.cloudidr.com`
* **cURL URL:** `https://api.llm-ops.cloudidr.com/v1/messages`

## API Keys

You'll need two credentials:

1. **Anthropic API Key** - Your Claude API key from [console.anthropic.com](https://console.anthropic.com)
2. **Cloudidr Key** - Your tracking token from the [LLM Ops dashboard](https://llm-ops.cloudidr.com/dashboard) (tokens are typically prefixed with `trk_`)

The marketing site [llmfinops.ai](https://llmfinops.ai) points at the same product; the dashboard URL above is the canonical app host.

Set them as environment variables:

```bash theme={null}
export ANTHROPIC_API_KEY="sk-ant-..."
export CLOUDIDR_KEY="trk_..."
```

***

## Integration Examples

<Tabs>
  <Tab title="Python">
    ### Install SDK

    ```bash theme={null}
    pip install anthropic
    ```

    ### Basic Example

    ```python theme={null}
    from anthropic import Anthropic

    # Initialize client with LLM Ops proxy (no /v1 — SDK appends /v1/messages)
    client = Anthropic(
        api_key="sk-ant-...",  # Your Anthropic API key
        base_url="https://api.llm-ops.cloudidr.com",
        default_headers={
            "X-Cloudidr-Key": "trk_..."  # Required for cost tracking
        }
    )

    # Make API call - costs are automatically tracked
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "What is the capital of France?"}
        ]
    )

    print(message.content[0].text)
    ```

    ### With Metadata (Department/Project/Agent Tracking)

    ```python theme={null}
    from anthropic import Anthropic

    client = Anthropic(
        api_key="sk-ant-...",
        base_url="https://api.llm-ops.cloudidr.com"
    )

    # Track costs by department, team, and agent
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Help me debug this code"}
        ],
        extra_headers={
            "X-Cloudidr-Key": "trk_...",
            "X-Department": "engineering",
            "X-Project": "backend",
            "X-Agent": "code-assistant"
        }
    )

    print(message.content[0].text)
    ```

    ### Streaming Example

    ```python theme={null}
    from anthropic import Anthropic

    client = Anthropic(
        api_key="sk-ant-...",
        base_url="https://api.llm-ops.cloudidr.com",
        default_headers={
            "X-Cloudidr-Key": "trk_...",
            "X-Agent": "streaming-bot"
        }
    )

    # Streaming is fully supported
    with client.messages.stream(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Write a short poem about AI"}
        ]
    ) as stream:
        for text in stream.text_stream:
            print(text, end="", flush=True)
    ```
  </Tab>

  <Tab title="JavaScript">
    ### Install SDK

    ```bash theme={null}
    npm install @anthropic-ai/sdk
    ```

    ### Basic Example

    ```javascript theme={null}
    import Anthropic from '@anthropic-ai/sdk';

    // Initialize client with LLM Ops proxy (no /v1 — SDK appends /v1/messages)
    const client = new Anthropic({
      apiKey: 'sk-ant-...',  // Your Anthropic API key
      baseURL: 'https://api.llm-ops.cloudidr.com',
      defaultHeaders: {
        'X-Cloudidr-Key': 'trk_...'  // Required for cost tracking
      }
    });

    // Make API call - costs are automatically tracked
    const message = await client.messages.create({
      model: 'claude-sonnet-4-20250514',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'What is the capital of France?' }
      ]
    });

    console.log(message.content[0].text);
    ```

    ### With Metadata (Department/Project/Agent Tracking)

    ```javascript theme={null}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      apiKey: 'sk-ant-...',
      baseURL: 'https://api.llm-ops.cloudidr.com'
    });

    // Track costs by department, team, and agent
    const message = await client.messages.create({
      model: 'claude-sonnet-4-20250514',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Help me debug this code' }
      ]
    }, {
      headers: {
        'X-Cloudidr-Key': 'trk_...',
        'X-Department': 'engineering',
        'X-Project': 'backend',
        'X-Agent': 'code-assistant'
      }
    });

    console.log(message.content[0].text);
    ```

    ### Streaming (Node.js)

    The snippet below uses `process.stdout`, which is available in Node.js. In the browser, append `chunk.delta.text` to your UI or a string buffer instead.

    ```javascript theme={null}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      apiKey: 'sk-ant-...',
      baseURL: 'https://api.llm-ops.cloudidr.com',
      defaultHeaders: {
        'X-Cloudidr-Key': 'trk_...',
        'X-Agent': 'streaming-bot'
      }
    });

    const stream = await client.messages.stream({
      model: 'claude-sonnet-4-20250514',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Write a short poem about AI' }
      ]
    });

    for await (const chunk of stream) {
      if (chunk.type === 'content_block_delta' && 
          chunk.delta.type === 'text_delta') {
        process.stdout.write(chunk.delta.text);
      }
    }
    ```
  </Tab>

  <Tab title="cURL">
    ### Basic Example

    ```bash theme={null}
    curl https://api.llm-ops.cloudidr.com/v1/messages \
      -H "Content-Type: application/json" \
      -H "x-api-key: sk-ant-..." \
      -H "anthropic-version: 2023-06-01" \
      -H "X-Cloudidr-Key: trk_..." \
      -d '{
        "model": "claude-sonnet-4-20250514",
        "max_tokens": 1024,
        "messages": [
          {
            "role": "user",
            "content": "What is the capital of France?"
          }
        ]
      }'
    ```

    ### With Metadata (Department/Project/Agent Tracking)

    ```bash theme={null}
    curl https://api.llm-ops.cloudidr.com/v1/messages \
      -H "Content-Type: application/json" \
      -H "x-api-key: sk-ant-..." \
      -H "anthropic-version: 2023-06-01" \
      -H "X-Cloudidr-Key: trk_..." \
      -H "X-Department: engineering" \
      -H "X-Project: backend" \
      -H "X-Agent: code-assistant" \
      -d '{
        "model": "claude-sonnet-4-20250514",
        "max_tokens": 1024,
        "messages": [
          {
            "role": "user",
            "content": "Help me debug this code"
          }
        ]
      }'
    ```

    ### Streaming Example

    ```bash theme={null}
    curl https://api.llm-ops.cloudidr.com/v1/messages \
      -H "Content-Type: application/json" \
      -H "x-api-key: sk-ant-..." \
      -H "anthropic-version: 2023-06-01" \
      -H "X-Cloudidr-Key: trk_..." \
      -H "X-Agent: streaming-bot" \
      -d '{
        "model": "claude-sonnet-4-20250514",
        "max_tokens": 1024,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "Write a short poem about AI"
          }
        ]
      }'
    ```
  </Tab>
</Tabs>

***

## Cost Tracking Headers

Add these headers to organize your costs by department, team, or agent:

| Header           | Description                                 | Example                                           |
| ---------------- | ------------------------------------------- | ------------------------------------------------- |
| `X-Cloudidr-Key` | **Required** - Your Cloudidr tracking token | `trk_abc123...`                                   |
| `X-Department`   | Track costs by department                   | `engineering`, `sales`, `marketing`, `support`    |
| `X-Project`      | Track costs by team                         | `backend`, `frontend`, `ml`, `data`, `qa`         |
| `X-Agent`        | Track costs by agent/application            | `chatbot`, `summarizer`, `analyzer`, `translator` |

***

## Supported Models

All Anthropic Claude models are supported. See the [Supported Models](/guides/llm-ops/supported-models) page for the complete list of available models and pricing.

***

## What Gets Tracked

LLM Ops automatically captures:

✅ **Token usage** - Input and output tokens\
✅ **Cost** - Real-time cost calculation\
✅ **Latency** - Request duration\
✅ **Model** - Which Claude model was used\
✅ **Metadata** - Department, team, agent\
✅ **Errors** - Failed requests and error types

<Warning>
  **What We DON'T Track:**

  * ❌ Customer API keys
  * ❌ Request content (prompts)
  * ❌ Response content (completions)

  We only persist metadata needed for cost analytics in our application database.
</Warning>

***

## View Your Data

After making requests, view your costs in the [LLM Ops Dashboard](https://llm-ops.cloudidr.com/dashboard):

* **Agent Explorer** - See costs by agent/application
* **Department Breakdown** - Compare department spending
* **Team Analysis** - Track team-level costs
* **Model Comparison** - Compare costs across models
* **Time Series** - Track spending over time

***

## Migration from Direct API

Switching from direct Anthropic API to LLM Ops is a two-line change:

```python theme={null}
# Before
client = Anthropic(api_key="sk-ant-...")

# After - add base_url (API host only, no /v1) and X-Cloudidr-Key header
client = Anthropic(
    api_key="sk-ant-...",
    base_url="https://api.llm-ops.cloudidr.com",      # ← Add this
    default_headers={"X-Cloudidr-Key": "trk_..."}  # ← Add this
)
```

Everything else stays the same - no code changes needed!

***

## Troubleshooting

<AccordionGroup>
  <Accordion title="Requests not being tracked" icon="exclamation-triangle">
    **Check these common issues:**

    * ✅ For **SDKs**, set `base_url` / `baseURL` to `https://api.llm-ops.cloudidr.com` (do **not** include `/v1`; the SDK adds `/v1/messages`).
    * ✅ For **cURL**, call `https://api.llm-ops.cloudidr.com/v1/messages`.
    * ✅ Confirm `X-Cloudidr-Key` is included on every request (or in `extra_headers` / per-request `headers` where applicable).
    * ✅ Check that your Anthropic API key is valid.
  </Accordion>

  <Accordion title="Authentication errors" icon="key">
    **Two separate keys are needed:**

    * Your Anthropic API key (for Claude access)
    * Your Cloudidr tracking token (for cost tracking)

    Make sure both are set correctly and not swapped.
  </Accordion>

  <Accordion title="Cost data not appearing" icon="chart-line">
    **Wait a few moments:**

    * Cost data may take 10-30 seconds to appear in dashboard
    * Check the correct time range in dashboard filters
    * Verify requests are returning 200 OK status
  </Accordion>
</AccordionGroup>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="View Dashboard" icon="chart-line" href="https://llm-ops.cloudidr.com/login">
    See your Claude API costs in real-time
  </Card>

  <Card title="Supported Models" icon="list" href="/guides/llm-ops/supported-models">
    View all supported Claude models
  </Card>

  <Card title="OpenAI Integration" icon="brain" href="/guides/llm-ops/integrations/openai">
    Add cost tracking for GPT models
  </Card>

  <Card title="Set Budgets" icon="wallet" href="guides/llm-ops/budget-guard">
    Configure spending alerts and limits
  </Card>
</CardGroup>
