Skip to content
@deckyard/anthropic

anthropic cheatsheet

Every export of @deckyard/anthropic. Guides are listed in the introduction.

jennifer
import "@deckyard/anthropic/" as anthropic;

Requests, replies, blocks and tools are json.Value documents in the API's own shape. A refused request throws AnthropicError (kind anthropic) with the HTTP status, Anthropic's error type, the requestId, the wait the server asked for and its rate-limit headers; so does a request the deck refuses to send (status 0). A reply over the cap is kind "limit", a deadline that passed "timeout", a connection the server closed after part of its answer "closed"; a network failure is the bundled http's net.NetError (kind "net"), passed on unchanged; a cancelled task stays "cancelled". A c argument is a Client, a req a Messages request, a reply what chat returned.

Constants

ConstantValueMeaning
VERSION"0.1.0"this deck's version
DEFAULT_BASE_URL"https://api.anthropic.com"where the API is served
API_VERSION"2026-10-21"the anthropic-version a fresh client sends
DEFAULT_MODEL"claude-opus-5-5"the model the docs and examples use
DEFAULT_TIMEOUT_MS600000how long one attempt may take (10 minutes)
DEFAULT_RETRIES2retries of a 408, 409, 429, 5xx or a failed connection
MAX_RETRY_WAIT_MS60000the longest retry-after-ms / retry-after waited for
DEFAULT_MAX_BYTES67108864the largest reply read (64 MiB)
DEFAULT_MAX_TOOL_INPUT1048576the largest tool input (its JSON) the tool loop hands to a handler (1 MiB)
DEFAULT_MAX_TOKENS16000a max_tokens that suits a reply that is not streamed
BETA_FALLBACKS"server-side-fallback-2026-07-01"the beta for withFallbacks
MAX_BATCH_REQUESTS100000the most requests in one batch

The client

FunctionReturnsDoes
client(apiKey)Clienta client for the Claude API with an API key
withBaseUrl(c, baseUrl)Clientanother API root: a gateway, a proxy, a fake
withPlainHttp(c)Clientallow plain http to another machine (the key crosses in clear)
withTls(c, tls)Clientcertificate options (caCert, skipVerify)
withApiVersion(c, version)Clientanother anthropic-version, such as 2023-06-01
withBetas(c, betas)Clientthe anthropic-beta names sent with every request
withTimeout(c, ms)Clientthe deadline of one attempt, 1000 to 86400000 ms
withRetries(c, retries)Clienthow often a retryable failure is retried, 0 to 10
withMaxBytes(c, most)Clientthe largest reply, 65536 to 1073741824 bytes
withMaxToolInput(c, most)Clientthe largest tool input the loop hands to a handler, 1024 to 1073741824 bytes
call(c, method, path, body)json.Valueany other endpoint; body JSON null for none
models(c)list of json.Valueevery model the key may use, newest first
model(c, id)json.Valueone model: context window, output cap, capabilities

Client

FieldMeaning
apiKey as stringthe API key, sent as x-api-key
baseUrl as stringthe API root without a trailing slash
version as stringthe anthropic-version sent
betas as list of stringthe anthropic-beta names sent
tls as http.TlsOptionscertificate checks for https
allowPlainHttp as boolwhether plain http to another machine is allowed
timeoutMs as inthow long one attempt may take
retries as inthow often a retryable failure is retried
backoffMs as intthe first wait before a retry without retry-after, doubled each time
maxBytes as intthe largest reply read
maxToolInput as intthe largest tool input, in bytes of its JSON, the tool loop hands to a handler

AnthropicError

FieldMeaning
kind as stringalways "anthropic"
message as stringanthropic: <request>: <why> (HTTP <status> <type>, request <id>)
status as intthe HTTP status; 0 when no reply was read
type as stringAnthropic's error.type, such as rate_limit_error; "" without one
requestId as stringthe request-id header or the body's request_id
retryAfterMs as intthe wait the server asked for (retry-after-ms, retry-after seconds or HTTP date), at most 1e12; 0 when it named none
limits as map of string to stringits rate-limit headers, lower-cased, at most 32: anthropic-ratelimit-*, x-ratelimit-*, retry-after, retry-after-ms

Messages

