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

# Claude

> Trace Anthropic Claude API calls with Netra auto-instrumentation. Monitor prompts, completions, token usage, and response latency automatically.

<img src="https://mintcdn.com/netra/u6ajHWd7ki_9CRWQ/images/integration-logos/llm-providers/claude.png?fit=max&auto=format&n=u6ajHWd7ki_9CRWQ&q=85&s=431b4b17bf8cbfc598a34706d646943f" alt="Anthropic Claude" width="321" height="80" data-path="images/integration-logos/llm-providers/claude.png" />

The Netra SDK automatically instruments Anthropic Claude API calls, capturing prompts, completions, token usage, and performance metrics through OpenTelemetry tracing.

# Installation

Install the Netra SDK and the Anthropic client library:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk anthropic
  ```

  ```bash TypeScript theme={null}
  npm install netra-sdk @anthropic-ai/sdk
  ```
</CodeGroup>

# Usage

Initialize the Netra SDK with your API key to automatically trace all Anthropic Claude API calls.

## Basic Setup

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import anthropic
  import os

  # Initialize Netra with your API key
  Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

  # Use Anthropic client as normal
  client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

  message = client.messages.create(
      model="claude-3-5-sonnet-20241022",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "Hello, Claude"}
      ]
  )

  print(message.content)
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Anthropic from "@anthropic-ai/sdk";

  async function main() {
    // Initialize Netra with your API key (must await)
    await Netra.init({
        headers: `x-api-key=${process.env.NETRA_API_KEY}`
    });

    // Use Anthropic client as normal
    const client = new Anthropic({
        apiKey: process.env.ANTHROPIC_API_KEY
    });

    const message = await client.messages.create({
        model: "claude-3-5-sonnet-20241022",
        max_tokens: 1024,
        messages: [
            { role: "user", content: "Hello, Claude" }
        ]
    });

    console.log(message.content);
  }

  main();
  ```
</CodeGroup>

## Streaming Responses

The SDK automatically traces streaming responses from Claude.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import anthropic
  import os

  Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

  client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

  with client.messages.stream(
      model="claude-3-5-sonnet-20241022",
      max_tokens=1024,
      messages=[{"role": "user", "content": "Tell me a story"}]
  ) as stream:
      for text in stream.text_stream:
          print(text, end="", flush=True)
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Anthropic from "@anthropic-ai/sdk";

  async function main() {
    await Netra.init({
        headers: `x-api-key=${process.env.NETRA_API_KEY}`
    });

    const client = new Anthropic({
        apiKey: process.env.ANTHROPIC_API_KEY
    });

    const stream = await client.messages.create({
        model: "claude-3-5-sonnet-20241022",
        max_tokens: 1024,
        messages: [{ role: "user", content: "Tell me a story" }],
        stream: true
    });

    for await (const chunk of stream) {
        if (chunk.type === 'content_block_delta') {
            process.stdout.write(chunk.delta.text);
        }
    }
  }

  main();
  ```
</CodeGroup>

## Session Tracking

Track user sessions and conversations across multiple Claude API calls.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra, ConversationType
  import anthropic
  import os

  Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

  # Set session and user context
  Netra.set_session_id("session-123")
  Netra.set_user_id("user-456")

  client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

  # Track conversation
  Netra.add_conversation(
      content="What is machine learning?",
      conversation_type=ConversationType.INPUT
  )

  response = client.messages.create(
      model="claude-3-5-sonnet-20241022",
      max_tokens=1024,
      messages=[{"role": "user", "content": "What is machine learning?"}]
  )

  Netra.add_conversation(
      content=response.content[0].text,
      conversation_type=ConversationType.OUTPUT
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Anthropic from "@anthropic-ai/sdk";

  async function main() {
    await Netra.init({
        headers: `x-api-key=${process.env.NETRA_API_KEY}`
    });

    // Set session and user context
    Netra.setSessionId("session-123");
    Netra.setUserId("user-456");

    const client = new Anthropic({
        apiKey: process.env.ANTHROPIC_API_KEY
    });

    // API calls are automatically traced with session context
    const message = await client.messages.create({
        model: "claude-3-5-sonnet-20241022",
        max_tokens: 1024,
        messages: [
            { role: "user", content: "What is machine learning?" }
        ]
    });
  }

  main();
  ```
</CodeGroup>

## Custom Attributes

Add custom metadata to your Claude API traces for filtering and analysis.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import anthropic
  import os

  Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

  # Set custom attributes
  Netra.set_custom_attributes({
      "environment": "production",
      "feature": "chat-assistant",
      "model_version": "v2"
  })

  client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

  message = client.messages.create(
      model="claude-3-5-sonnet-20241022",
      max_tokens=1024,
      messages=[{"role": "user", "content": "Hello"}]
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Anthropic from "@anthropic-ai/sdk";

  async function main() {
    await Netra.init({
        headers: `x-api-key=${process.env.NETRA_API_KEY}`
    });

    // Set custom attributes
    Netra.setCustomAttributes({
        environment: "production",
        feature: "chat-assistant",
        modelVersion: "v2"
    });

    const client = new Anthropic({
        apiKey: process.env.ANTHROPIC_API_KEY
    });

    const message = await client.messages.create({
        model: "claude-3-5-sonnet-20241022",
        max_tokens: 1024,
        messages: [{ role: "user", content: "Hello" }]
    });
  }

  main();
  ```
</CodeGroup>

# Next Steps

* [Auto-instrumentation Guide](https://docs.getnetra.ai/tracing/auto-instrumentation) - Learn about automatic tracing capabilities
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [Analytics Dashboard](https://docs.getnetra.ai/Dashboard/Custom-dashboard) - Query and visualize your Claude API usage
* [Anthropic Claude Documentation](https://docs.anthropic.com/en/home) - Official Anthropic Claude API documentation
