Skip to main content

Minimal request

{
  "model": "claude-sonnet-4-20250514",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": [{ "type": "text", "text": "Extract order fields and return JSON only." }]
    },
    {
      "role": "assistant",
      "content": [{ "type": "text", "text": "{" }]
    }
  ]
}

cURL example

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [{ "type": "text", "text": "Extract order fields and return JSON only." }]
      },
      {
        "role": "assistant",
        "content": [{ "type": "text", "text": "{" }]
      }
    ]
  }'

Python example

from anthropic import Anthropic

client = Anthropic(api_key="<ANTHROPIC_API_KEY>")

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [{"type": "text", "text": "Extract order fields and return JSON only."}],
        },
        {
            "role": "assistant",
            "content": [{"type": "text", "text": "{"}],
        },
    ],
)

print(response.content[0].text)

Node.js example

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const response = await client.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [{ type: "text", text: "Extract order fields and return JSON only." }]
    },
    {
      role: "assistant",
      content: [{ type: "text", text: "{" }]
    }
  ]
});

console.log(response.content[0].text);

Best practices

  • Follow Anthropic’s JSON-mode guidance: specify the target structure precisely
  • Prefill the assistant response when you need stricter output consistency
  • For larger schemas, add examples instead of relying on one-line instructions