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 install @anthropic-ai/sdk
Initialize 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
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." }]
});
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);
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 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
| Error | Cause | Resolution |
|---|---|---|
| 401 | Invalid API key | Check Bearer token and group key. |
| 404 model | Model not in group | Change model or purchase a suitable group key. |
| stream closed | Network/proxy disconnected | Retry and log request ID if available. |
| max_tokens | Value exceeds model limit | Decrease max_tokens or switch to a model with larger context. |