All guides
Anthropic · Beginner

Anthropic Claude API - from the first token to tool use

Claude 4.5 Sonnet is a strong default for coding and agents. Here's how to connect it.

8 min readUpdated Jun 2026

1. Key

Open console.anthropic.com -> Settings -> API Keys -> Create Key. Add prepaid credit (minimum US$5).

2. First call

import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const res = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Refactor this code..." }],
});
console.log(res.content[0].type === "text" && res.content[0].text);

3. Uso de herramientas (llamadas a funciones)

const res = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 1024,
  tools: [{
    name: "get_weather",
    description: "Returns current weather",
    input_schema: { type: "object", properties: { city: { type: "string" } }, required: ["city"] },
  }],
  messages: [{ role: "user", content: "How is the weather in San Francisco?" }],
});

4. Prompt caching

Mark large blocks (system prompt, docs) with `cache_control: { type: "ephemeral" }`. Follow-up requests within about 5 minutes pay roughly 10% of the input price.

Best practices

  • Use XML tags in prompts (Claude is trained for that pattern)
  • Use Sonnet 4.5 for code and Haiku 4 for cheap classification
  • Turn on extended thinking only when the problem truly needs reasoning