Tin
Reference · The Anthropic API

On this page

The Anthropic API

import "std/anthropic": count tokens, list models, upload files, send Message Batches, and the handlers behind Claude's memory and text editor tools, in Tin.

Agents cover one conversation with Claude. The rest of the Anthropic API is a module written in Tin: std/anthropic. It calls the API with http(), so a program that uses it needs net, and what comes back is remote data, like a fetch() body. It is plain Tin over http() (tested with tin run). The tools Anthropic defines for agents (web search, code execution, memory, the text editor, computer use, the MCP connector) are agent fields: see Tools Anthropic defines.

import "std/anthropic"

import "std/anthropic"

fn main() {
  let c = anthropic.fromEnv()          // ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN, ANTHROPIC_BASE_URL
  print(c.countText("claude-haiku-4-5", "", "Hello, Claude").unwrap())
}

import "std/<name>" reads a module of the standard library, named after its last part. Its names are anthropic.x, so they never collide with yours. The command line resolves it (tin run, tin build, tin check); a one-file program in the playground cannot import yet.

The client

CallWhat it does
anthropic.client(key), anthropic.fromEnv()A Client: x-api-key (or Authorization: Bearer from authToken), anthropic-version: 2026-10-21 (the SDK's), 2 retries.
c.beta("files-api-2025-04-14"), c.header(name, value)The same client with one more anthropic-beta value or header on every call.
c.call(method, path, body, betas), c.send(…)Any endpoint: JSON in and out, or raw bytes. 429, 5xx and network failures are retried, waiting retry-after when the API says so.

A failed call is an ApiError: status (0 when there was no response), kind (the API's type, such as not_found_error or rate_limit_error), code (finer, such as file_not_downloadable), requestId (quote it to Anthropic) and a message written for a person; e.show() prints both.

Counting tokens

c.countTokens(req) sends any Messages request body (model, messages, system, tools, thinking…) to /v1/messages/count_tokens and returns the input tokens; max_tokens and stream are dropped, because the endpoint refuses them. c.countText(model, system, text) is the one-message case.

Models

for m in c.models().unwrap() {           // every page, newest first
  print("{m.id} {m.displayName} {m.lifecycle} {m.maxInputTokens}")
}
let h = c.model("claude-haiku-4-5").unwrap()   // an alias resolves to the dated id
print(h.supports("thinking"))                    // a path in capabilities, e.g. "server_tools.web_search"

Files

let f = c.uploadPath("report.pdf", "application/pdf").unwrap()   // needs fs.read too
let g = c.upload("notes.txt", "text/plain", bytes, 24h).unwrap()     // expires in 1 h … 90 days, or 0s: never
for x in c.files().unwrap() { print("{x.id} {x.filename} {x.sizeBytes}") }
let made = c.download("file_…")      // Bytes; only files the API made are downloadable
c.deleteFile(f.id)
Reader([documentFile(g.id), text("Summarise it.")])   // a file in a question (prelude parts)
Coder([containerUpload(f.id), text("Plot column 2.")])  // a file put into the code execution container
anthropic.fileDocument(f.id)         // the same as raw Json blocks, for batches and hand-built requests:
anthropic.fileImage(g.id)            //   a document, an image,
anthropic.containerUpload(f.id)      //   or a file for the code execution container

The upload is multipart/form-data built in Tin; only the last part of a file name is sent.

Message Batches

let b = c.createBatch([
  anthropic.batchText("q1", "claude-haiku-4-5", 200, "What is 2 + 2?"),
  anthropic.batchRequest("q2", params),           // any Messages request body
]).unwrap()                                       // custom_ids are checked before anything is sent
let done = c.waitBatch(b.id, 30s, 120).unwrap()   // poll until it ends (or 120 tries)
for r in c.batchResults(b.id).unwrap() {          // one result per request, in any order
  print("{r.customId} {r.kind} {r.text} {r.errorKind}")   // succeeded | errored | canceled | expired
}
c.cancelBatch(b.id)
c.deleteBatch(b.id)                               // and c.batch(id), c.batches()

Handlers for client tools

These answer the Anthropic-defined tools an agent names in clientTools:. The tool fn receives the API's input as JSON text and returns the text for Claude; an Err is sent with is_error.

/// Claude's memory, kept in memory.json.
tool fn remember(input: string): Result<string, string> => anthropic.memoryAt("memory.json", input)

/// View and edit files in ./workspace.
guarded tool fn edit(input: string): Result<string, string> => anthropic.textEditor("workspace").call(input)

/// The screen: screenshots go back to Claude as images.
guarded tool fn screen(input: string): string => anthropic.screenshot(readBytes("shot.png").unwrap())
  • Memory (memory_20250818): view, create, str_replace, insert, delete and rename, with the answers Anthropic documents. The files live in a map saved as one JSON file, so a path can only name something under /memories: .., backslashes, percent-encoding and control characters are refused, and the root cannot be deleted or renamed. anthropic.memoryStore() and MemoryStore.run(input) give the same store without a file.
  • Text editor (text_editor_20250728): view (files with line numbers and view_range, folders), create, str_replace (exactly one match) and insert, on real files under one root. An absolute path is read from the root. The check is on the path: a symlink inside the root that points out of it is followed, so give the editor a folder you own.
  • Bash and computer have no ready-made handler: what a command or a click does is the program's decision.

What is verified, and how

The parity matrix (docs/parity/anthropic.json, generated from the official SDK's types) lists 96 items for these features: 94 supported, 2 not applicable (each with the reason) and 0 missing. Each supported item says how it was checked:

  • Live (49): beta:advisor-tool-2026-03-01, beta:mcp-client-2025-11-20, block.in:advisor_tool_result, block.in:bash_code_execution_tool_result, block.in:container_upload, block.in:mcp_tool_result, block.in:mcp_tool_use, block.in:server_tool_use, block.in:tool_search_tool_result, block.in:web_fetch_tool_result, block.in:web_search_tool_result, block.out:advisor_tool_result, block.out:bash_code_execution_tool_result, block.out:mcp_tool_result, block.out:mcp_tool_use, block.out:server_tool_use, block.out:tool_search_tool_result, block.out:web_fetch_tool_result, block.out:web_search_tool_result, endpoint:files.delete, endpoint:files.download, endpoint:files.list, endpoint:files.retrieveMetadata, endpoint:files.upload, endpoint:messages.batches.cancel, endpoint:messages.batches.create, endpoint:messages.batches.delete, endpoint:messages.batches.list, endpoint:messages.batches.results, endpoint:messages.batches.retrieve, endpoint:messages.countTokens, endpoint:models.list, endpoint:models.retrieve, message:container, param:mcp_servers, advisor_20260301, bash_20250124, code_execution_20250825, code_execution_20260521, computer_20250124, mcp_toolset, memory_20250818, text_editor_20250728, tool_search_tool_bm25_20251119, tool_search_tool_regex_20251119, web_fetch_20250910, web_fetch_20260318, web_search_20250305, web_search_20260318. Memory, text editor, bash and computer ran on Claude Haiku 4.5 through Bedrock; everything else on api.anthropic.com with Claude Haiku 5.5, as Tin programs (tests/live/20–23-anthropic-*.tin, npm run test:live -- --target anthropic). Their tapes replay offline in npm test.
  • Fixture (44): against the request and response examples of Anthropic's API reference and tool pages. Most are other versions of a tool or the beta. spelling of a live endpoint. Where there is a reason it is not live:
    • beta:code-execution-2025-05-22: the beta that goes with code_execution_20250522 (not run, see there); beta headers reach the wire live (anthropic.betas.on_wire)
    • beta:computer-use-2024-10-22: added with the 2024-10-22 client tool versions (fixture); beta headers reach the wire live (anthropic.betas.on_wire)
    • beta:computer-use-2025-01-24: added with computer_20250124, which ran live on Bedrock (betas in the body there); on api.anthropic.com beta headers reach the wire live (anthropic.betas.on_wire)
    • beta:computer-use-2025-11-24: added with computer_20251124 (fixture); beta headers reach the wire live (anthropic.betas.on_wire)
    • beta:files-api-2025-04-14: Files is GA: the live Files calls need no beta; the header is a pass-through (beta headers reach the wire live)
    • beta:mcp-client-2026-09-15: pass-through via betas: [...] (beta headers reach the wire live); the connector itself is live-verified with mcp-client-2025-11-20
    • beta:message-batches-2024-09-24: Batches is GA: the live batch calls need no beta; the header is a pass-through (beta headers reach the wire live)
    • beta:token-counting-2024-11-01: count_tokens is GA: the live call needs no beta; the header is a pass-through (beta headers reach the wire live)
    • block.in:code_execution_tool_result: returned only by code_execution_20250522 (legacy, Python-only); the live versions 20250825/20260521 returned bash_code_execution_tool_result, which is live-verified on the same raw path
    • block.in:mcp_tool_listing: no live reply carried one (DeepWiki answered with mcp_tool_use/mcp_tool_result, both live); same generic raw path
    • block.in:text_editor_code_execution_tool_result: the model chose bash in every live code execution run; the block takes the same raw path as bash_code_execution_tool_result (live)
    • block.out:code_execution_tool_result: returned only by code_execution_20250522 (legacy, Python-only); the live versions 20250825/20260521 returned bash_code_execution_tool_result, which is live-verified on the same raw path
    • block.out:container_upload: replies seen live never carried a container_upload block (the API echoes none); the input block is live-verified (block.in:container_upload)
    • block.out:mcp_tool_listing: no live reply carried one (DeepWiki answered with mcp_tool_use/mcp_tool_result, both live); same generic raw path
    • block.out:text_editor_code_execution_tool_result: the model chose bash in every live code execution run; the block takes the same raw path as bash_code_execution_tool_result (live)
    • endpoint:beta.files.delete: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.files.download: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.files.list: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.files.retrieveMetadata: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.files.upload: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.cancel: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.create: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.delete: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.list: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.results: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.batches.retrieve: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.messages.countTokens: beta: route is the same endpoint with an anthropic-beta header; the GA call is live-verified
    • endpoint:beta.models.list: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • endpoint:beta.models.retrieve: same endpoints with an anthropic-beta header; the GA calls are live-verified
    • param:container: an explicit container: id is fixture-only; the response container going back on the next request (message:container) is live-verified
    • server_tool:bash_20241022: another version of a tool whose 20250124 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:browser_toolset_20260801: a toolset needs a real browser loop answering its member calls; the lowering is fixture-verified
    • server_tool:code_execution_20250522: another version of a tool whose 20250825 and 20260521 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:code_execution_20260120: another version of a tool whose 20250825 and 20260521 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:computer_20241022: another version of a tool whose 20250124 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:computer_20251124: another version of a tool whose 20250124 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:computer_toolset_20260801: a toolset needs a real screen loop answering its member calls; the single computer tool ran live (computer_20250124) and the toolset lowering is fixture-verified
    • server_tool:text_editor_20241022: another version of a tool whose 20250728 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:text_editor_20250124: another version of a tool whose 20250728 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:text_editor_20250429: another version of a tool whose 20250728 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:web_fetch_20260209: another version of a tool whose 20250910 and 20260318 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:web_fetch_20260309: another version of a tool whose 20250910 and 20260318 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • server_tool:web_search_20260209: another version of a tool whose 20250305 and 20260318 version is live-verified: the same lowering, only the type string differs; not run separately to keep spend small
    • stop_reason:pause_turn: not triggered live: one request with 12 web searches (100k tokens) and one with 14 sequential code executions (claude-haiku-5-5, 2026-11-16) both ended end_turn; the API decides when a turn pauses, so the continuation is fixture-verified (tests/pause-turn.test.ts, also on a native binary)

A container_upload block goes with a question as a part: A.ask([containerUpload(fileId), text("...")]).