Overview
LLM APIs let you integrate AI capabilities directly into your applications. Whether you are building a coding assistant, chatbot, content generator, or data analysis tool, understanding how to call LLM APIs effectively is an essential developer skill.
API Comparison
| Provider | Top Model | Context | Price (Input/Output per 1M tokens) |
|---|---|---|---|
| Anthropic | Claude 3.5 Sonnet | 200K | $3 / $15 |
| OpenAI | GPT-4o | 128K | $2.50 / $10 |
| Gemini 1.5 Pro | 1M+ | $1.25 / $5 | |
| Meta (via providers) | Llama 3.1 405B | 128K | Varies by host |
Example: Claude API (TypeScript)
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Write a TypeScript function to validate email addresses.',
},
],
});
console.log(message.content[0].text);Best Practices
- •Always set max_tokens to prevent runaway costs
- •Implement retry logic with exponential backoff for rate limits
- •Cache LLM responses when the same prompt produces the same result
- •Use streaming for long responses to improve perceived latency
- •Store API keys in environment variables, never in code
- •Monitor usage and set billing alerts to prevent surprise costs