ai / claude

Preferred guidelines for how I use Claude via API. I have used Ruby or Go in practice, but examples here use raw HTTP/JSON.

Model selection

Always use structured outputs

Require structured outputs on every call (/v1/messages and batch requests).

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-haiku-4-5",
    "max_tokens": 512,
    "messages": [
      {
        "role": "user",
        "content": "Write a YC-style company headline for an AI email triage app."
      }
    ],
    "output_config": {
      "format": {
        "type": "json_schema",
        "schema": {
          "type": "object",
          "properties": {
            "headline": {
              "type": "string",
              "description": "A Y Combinator-style company headline, 80 characters or less."
            }
          },
          "required": ["headline"],
          "additionalProperties": false
        }
      }
    }
  }'

Use adaptive thinking

Enable adaptive thinking for complex Sonnet/Opus work:

"thinking": {"type": "adaptive"}

Do not use with Haiku.

Research mode

Add tools directly and pair web search with code execution:

{
  "model": "claude-opus-4-7",
  "thinking": {"type": "adaptive"},
  "tools": [
    {"type": "web_search_20260209", "name": "web_search"},
    {"type": "code_execution_20260120", "name": "code_execution"}
  ]
}

Use code execution to filter and aggregate retrieved data before it consumes context.

Prompt boundary rule

Use the system prompt only to separate instructions from untrusted input (scraped pages, user text, raw threads). Otherwise, keep instructions in the user prompt.

Hallucination guardrails

Apply Anthropic's guidance:

Handle "Insufficient data" before database writes.

Context window budgeting

Compute max prompt size:

(context_window - max_output_tokens - buffer) * 4 chars/token

Budget per model context window and output cap. Example defaults: 64k output for Haiku/Sonnet, 128k for Opus, and a 5k system-prompt buffer.

← All articles