Claude API Tutorial 2026: Build AI Apps with Anthropic Claude

Claude API Tutorial 2026: Build AI Apps with Anthropic Claude

The Claude API by Anthropic is one of the most capable AI APIs in 2026. Claude Sonnet 4 offers 200K context, vision, tool use, and prompt caching. This tutorial covers first API call through production AI features.

Setup

pip install anthropic
export ANTHROPIC_API_KEY=sk-ant-...your-key...

First API Call

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model='claude-sonnet-4-5',
    max_tokens=1024,
    messages=[
        {'role': 'user', 'content': 'Explain async/await in Python in 3 sentences.'}
    ]
)

print(message.content[0].text)

Multi-Turn Chat

client = anthropic.Anthropic()
system = 'You are a senior Python developer. Be concise. Use code examples.'
history = []

def chat(user_msg: str) -> str:
    history.append({'role': 'user', 'content': user_msg})
    resp = client.messages.create(
        model='claude-sonnet-4-5',
        max_tokens=2048,
        system=system,
        messages=history
    )
    reply = resp.content[0].text
    history.append({'role': 'assistant', 'content': reply})
    return reply

print(chat('How do I read a CSV file?'))
print(chat('Now filter rows where age > 25'))

Streaming

with client.messages.stream(
    model='claude-sonnet-4-5',
    max_tokens=1024,
    messages=[{'role': 'user', 'content': 'Write a Python quicksort'}]
) as stream:
    for text in stream.text_stream:
        print(text, end='', flush=True)

Tool Use (Function Calling)

tools = [{
    "name": "get_weather",
    "description": "Get current weather for a city",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"]
    }
}]

response = client.messages.create(
    model='claude-sonnet-4-5',
    max_tokens=1024,
    tools=tools,
    messages=[{'role': 'user', 'content': 'What is the weather in Tokyo?'}]
)

if response.stop_reason == 'tool_use':
    for block in response.content:
        if block.type == 'tool_use':
            print(f'Tool: {block.name}, Input: {block.input}')

Prompt Caching (90% Cost Reduction)

response = client.messages.create(
    model='claude-sonnet-4-5',
    max_tokens=1024,
    system=[{
        "type": "text",
        "text": very_long_system_prompt,
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[{'role': 'user', 'content': user_question}]
)
print(f'Cache read: {response.usage.cache_read_input_tokens}')

Vision: Analyze Images

import base64
with open('screenshot.png', 'rb') as f:
    img = base64.standard_b64encode(f.read()).decode()

response = client.messages.create(
    model='claude-sonnet-4-5',
    max_tokens=1024,
    messages=[{'role': 'user', 'content': [
        {'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/png', 'data': img}},
        {'type': 'text', 'text': 'Describe this UI and list any bugs.'}
    ]}]
)

Conclusion

Claude API is the fastest path to production AI. Start with messages, add streaming for UX, use tool use for data access, and enable prompt caching to cut costs 90%. Claude Sonnet 4 handles code, analysis, vision, and long documents better than any other model at its price in 2026.

MD Rafikul Islam

Written by

MD Rafikul Islam is a software developer and editor of TechPulse. He writes about developer tools, hardware, AI, and practical technology decisions. Some articles are based on cited documentation and analysis rather than hands-on testing; readers should check each article for sources and testing disclosures. Corrections are welcome at rony.yf25@gmail.com.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *