Claude Seekho

Claude API Fundamentals

With the Claude API (Claude Developer Platform) you can build Claude into your own app. Everything goes through one main endpoint: POST /v1/messages (the Messages API). Tools, thinking and structured output are all features of this same endpoint.

5.1 Setup

  1. Create an account on platform.claude.com (Console) and add billing.
  2. Create an API key (inside a workspace). Do not hardcode the key in code — keep it in the ANTHROPIC_API_KEY environment variable.
  3. Install the SDK: Python pip install anthropic, TypeScript npm install @anthropic-ai/sdk, C# dotnet add package Anthropic. There are also official SDKs for Java, Go, Ruby and PHP.
Request–response cycle of the Messages API
  1. 1.Your app: model, system, messages, tools
  2. 2.POST /v1/messages: x-api-key + anthropic-version
  3. 3.Claude model: thinking + generation
  4. 4.Response: content blocks + stop_reason + usage

The API is stateless: every request must carry the full conversation history

5.2 First request (Python, C#, cURL)

import anthropic
client = anthropic.Anthropic()          # reads ANTHROPIC_API_KEY from the environment

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=16000,
    system="You are a helpful assistant that answers in Hinglish.",
    messages=[{"role": "user", "content": "What is an index in SQL?"}],
)
for block in response.content:          # content is a list (text, thinking, tool_use...)
    if block.type == "text":
        print(block.text)
print(response.stop_reason, response.usage.input_tokens, response.usage.output_tokens)
using Anthropic;
using Anthropic.Models.Messages;

AnthropicClient client = new();     // reads ANTHROPIC_API_KEY from the environment

var message = await client.Messages.Create(new MessageCreateParams
{
    Model = "claude-opus-5-5",
    MaxTokens = 1024,
    Messages = [ new UserMessageParam { Content = "Hello, Claude" } ],
});
Console.WriteLine(message);
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{"model": "claude-opus-5-5", "max_tokens": 1024,
       "messages": [{"role": "user", "content": "Hello, Claude"}]}'

5.3 Request parameters

ParameterPurposeNote
modelWhich modelExact ID, e.g. claude-opus-5-5
max_tokensUpper limit on outputIf set too low the answer is cut off midway (stop_reason: max_tokens)
messagesConversation: user / assistant turnsThe first message is the user's; you send the history yourself
systemSystem prompt (role, rules)A top-level field, not inside messages
toolsFunctions Claude can callSee section 7
thinkingThinking mode, e.g. {type: "adaptive"}Section 6
output_configeffort, structured formateffort goes here, not top-level
streamSend tokens as they arrive (SSE)Required for long outputs
stop_sequencesStop on custom stringsOn a match, stop_reason: stop_sequence
metadataInfo such as user_idFor abuse detection

5.4 Response and stop_reason

{
  "id": "msg_01...",
  "type": "message",
  "role": "assistant",
  "model": "claude-opus-5-5",
  "content": [{"type": "text", "text": "An index is a data structure..."}],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 25, "output_tokens": 180,
            "cache_read_input_tokens": 0, "cache_creation_input_tokens": 0}
}
stop_reasonMeaningWhat to do
end_turnClaude finished its answer naturallyNormal
max_tokensCut off at the limitRaise max_tokens or ask it to continue
stop_sequenceA custom stop string was hitThe stop_sequence field says which one
tool_useClaude wants to call a toolRun the tool, send back a tool_result
pause_turnA long server-tool task paused midwaySend the response back and let it continue
refusalThe safety classifier declinedCheck stop_details.category; you can use a fallback model
model_context_window_exceededThe context window is fullCompaction / context editing

5.5 Multi-turn conversation

history = []
def chat(user_text):
    history.append({"role": "user", "content": user_text})
    r = client.messages.create(model="claude-opus-5-5", max_tokens=4000, messages=history)
    history.append({"role": "assistant", "content": r.content})   # append the full content back
    return "".join(b.text for b in r.content if b.type == "text")

chat("My name is Rahul")
print(chat("What is my name?"))     # remembered because of the history

5.6 Streaming

In streaming, the response arrives in pieces over Server-Sent Events (SSE), so the user sees text immediately and long requests do not time out. Event order: message_start → content_block_start → content_block_delta (repeatedly) → content_block_stop → message_delta (stop_reason, usage) → message_stop. A ping can also arrive in between.

with client.messages.stream(
    model="claude-opus-5-5", max_tokens=64000,
    messages=[{"role": "user", "content": "Write a long article on microservices"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    final = stream.get_final_message()     # the complete message object

5.7 Errors and rate limits

HTTPTypeMeaning / Fix
400invalid_request_errorBad request (param, format). Do not retry, fix it
401authentication_errorAPI key wrong/missing
403permission_errorThe key has no permission for that resource
404not_found_errorModel/resource not found
413request_too_largeRequest is over the size limit
429rate_limit_errorLimit exceeded; retry after the retry-after header
500api_errorAnthropic-side error; retry
529overloaded_errorAPI overloaded; retry with backoff
  • There are three kinds of rate limits: RPM (requests/min), ITPM (input tokens/min), OTPM (output tokens/min). Limits grow as your usage tier goes up.
  • The SDK retries 2 times by default (408, 409, 429, 5xx) with exponential backoff. Default timeout is 10 minutes.
  • In error handling, catch from specific to general: RateLimitError → APIStatusError → APIConnectionError.