Claude API

Anthropic's programming interface to the Claude models. Billed separately from a claude.ai subscription, with keys from the Claude Console.

Website ↗

The Safari Bookmark Sorter uses the Claude API with the Python SDK anthropic and the model claude-haiku-5-5.

Access

  • Keys come from the Claude Console at platform.claude.com under API Keys. The full key is shown only once, right after creating it; the list later shows a shortened form.
  • The account needs credit under Billing. A claude.ai subscription does not include API access.
  • I store the key in the macOS Keychain.
  • An error 401 "invalid bearer token" simply means the key is invalid. The SDK (version 2.8.0) sends every API key in that form. A common cause is a key copied from the shortened list view.

Reliable answers

  • Structured outputs: with output_config and a JSON schema as format, the API enforces the answer format, so broken JSON cannot occur.
  • Thinking block first: the answer can start with a thinking block. Take the first block of type text, not the first block.
  • Check stop_reason: max_tokens means the answer was cut off, refusal that the model declined.
  • Short IDs: the model gets short numbers instead of long UUIDs. That saves tokens, and it cannot garble them.
  • Batches: the bookmarks go to the API in batches of 40, so even a large collection fits into the answers.

More on this

Used in
  • Photo Sidecars with a Vision Model The script vision.ts of my old site: EXIF data and a vision model's alt text, titles and tags in a JSON file next to each photo, generated locally before the deploy.
  • Safari Bookmark Sorter Python script that sorts Safari bookmarks into folders with Claude Haiku and hands the result to Safari as an import file, so iCloud syncs it.
By
  • Anthropic American AI company, a public benefit corporation in San Francisco, that develops the Claude language models.
Mentioned
  • macOS Keychain Password store built into macOS. Scripts can keep API keys there instead of in files or the shell profile and read them with the security command.
  • Python General-purpose programming language with highly readable syntax. The standard for data analysis, automation and quick prototypes.
Tags

Show in the graph view →