FunctionReturnsDoes
newRequest(model)json.Valuea new request {"model", "max_tokens": 16000, "messages": []}
withMaxTokens(req, n)json.Valueanother max_tokens, 0 to 300000
withSystem(req, text)json.Valuethe system prompt as text
withSystemBlocks(req, blocks)json.Valuethe system prompt as text blocks (for cache_control)
addUserText(req, text)json.Valuea user turn of text appended
addUser(req, blocks)json.Valuea user turn of content blocks appended
addAssistant(req, content)json.Valuean assistant turn appended (a string or blocks)
addReply(req, reply)json.Valuea reply's whole content appended unchanged, thinking included
textBlock(text)json.Valuea text block
imageBlock(mediaType, data)json.Valuea base64 image block: jpeg, png, gif or webp
pdfBlock(data)json.Valuea base64 PDF document block
withCacheControl(v, ttl)json.Valuea cache breakpoint on a block or tool, or automatic caching on a request; ttl "", 5m or 1h
withThinking(req, display)json.Valueadaptive thinking; display "", summarized, omitted or updates
withEffort(req, effort)json.Valueoutput_config.effort: low to max
withFormat(req, schema)json.Valuestructured output: output_config.format with a JSON Schema
withFallbacks(req)json.Valueserver-side refusal fallbacks (needs BETA_FALLBACKS)
chat(c, req)json.Valuesend the request, return the reply
countTokens(c, req)intthe input tokens the request would take
text(reply)stringthe reply's text blocks joined
toolCalls(reply)list of ToolCallthe reply's tool_use blocks
stopReason(reply)stringend_turn, max_tokens, tool_use, pause_turn, refusal, compaction, ...
refusalCategory(reply)stringa refusal's category, such as cyber; "" otherwise
usage(reply)Usagethe reply's token counts

Usage

FieldMeaning
inputTokens as intevery input token: input_tokens + cache reads + cache writes
outputTokens as intoutput tokens, thinking included
totalTokens as intinputTokens + outputTokens (Anthropic sends no total)
cachedTokens as intthe part of inputTokens read from the prompt cache
reasoningTokens as intthe part of outputTokens spent thinking
cacheCreationInputTokens as intthe part of inputTokens written to the prompt cache

ToolCall

FieldMeaning
id as stringthe tool_use block's id, which its tool_result answers
name as stringthe tool's name
input as json.Valuethe input, an object ({} when the block has none)

Tools

FunctionReturnsDoes
tool(name, description, schema)json.Valuea tool definition
withStrict(tool)json.Valuethe tool with strict: true
addTool(req, tool)json.Valuea tool (or server tool) appended to tools
addTools(req, tools)json.Valueseveral tools appended, in order
toolResult(toolUseId, content, isError)json.Valuea tool_result block
toolText(s)json.Valuetext as a JSON string, for a handler's answer
toolError(message)json.Valuean error result for a handler's answer; the model can recover
runTools(c, req, handler, maxTurns)ToolRunthe tool loop, until the model stops for something else
runToolsWith(c, req, handler, context, maxTurns)ToolRunthe tool loop with a context passed to every handler call

A runTools handler is func(name as string, input as json.Value), a runToolsWith handler func(name as string, input as json.Value, context as json.Value); both return a json.Value. A handler that throws ends the loop with its error. A tool input over maxToolInput is answered with {"error": "the input is over N bytes; call the tool again with a smaller input"} (is_error) without calling the handler.

ToolRun

FieldMeaning
reply as json.Valuethe last reply
request as json.Valuethe conversation, the last reply and its tool results included
turns as inthow many requests were sent
calls as inthow many tool calls the handler ran
usage as Usagethe token counts of every reply, summed
stopped as string"" when the model finished, maxTurns when the loop stopped there

Message batches

FunctionReturnsDoes
batchRequest(customId, params)json.Valueone request of a batch
createBatch(c, requests)json.Valuecreate a batch
batch(c, id)json.Valuea batch and its processing_status
batches(c, limit, afterId)json.Valueone page of batches, newest first
cancelBatch(c, id)json.Valuecancel a batch in progress
deleteBatch(c, id)json.Valuedelete an ended batch
waitBatch(c, id, pollMs, timeoutMs)json.Valuepoll until the batch has ended
batchResults(c, id)list of json.Valuethe JSONL results, one per request, in any order