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

# Anthropic SDKs

> Learn how to use Anthropic SDKs with Electron Hub as your base URL

Use Electron Hub with the official Anthropic SDKs by simply changing the base URL. This allows you to access Claude models and 450+ other AI models through familiar Anthropic interfaces.

## Claude Code

```json theme={null}
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.electronhub.ai",
    "ANTHROPIC_AUTH_TOKEN": "ek-your-api-key-here",
    "ANTHROPIC_MODEL": "kimi-k2.6:dev",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "kimi-k2.6:dev",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "kimi-k2.7-code:dev",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-v4-flash:dev"
  }
}
```

Base URL is the host only (Claude Code appends `/v1/messages`). Coding Plan keys require `:dev` model IDs.

## JavaScript/TypeScript SDK

Install the Anthropic SDK:

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

Configure it to use Electron Hub:

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

const anthropic = new Anthropic({
  apiKey: 'ek-your-api-key-here',
  baseURL: 'https://api.electronhub.ai/v1',
});

// Create a message using Claude
const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  temperature: 0,
  system: "You are a helpful assistant.",
  messages: [
    {
      role: 'user',
      content: 'Hello, Claude!'
    }
  ]
});

console.log(message.content);
```

### Using Different Claude Models

Switch between different Claude models:

```javascript theme={null}
// Latest Claude 3.5 Sonnet
const sonnetResponse = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  messages: [
    { role: 'user', content: 'Explain machine learning briefly' }
  ]
});

// Claude 3 Opus for complex reasoning
const opusResponse = await anthropic.messages.create({
  model: 'claude-3-opus-20240229',
  max_tokens: 1000,
  messages: [
    { role: 'user', content: 'Write a complex analysis of economic trends' }
  ]
});

// Claude 3 Haiku for fast responses
const haikuResponse = await anthropic.messages.create({
  model: 'claude-3-haiku-20240307',
  max_tokens: 1000,
  messages: [
    { role: 'user', content: 'Quick summary of the news' }
  ]
});
```

### Vision Capabilities

Use Claude's vision capabilities with images:

```javascript theme={null}
const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'What do you see in this image?'
        },
        {
          type: 'image',
          source: {
            type: 'base64',
            media_type: 'image/jpeg',
            data: '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBD...' // base64 image data
          }
        }
      ]
    }
  ]
});

console.log(message.content);
```

### Streaming Responses

Stream responses for real-time output:

```javascript theme={null}
const stream = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  messages: [
    { role: 'user', content: 'Tell me a long story about space exploration' }
  ],
  stream: true
});

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

## Python SDK

Install the Anthropic Python SDK:

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

Configure it for Electron Hub:

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    api_key="ek-your-api-key-here",
    base_url="https://api.electronhub.ai/v1"
)

# Create a message
message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1000,
    temperature=0,
    system="You are a helpful assistant.",
    messages=[
        {
            "role": "user",
            "content": "Hello, Claude!"
        }
    ]
)

print(message.content)
```

### Tool Use (Function Calling)

Use Claude's tool calling capabilities:

```python theme={null}
import json

# Define tools
tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                }
            },
            "required": ["location"]
        }
    }
]

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1000,
    tools=tools,
    messages=[
        {
            "role": "user", 
            "content": "What's the weather like in Paris?"
        }
    ]
)

print(message.content)

# Handle tool use
for content in message.content:
    if content.type == "tool_use":
        print(f"Tool: {content.name}")
        print(f"Input: {content.input}")
```

### Streaming with Python

```python theme={null}
stream = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1000,
    messages=[
        {"role": "user", "content": "Write a poem about AI"}
    ],
    stream=True
)

for event in stream:
    if event.type == "content_block_delta":
        if event.delta.type == "text_delta":
            print(event.delta.text, end="")
```

## Advanced Features

### Prompt Caching

Reduce costs and improve performance by caching parts of your prompts that don't change between requests. Prompt caching allows you to mark sections of your prompt for caching, with cache writes charged at 1.25x and cache reads at 0.1x the original input price.

<CardGroup cols={1}>
  <Card title="Prompt Caching Guide" icon="zap" href="/guides/prompt-caching">
    Learn how to use prompt caching with Anthropic Claude models, including examples for system and user message caching.
  </Card>
</CardGroup>

### System Messages

Use system messages to set Claude's behavior:

```javascript theme={null}
const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  system: `You are a helpful AI assistant that specializes in explaining complex technical concepts in simple terms. Always:
  1. Use analogies and examples
  2. Break down complex ideas into steps
  3. Ask clarifying questions if needed
  4. Provide practical applications`,
  messages: [
    {
      role: 'user',
      content: 'Explain quantum computing'
    }
  ]
});
```

### Multi-turn Conversations

Handle multi-turn conversations:

```javascript theme={null}
const conversation = [
  { role: 'user', content: 'What is photosynthesis?' },
  { role: 'assistant', content: 'Photosynthesis is the process by which plants...' },
  { role: 'user', content: 'How does this relate to climate change?' }
];

const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  messages: conversation
});
```

### Structured Output

Use Claude for structured data generation:

```javascript theme={null}
const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 1000,
  system: 'You are a JSON generator. Always respond with valid JSON.',
  messages: [
    {
      role: 'user',
      content: 'Generate a JSON object with information about a fictional character including name, age, occupation, and three personality traits.'
    }
  ]
});

const characterData = JSON.parse(message.content[0].text);
console.log(characterData);
```

## Error Handling

Handle errors gracefully:

```javascript theme={null}
try {
  const message = await anthropic.messages.create({
    model: 'claude-sonnet-4-5-20250929',
    max_tokens: 1000,
    messages: [
      { role: 'user', content: 'Hello!' }
    ]
  });
} catch (error) {
  if (error instanceof Anthropic.APIError) {
    console.error('Anthropic API Error:', error.message);
    console.error('Status:', error.status);
    console.error('Type:', error.error?.type);
  } else {
    console.error('Unexpected error:', error);
  }
}
```

## Rate Limiting

Implement rate limiting and retry logic:

```javascript theme={null}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function createMessageWithRetry(params, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await anthropic.messages.create(params);
    } catch (error) {
      if (error.status === 429 && i < maxRetries - 1) {
        // Exponential backoff
        const delay = Math.pow(2, i) * 1000;
        console.log(`Rate limited. Waiting ${delay}ms before retry...`);
        await sleep(delay);
        continue;
      }
      throw error;
    }
  }
}
```

## Best Practices

### 1. Optimize Token Usage

```javascript theme={null}
// Be specific and concise in prompts
const message = await anthropic.messages.create({
  model: 'claude-sonnet-4-5-20250929',
  max_tokens: 500, // Set appropriate limits
  messages: [
    {
      role: 'user',
      content: 'Summarize this article in 3 bullet points: [article text]'
    }
  ]
});
```

### 2. Use Appropriate Models

```javascript theme={null}
// Use Haiku for simple tasks
const quickResponse = await anthropic.messages.create({
  model: 'claude-3-haiku-20240307',
  max_tokens: 100,
  messages: [{ role: 'user', content: 'What time is it in UTC?' }]
});

// Use Opus for complex reasoning
const complexAnalysis = await anthropic.messages.create({
  model: 'claude-3-opus-20240229',
  max_tokens: 2000,
  messages: [{ role: 'user', content: 'Analyze the economic implications of...' }]
});
```

### 3. Implement Caching

```javascript theme={null}
const cache = new Map();

async function getCachedResponse(prompt, model = 'claude-sonnet-4-5-20250929') {
  const cacheKey = `${model}:${prompt}`;
  
  if (cache.has(cacheKey)) {
    return cache.get(cacheKey);
  }
  
  const message = await anthropic.messages.create({
    model,
    max_tokens: 1000,
    messages: [{ role: 'user', content: prompt }]
  });
  
  cache.set(cacheKey, message);
  return message;
}
```

## Integration Examples

### Chatbot Implementation

```javascript theme={null}
class ClaudeChatbot {
  constructor() {
    this.anthropic = new Anthropic({
      apiKey: 'ek-your-api-key-here',
      baseURL: 'https://api.electronhub.ai/v1',
    });
    this.conversation = [];
  }

  async chat(userMessage) {
    this.conversation.push({ role: 'user', content: userMessage });
    
    const message = await this.anthropic.messages.create({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1000,
      system: 'You are a helpful assistant.',
      messages: this.conversation
    });

    const assistantMessage = message.content[0].text;
    this.conversation.push({ role: 'assistant', content: assistantMessage });
    
    return assistantMessage;
  }

  clearHistory() {
    this.conversation = [];
  }
}

// Usage
const chatbot = new ClaudeChatbot();
const response = await chatbot.chat('Hello! How are you?');
console.log(response);
```

### Document Analysis

```javascript theme={null}
async function analyzeDocument(documentText) {
  const message = await anthropic.messages.create({
    model: 'claude-sonnet-4-5-20250929',
    max_tokens: 2000,
    system: 'You are a document analyst. Provide structured analysis of documents.',
    messages: [
      {
        role: 'user',
        content: `Please analyze this document and provide:
        1. Main topics
        2. Key insights
        3. Action items
        4. Summary
        
        Document: ${documentText}`
      }
    ]
  });

  return message.content[0].text;
}
```

## Migration from Anthropic Direct

When migrating from Anthropic's direct API to Electron Hub:

1. **Change the base URL** to `https://api.electronhub.ai/v1`
2. **Update your API key** to use your Electron Hub key (starts with `ek-`)
3. **No code changes needed** - All parameters and responses remain the same
4. **Access to more models** - You can also use OpenAI, Google, and other models
5. **Unified billing** - All model usage is billed through Electron Hub
6. **Enhanced features** - Access to additional capabilities like web search

Monitor your usage through the Electron Hub dashboard at `https://app.electronhub.ai`.
