Anthropic Hello World
Overview
Three minimal examples covering the Claude Messages API core surfaces: basic text completion, vision (image analysis), and streaming responses.
Prerequisites
- Completed
anth-install-auth setup
- Valid
ANTHROPIC_API_KEY in environment
- Python 3.8+ with
anthropic package or Node.js 18+ with @anthropic-ai/sdk
Instructions
Example 1: Basic Text Message (Python)
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain quantum computing in 3 sentences."}
]
)
# Response structure
print(message.content[0].text) # The actual text response
print(f"ID: {message.id}") # msg_01XFDUDYJgAACzvnptvVoYEL
print(f"Model: {message.model}") # claude-sonnet-4-20250514
print(f"Stop: {message.stop_reason}")# end_turn
print(f"Usage: {message.usage.input_tokens}in / {message.usage.output_tokens}out")
Example 2: Vision — Analyze an Image (TypeScript)
import Anthropic from '@anthropic-ai/sdk';
import * as fs from 'fs';
const client = new Anthropic();
// From file (base64)
const imageData = fs.readFileSync('chart.png').toString('base64');
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{
role: 'user',
content: [
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: imageData,
},
},
{ type: 'text', text: 'Describe what this chart shows.' },
],
}],
});
console.log(message.content[0].type === 'text' ? message.content[0].text : '');
Example 3: Streaming Response (Python)
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a haiku about APIs."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# Get final message with full metadata
final = stream.get_final_message()
print(f"\nTokens used: {final.usage.input_tokens}+{final.usage.output_tokens}")
Output
- Working code file with Claude client initialization
- Successful API response with text content
- Console output showing model response and usage metadata
Error Handling
| Error |
HTTP Code |
Cause |
Solution |
authentication_error |
401 |
Invalid API key |
Check ANTHROPIC_API_KEY |
invalid_request_error |
400 |
Bad params (e.g., empty messages) |
Validate request body |
rate_limit_error |
429 |
Too many requests |
Implement backoff (see anth-rate-limits) |
overloaded_error |
529 |
API temporarily overloaded |
Retry after 30-60s |
api_error |
500 |
Server error |
Retry with exponential backoff |
Key API Parameters
| Parameter |
Required |
Description |
model |
Yes |
Model ID: claude-sonnet-4-20250514, claude-haiku-4-20250514, claude-opus-4-20250514 |
max_tokens |
Yes |
Maximum output tokens (model-dependent max) |
messages |
Yes |
Array of {role, content} objects |
system |
No |
System prompt (string or content blocks) |
temperature |
No |
0.0-1.0, default 1.0 |
top_p |
No |
Nucleus sampling (use temperature OR top_p) |
stop_sequences |
No |
Array of strings that stop generation |
stream |
No |
Enable SSE streaming |
Resources
Next Steps
Proceed to anth-local-dev-loop for development workflow setup.
1---2name: anth-hello-world3description: |4 Create a minimal working Anthropic Claude Messages API example.5 Use when starting a new Claude integration, testing your setup,6 or learning basic Messages API patterns for text, vision, and streaming.7 Trigger with phrases like "anthropic hello world", "claude api example",8 "anthropic quick start", "simple claude code", "first messages api call".9allowed-tools: Read, Write, Edit10version: 1.0.011license: MIT12author: Jeremy Longshore <jeremy@intentsolutions.io>13tags: [saas, ai, anthropic]14compatible-with: claude-code15---1617# Anthropic Hello World1819## Overview2021Three minimal examples covering the Claude Messages API core surfaces: basic text completion, vision (image analysis), and streaming responses.2223## Prerequisites2425- Completed `anth-install-auth` setup26- Valid `ANTHROPIC_API_KEY` in environment27- Python 3.8+ with `anthropic` package or Node.js 18+ with `@anthropic-ai/sdk`2829## Instructions3031### Example 1: Basic Text Message (Python)3233```python34import anthropic3536client = anthropic.Anthropic()3738message = client.messages.create(39 model="claude-sonnet-4-20250514",40 max_tokens=1024,41 messages=[42 {"role": "user", "content": "Explain quantum computing in 3 sentences."}43 ]44)4546# Response structure47print(message.content[0].text) # The actual text response48print(f"ID: {message.id}") # msg_01XFDUDYJgAACzvnptvVoYEL49print(f"Model: {message.model}") # claude-sonnet-4-2025051450print(f"Stop: {message.stop_reason}")# end_turn51print(f"Usage: {message.usage.input_tokens}in / {message.usage.output_tokens}out")52```5354### Example 2: Vision — Analyze an Image (TypeScript)5556```typescript57import Anthropic from '@anthropic-ai/sdk';58import * as fs from 'fs';5960const client = new Anthropic();6162// From file (base64)63const imageData = fs.readFileSync('chart.png').toString('base64');6465const message = await client.messages.create({66 model: 'claude-sonnet-4-20250514',67 max_tokens: 1024,68 messages: [{69 role: 'user',70 content: [71 {72 type: 'image',73 source: {74 type: 'base64',75 media_type: 'image/png',76 data: imageData,77 },78 },79 { type: 'text', text: 'Describe what this chart shows.' },80 ],81 }],82});8384console.log(message.content[0].type === 'text' ? message.content[0].text : '');85```8687### Example 3: Streaming Response (Python)8889```python90import anthropic9192client = anthropic.Anthropic()9394with client.messages.stream(95 model="claude-sonnet-4-20250514",96 max_tokens=1024,97 messages=[{"role": "user", "content": "Write a haiku about APIs."}]98) as stream:99 for text in stream.text_stream:100 print(text, end="", flush=True)101102# Get final message with full metadata103final = stream.get_final_message()104print(f"\nTokens used: {final.usage.input_tokens}+{final.usage.output_tokens}")105```106107## Output108109- Working code file with Claude client initialization110- Successful API response with text content111- Console output showing model response and usage metadata112113## Error Handling114115| Error | HTTP Code | Cause | Solution |116|-------|-----------|-------|----------|117| `authentication_error` | 401 | Invalid API key | Check `ANTHROPIC_API_KEY` |118| `invalid_request_error` | 400 | Bad params (e.g., empty messages) | Validate request body |119| `rate_limit_error` | 429 | Too many requests | Implement backoff (see `anth-rate-limits`) |120| `overloaded_error` | 529 | API temporarily overloaded | Retry after 30-60s |121| `api_error` | 500 | Server error | Retry with exponential backoff |122123## Key API Parameters124125| Parameter | Required | Description |126|-----------|----------|-------------|127| `model` | Yes | Model ID: `claude-sonnet-4-20250514`, `claude-haiku-4-20250514`, `claude-opus-4-20250514` |128| `max_tokens` | Yes | Maximum output tokens (model-dependent max) |129| `messages` | Yes | Array of `{role, content}` objects |130| `system` | No | System prompt (string or content blocks) |131| `temperature` | No | 0.0-1.0, default 1.0 |132| `top_p` | No | Nucleus sampling (use temperature OR top_p) |133| `stop_sequences` | No | Array of strings that stop generation |134| `stream` | No | Enable SSE streaming |135136## Resources137138- [Messages API Reference](https://docs.anthropic.com/en/api/messages)139- [Messages Examples](https://docs.anthropic.com/en/api/messages-examples)140- [Vision Guide](https://docs.anthropic.com/en/docs/build-with-claude/vision)141142## Next Steps143144Proceed to `anth-local-dev-loop` for development workflow setup.145