Anthropic API Basics

Anthropic API Basics

Getting started with the Anthropic Messages API

Features

  • Messages API for multi-turn conversations
  • System prompts for behavior customization
  • Streaming responses for real-time output
  • Token counting and usage tracking

Pros

  • Simple, well-designed API surface
  • Comprehensive documentation with examples
  • Predictable pricing based on token usage

Cons

  • Rate limits vary by usage tier
  • Requires understanding of token economics
  • API key management and security responsibility

Overview

The Anthropic Messages API is the foundation for interacting with Claude models. It uses a simple request/response model where you send a list of messages (with roles of “user” and “assistant”) and receive a response from Claude. The API supports system prompts for customizing Claude’s behavior, streaming for real-time output, and tool use for function calling.

Understanding the API basics is essential before diving into specific SDKs or frameworks. The Messages API is model-agnostic within the Claude family: the same API works with Claude Opus, Sonnet, and Haiku, with the model specified as a parameter.

Key concepts include token counting (input and output tokens determine cost), context windows (how much text Claude can process at once), and response formatting (text blocks, tool use blocks).

When to Use

Start here if you are new to the Anthropic API. Understand these fundamentals before choosing an SDK or framework.

Getting Started

curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-5-20250929",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello, Claude!"}]
  }'