Basic Message Request (Python)
import anthropic
client = anthropic.Anthropic(api_key="YOUR_API_KEY")
message = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what a context window is in one paragraph."}
]
)
print(message.content[0].text)
Using a System Prompt
message = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
system="You are a concise technical writer. Keep answers under 100 words.",
messages=[
{"role": "user", "content": "What is RAG?"}
]
)
print(message.content[0].text)
JavaScript / Node.js Example
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "YOUR_API_KEY" });
const message = await client.messages.create({
model: "claude-sonnet-5",
max_tokens: 1024,
messages: [{ role: "user", content: "Summarize this in two sentences: ..." }],
});
console.log(message.content[0].text);
Common Parameters Worth Knowing
max_tokens is required and caps the length of the generated response; system is a separate top-level field (not part of the messages array) for shaping overall model behavior; temperature controls randomness. See Anthropic's own current documentation for the complete, up-to-date parameter list and model names.
Related Pages
Frequently Asked
Is the model name in these examples always current?
Model names and versions change over time; check Anthropic's current documentation for the latest available model identifiers before deploying.
How is the Claude API different from OpenAI's API structurally?
The overall pattern is similar, though specific details differ — for example, Claude's system prompt is a separate top-level parameter rather than a message within the messages array.
Where do I get an API key?
Through your account on Anthropic's own developer platform; never commit a real API key to public source code or version control.
Where can I see similar examples for other providers?
See our OpenAI API Examples and Gemini API Examples pages for equivalent code in other major providers.