🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

Tutorial Claude API 2026: Crie aplicativos de IA com Anthropic Claude

⏱️3 min read  ·  441 words
Claude API Tutorial 2026: Build AI Apps with Anthropic Claude

OCláudio APIda Anthropic é uma das APIs de IA mais capazes em 2026. Claude Sonnet 4 oferece contexto de 200K, visão, uso de ferramentas e cache imediato. Este tutorial aborda a primeira chamada de API por meio de recursos de IA de produção.

Configuração

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

Primeira chamada de API

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)

Bate-papo multivoltas

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'))

Transmissão

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)

Uso de ferramenta (chamada de função)

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}')

Cache imediato (redução de custos de 90%)

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}')

Visão: Analisar Imagens

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.'}
    ]}]
)

Conclusão

A API Claude é o caminho mais rápido para a IA de produção. Comece com mensagens, adicione streaming para UX, use ferramentas para acesso a dados e habilite o cache imediato para reduzir custos em 90%. Claude Sonnet 4 lida com código, análise, visão e documentos longos melhor do que qualquer outro modelo com seu preço em 2026.

✍️ Leave a Comment

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

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা