Skip to content

jy02140251/claude-api-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Claude API Wrapper

Type-safe Claude API wrapper with streaming, function calling, and conversation memory.

Features

  • Full Claude 3.5 Sonnet/Opus support
  • Streaming with async iterators
  • Function/Tool calling
  • Conversation memory management
  • TypeScript-first design
  • Automatic retry with backoff
  • Token counting

Installation

npm install claude-api-wrapper

Quick Start

import { Claude } from 'claude-api-wrapper';

const claude = new Claude({
  apiKey: process.env.ANTHROPIC_API_KEY!,
});

// Simple completion
const response = await claude.complete('Explain quantum computing');
console.log(response.content);

// Streaming
for await (const chunk of claude.stream('Write a poem about coding')) {
  process.stdout.write(chunk);
}

// With conversation memory
const conversation = claude.conversation();
await conversation.send('My name is Alice');
const reply = await conversation.send('What is my name?');
// "Your name is Alice"

API Reference

new Claude(options)

Option Type Default Description
apiKey string required Anthropic API key
model string 'claude-3-5-sonnet' Model to use
maxTokens number 4096 Max output tokens
temperature number 1 Sampling temperature

Methods

// Simple completion
const response = await claude.complete(prompt, options?);

// Streaming
for await (const chunk of claude.stream(prompt, options?)) {
  // chunk is string
}

// With system prompt
const response = await claude.complete(prompt, {
  system: 'You are a helpful coding assistant',
});

// Conversation with memory
const conv = claude.conversation({ system: 'You are helpful' });
await conv.send('Hello');
await conv.send('Follow up question');
conv.clear(); // Reset memory

Function Calling

const response = await claude.complete('What is the weather in Tokyo?', {
  tools: [
    {
      name: 'get_weather',
      description: 'Get current weather for a location',
      input_schema: {
        type: 'object',
        properties: {
          location: { type: 'string', description: 'City name' },
        },
        required: ['location'],
      },
    },
  ],
});

if (response.toolUse) {
  const { name, input } = response.toolUse;
  // name: 'get_weather', input: { location: 'Tokyo' }
  
  // Execute tool and continue conversation
  const result = await getWeather(input.location);
  const final = await claude.complete('', {
    toolResult: { id: response.toolUse.id, content: result },
  });
}

Error Handling

import { ClaudeError, RateLimitError } from 'claude-api-wrapper';

try {
  await claude.complete(prompt);
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log('Rate limited, retry after:', error.retryAfter);
  }
}

License

MIT

About

Production-ready Claude API wrapper with streaming, function calling, and conversation memory. Full TypeScript support. Claude 3.5 Sonnet/Opus ready.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors