API Formats

Anthropic Format

Configure Anthropic SDK via cheapkeyai.com, using Messages API, streaming, vision, multi-turn, tool use, and count tokens.

Anthropic-compatible is suitable when the application already uses Claude natively or requires Messages API features like system prompt, tool use, vision, and streaming events.

Installation

npm
npm install @anthropic-ai/sdk

Initialize Client

Client
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  apiKey: process.env.CHEAPKEYAI_API_KEY,
  baseURL: "https://cheapkeyai.com"
});

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

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

Messages API

System prompt
const message = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20241022",
  system: "You are a senior engineer, answer short and precisely.",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Review this code snippet." }]
});
Streaming
const stream = await anthropic.messages.stream({
  model: "claude-3-5-sonnet-20241022",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a migration plan." }]
});

stream.on("text", (text) => process.stdout.write(text));
const final = await stream.finalMessage();
console.log(final.usage);
Vision
const message = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20241022",
  max_tokens: 1024,
  messages: [{
    role: "user",
    content: [
      { type: "image", source: { type: "url", url: "https://example.com/screenshot.png" } },
      { type: "text", text: "Analyze UI error in the image." }
    ]
  }]
});

Tool Use

Tool use allows the model to return a function call request. Your application executes the function, returns the results to messages, and then calls it again for the final answer.

Count Tokens

cURL
curl https://cheapkeyai.com/v1/messages/count_tokens \
  -H "Authorization: Bearer " \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-3-5-sonnet-20241022","messages":[{"role":"user","content":"Hello"}]}'

Common Errors

ErrorCauseResolution
401Invalid API keyCheck Bearer token and group key.
404 modelModel not in groupChange model or purchase a suitable group key.
stream closedNetwork/proxy disconnectedRetry and log request ID if available.
max_tokensValue exceeds model limitDecrease max_tokens or switch to a model with larger context.