diff --git a/skills/claude-api/SKILL.md b/skills/claude-api/SKILL.md index 6ffb7621e..317b8b501 100644 --- a/skills/claude-api/SKILL.md +++ b/skills/claude-api/SKILL.md @@ -32,7 +32,7 @@ Never mix the two - don't reach for `requests`/`fetch` in a Python or TypeScript Unless the user requests otherwise: -For the Claude model version, please use Claude Opus 5, which you can access via the exact model string `claude-opus-5`. Please default to using adaptive thinking (`thinking: {type: "adaptive"}`) for anything remotely complicated. And finally, please default to streaming for any request that may involve long input, long output, or high `max_tokens` - it prevents hitting request timeouts. Use the SDK's `.get_final_message()` / `.finalMessage()` helper to get the complete response if you don't need to handle individual stream events +For the Claude model version, please use Claude Opus 5.5, which you can access via the exact model string `claude-opus-5-5`. Please default to using adaptive thinking (`thinking: {type: "adaptive"}`) for anything remotely complicated. And finally, please default to streaming for any request that may involve long input, long output, or high `max_tokens` - it prevents hitting request timeouts. Use the SDK's `.get_final_message()` / `.finalMessage()` helper to get the complete response if you don't need to handle individual stream events. When a streaming request defines user-defined (client) tools, set `eager_input_streaming: true` on each of those tools so large tool inputs (file contents, code, documents) stream as they are generated instead of arriving in one burst after the server finishes buffering them; the client then owns validation: the SDKs' tolerant parsers can return a silently truncated input instead of raising, so validate each parsed tool input against its schema before running it (the typed runner helpers such as `betaZodTool` / typed `@beta_tool` do this; `betaTool()` JSON-Schema tools and manual loops must validate themselves), treat a failure like invalid JSON (`INVALID_JSON` error `tool_result` when you hold the block, re-issue otherwise), check `max_tokens` / `refusal` stop reasons before running tools, and catch only the SDK's JSON error, never its typed API errors - pattern in `shared/tool-use-concepts.md` -> Eager input streaming. Leave it off for non-streaming requests, for server tools, and when the request goes through a proxy or an older Bedrock model deployment that rejects the field. ## Warning: API Drift - Your Training Prior May Be Stale @@ -40,8 +40,8 @@ Several common Claude API shapes changed in 2025-2026. If you recall a pattern f | Area | Stale prior | Current API | |---|---|---| -| Extended thinking | `thinking: {type: "enabled", budget_tokens: N}` | On Claude 4.6+ models: `thinking: {type: "adaptive"}`. `budget_tokens` is deprecated on Opus 4.6 / Sonnet 4.6 and **rejected with a 400** on Fable 5/5.1 / Sonnet 5 / Opus 5 / 4.8 / 4.7. Pre-4.6 models still use `budget_tokens`. | -| Web search / web fetch tool type | `web_search_20250305`, `web_fetch_20250910` | `web_search_20260209`, `web_fetch_20260209` (dynamic filtering) on Opus 5/4.8/4.7/4.6, Sonnet 5, and Sonnet 4.6. Older models keep the basic variants; on Vertex AI only basic `web_search_20250305` is available (web fetch is not on Vertex) - see the Server Tools QR below. | +| Extended thinking | `thinking: {type: "enabled", budget_tokens: N}` | On Claude 4.6+ models: `thinking: {type: "adaptive"}`. `budget_tokens` is deprecated on Opus 4.6 / Sonnet 4.6 and **rejected with a 400** on Fable 5/5.1 / Sonnet 5.5 / Sonnet 5 / Opus 5.5 / 5 / 4.8 / 4.7. Pre-4.6 models still use `budget_tokens`. | +| Web search / web fetch tool type | `web_search_20250305`, `web_fetch_20250910` | `web_search_20260209`, `web_fetch_20260209` (dynamic filtering) on Opus 5.5/5/4.8/4.7/4.6, Sonnet 5.5, Sonnet 5, and Sonnet 4.6. Older models keep the basic variants; on Vertex AI only basic `web_search_20250305` is available (web fetch is not on Vertex) - see the Server Tools QR below. | | PHP parameter names | snake_case wire names as named args (`max_tokens`) | Top-level named args are camelCase (`maxTokens`). Nested array keys vary by feature (e.g. `'taskBudget'`, `'skillID'`, `'mcp_server_name'`) - copy the exact key from the documented example; do not bulk-convert. | | Managed Agents credentials | Keep secrets host-side via custom tools (the only option before vaults shipped) | Vault `environment_variable` credentials - stored by Anthropic, substituted at egress, never visible in the sandbox (`shared/managed-agents-tools.md` -> Vaults). Host-side custom tools remain the fallback for self-hosted sandboxes. | | Files API / Skills | `client.beta.files.*` / `client.beta.skills.*` with beta `files-api-2025-04-14` / `skills-2025-10-02` | Out of beta: `client.files.*` / `client.skills.*`, no beta header. In current SDKs `client.beta.files` / `client.beta.skills` have breaking shape changes from previous versions, matching the stable namespaces - migrate per `shared/live-sources.md` -> Files API / Skills Guide. | @@ -57,9 +57,12 @@ If the User Request at the bottom of this prompt is a bare subcommand string (no | Subcommand | Action | |---|---| | `migrate` | Migrate existing Claude API code to a newer model. **Read `shared/model-migration.md` immediately** and follow it in order: Step 0 (confirm scope - ask which files/directories before any edit), Step 1 (classify each file), then the per-target breaking-changes section. Do not summarize the guide - execute it. If the user did not name a target model, ask which model to migrate to in the same turn as the scope question. After the per-target changes are applied, audit the in-scope prompt text, tool descriptions, and request code against `shared/prompt-audit.md` - prompting written for the source model is part of every migration, and it does not announce itself. | -| `prompt-audit` | Audit existing prompts, skills, and tool descriptions for dated patterns ("cruft") written for older models. **Read `shared/prompt-audit.md` immediately** and follow it in order: Step 0 (establish scope and target model from the request and the repository - state the assumptions in the report, do not stop to ask), inventory, provenance, then the pattern scan. Produce both deliverables in full - the audit report (findings with `file:line`, pattern, why it's obsolete for the target model, confidence) and a proposed diff - without pausing for confirmation; apply edits only if the request explicitly asked for them. Do not summarize the guide - execute it. | +| `prompt-audit` | Audit existing prompts, tool descriptions, skills, and agent configuration files (`CLAUDE.md`, rule files, commands, subagents) for dated patterns ("cruft"): text written for older models, and instructions the repository has outgrown or that contradict each other. **Read `shared/prompt-audit.md` immediately** and follow it in order: Step 0 (establish scope and target model from the request and the repository - state the assumptions in the report, do not stop to ask), inventory, provenance, then the pattern scan. Produce both deliverables in full - the audit report (findings with `file:line`, pattern, why it's obsolete, confidence) and a proposed diff - without pausing for confirmation; apply edits only if the request explicitly asked for them. Do not summarize the guide - execute it. | | `upgrade` | Upgrade the project's Anthropic SDK dependency across a major version - currently the Python SDK, `anthropic` 0.x -> 1.x. Trailing words may name the language and/or a scope (`upgrade python`, `upgrade python sdk src/`). **Read `python/claude-api/sdk-upgrade.md` immediately** and follow it in order: Step 0 (confirm scope, then establish the current and target versions - a published 1.x must exist before you write a pin), the Step 1 inventory, each numbered section, then verification and the report. Do not summarize the guide - execute it. If the detected or named language has no `sdk-upgrade.md` in this skill, say that no major-version upgrade guide is bundled for that SDK yet and point the user at that SDK's CHANGELOG (repositories in `shared/live-sources.md`); do not improvise one from the Python guide. This is not model migration - to move code to a newer Claude model, use `migrate`. | | `cost-optimize` | Reduce what existing Claude API code costs to run, without sacrificing output quality. **Read `shared/cost-optimization.md` immediately** and follow it in order: Step 0 (establish scope, quality bar, and baseline), the token profile - measured through the Usage and Cost Admin API when the user has an Admin API key, from the app's own `response.usage` logs when it has those (ask), or estimated from the code otherwise - then a savings-ranked shortlist of levers (quoted in dollars, % of bill, or relative buckets depending on which of those data sources you have), free wins (caching, input-token hygiene, loop hygiene, output-token hygiene, batch) before tradeoffs (budgets, effort, model choice, multi-model); any lever that earns a place becomes its own diff - proposed by default, applied and measured against the eval covering the traffic it touches when the user asks and approves - and "no changes recommended" is a valid outcome. Two standing rules: every run that exercises the model spends real money, so get the user's approval first; and when context for a lever is missing, work through it interactively with the user - this workflow is not expected to one-shot the audit. Do not summarize the guide - execute it; presenting the profile and the ranked plan to the user is part of executing it. | +| `build-eval` | Help the user build an eval set for their Claude-powered app. **Read `shared/evals/build-eval.md` immediately** and run its interview: Step 0 (what's being evaluated), Step 1 (source the prompts - existing eval / transcripts / synthesized), Step 2 (grading method), Step 3 (runnable script + measured cost). Get the user's explicit sign-off on the inputs, the grading method, and the cost before producing the eval. | +| `preserved-thinking-migration` | Make an existing integration compatible with preserved thinking - the check that keeps a thinking block valid only in the conversation that produced it. **Read `shared/preserved-thinking-migration.md` immediately** and follow it in order: Step 0 (scope, traffic classes, platform and model, enforcement status, quality bar, baseline), Step 0.5 (prove the check is running with the three-request self-test), Step 1 (capture request bodies, diff consecutive pairs with `shared/preserved-thinking-migration/prefix_diff.py`, scan the code for the causes, name each edit and whether it is deliberate), Step 2 (replay a test slice with `prefix_mismatch_behavior: "drop_block"` under the `thinking-binding-controls-2026-08-01` header, count new dropped blocks per conversation, read the diagnosis header when present), Step 3 (one cause per diff in order of reasoning lost - proposed by default, applied when the user asks - then re-measure, keep or revert; the three-arm protocol when an eval exists), the model-switch section (in `shared/preserved-thinking-migration/causes.md`, with the cause table and the keep list) when the harness routes between models, Step 4 (the break profile and the changes). Two standing rules: every replay spends real money, so get the user's approval for the measurement budget first; and "no changes recommended" - the slice replayed thinking and nothing was dropped - is a valid outcome. Causes that have an append-only form only under a newer beta (keep-tail and background compaction: `compact-2026-09-04`; same-name tool changes: `inline-tools-2026-09-15`) are, where that beta is not available, measured and decided, not rewritten. For the *why* (the three-step check, the append-only edit table) it chains to `shared/model-migration.md` -> Breaking change 3; do not summarize the guide - execute it. | +| `hillclimb` | Iteratively improve the user's app against an existing eval. **Read `shared/evals/eval-hillclimb.md` immediately** and follow it: Step 0 (confirm a runnable eval exists - if not, route to `build-eval`), Step 1 (what to change / what's off-limits), Step 2 (budget + stopping condition from measured per-run cost), get the plan approved, then the read->propose->apply->run->record loop with on-disk state and a train/validation/test split. | --- @@ -69,31 +72,31 @@ Before reading code examples, determine which language the user is working in (e 1. **Look at project files** to infer the language: - - `*.py`, `requirements.txt`, `pyproject.toml`, `setup.py`, `Pipfile` -> **Python** - read from `python/` - - `*.ts`, `*.tsx`, `package.json`, `tsconfig.json` -> **TypeScript** - read from `typescript/` - - `*.js`, `*.jsx` (no `.ts` files present) -> **TypeScript** - JS uses the same SDK, read from `typescript/` - - `*.java`, `pom.xml`, `build.gradle` -> **Java** - read from `java/` - - `*.kt`, `*.kts`, `build.gradle.kts` -> **Java** - Kotlin uses the Java SDK, read from `java/` - - `*.scala`, `build.sbt` -> **Java** - Scala uses the Java SDK, read from `java/` - - `*.go`, `go.mod` -> **Go** - read from `go/` - - `*.rb`, `Gemfile` -> **Ruby** - read from `ruby/` - - `*.cs`, `*.csproj` -> **C#** - read from `csharp/` - - `*.php`, `composer.json` -> **PHP** - read from `php/` + - `*.py`, `requirements.txt`, `pyproject.toml`, `setup.py`, `Pipfile` -> **Python** - read from `python/` + - `*.ts`, `*.tsx`, `package.json`, `tsconfig.json` -> **TypeScript** - read from `typescript/` + - `*.js`, `*.jsx` (no `.ts` files present) -> **TypeScript** - JS uses the same SDK, read from `typescript/` + - `*.java`, `pom.xml`, `build.gradle` -> **Java** - read from `java/` + - `*.kt`, `*.kts`, `build.gradle.kts` -> **Java** - Kotlin uses the Java SDK, read from `java/` + - `*.scala`, `build.sbt` -> **Java** - Scala uses the Java SDK, read from `java/` + - `*.go`, `go.mod` -> **Go** - read from `go/` + - `*.rb`, `Gemfile` -> **Ruby** - read from `ruby/` + - `*.cs`, `*.csproj` -> **C#** - read from `csharp/` + - `*.php`, `composer.json` -> **PHP** - read from `php/` 2. **If multiple languages detected** (e.g., both Python and TypeScript files): - - Check which language the user's current file or question relates to - - If still ambiguous, ask: "I detected both Python and TypeScript files. Which language are you using for the Claude API integration?" + - Check which language the user's current file or question relates to + - If still ambiguous, ask: "I detected both Python and TypeScript files. Which language are you using for the Claude API integration?" 3. **If language can't be inferred** (empty project, no source files, or unsupported language): - - Use AskUserQuestion with options: Python, TypeScript, Java, Go, Ruby, cURL/raw HTTP, C#, PHP - - If AskUserQuestion is unavailable, default to Python examples and note: "Showing Python examples. Let me know if you need a different language." + - Use AskUserQuestion with options: Python, TypeScript, Java, Go, Ruby, cURL/raw HTTP, C#, PHP + - If AskUserQuestion is unavailable, default to Python examples and note: "Showing Python examples. Let me know if you need a different language." 4. **If unsupported language detected** (Rust, Swift, C++, Elixir, etc.): - - Suggest cURL/raw HTTP examples from `curl/` and note that community SDKs may exist - - Offer to show Python or TypeScript examples as reference implementations + - Suggest cURL/raw HTTP examples from `curl/` and note that community SDKs may exist + - Offer to show Python or TypeScript examples as reference implementations 5. **If user needs cURL/raw HTTP examples**, read from `curl/`. @@ -119,6 +122,7 @@ Every SDK language above supports both the beta Tool Runner and Managed Agents ( | Persisted, versioned agent configs | Agent | **Managed Agents** | Agents are stored objects; sessions pin to a version | | Long-running multi-turn agent with file mounts | Agent | **Managed Agents** | Per-session containers, SSE event stream, Skills + MCP | | Agent that runs on a schedule (cron, "every night") | Agent | **Managed Agents** - scheduled deployments | Deployments fire sessions autonomously; no client-side scheduler | +| Agent work that must meet a quality bar ("until it's right") | Agent | **Managed Agents** - outcomes | A separate grader iterates the agent against your rubric until it passes | > **Note:** Managed Agents is the right choice when you want Anthropic to run the agent loop *and* host the container where tools execute - file ops, bash, code execution all run in the per-session workspace. If you want to host the compute yourself or run your own custom tool runtime, Claude API + tool use is the right choice - use the tool runner for the agentic loop - its per-turn hooks still give you approval gates, logging, error interception, and conditional execution (see `shared/tool-use-concepts.md`) - or the manual loop when you want to own the entire loop yourself. @@ -172,24 +176,26 @@ Everything goes through `POST /v1/messages`. Tools and output constraints are fe --- -## Current Models (cached: 2026-06-24) +## Current Models (cached: 2026-09-25) | Model | Model ID | Context | Input $/1M | Output $/1M | | ----------------- | ------------------- | -------------- | ---------- | ----------- | | Claude Fable 5.1 | `claude-fable-5-1` | 1M | $10.00 | $50.00 | | Claude Mythos 5.1 (Project Glasswing only) | `claude-mythos-5-1` | 1M | $10.00 | $50.00 | | Claude Fable 5 | `claude-fable-5` | 1M | $10.00 | $50.00 | +| Claude Opus 5.5 | `claude-opus-5-5` | 1M | $4.00 | $20.00 | | Claude Opus 5 | `claude-opus-5` | 1M | $5.00 | $25.00 | | Claude Opus 4.8 | `claude-opus-4-8` | 1M | $5.00 | $25.00 | | Claude Opus 4.7 | `claude-opus-4-7` | 1M | $5.00 | $25.00 | | Claude Opus 4.6 | `claude-opus-4-6` | 1M | $5.00 | $25.00 | +| Claude Sonnet 5.5 | `claude-sonnet-5-5` | 1M | $2.00 | $10.00 | | Claude Sonnet 5 | `claude-sonnet-5` | 1M | $2.00 | $10.00 | | Claude Sonnet 4.6 | `claude-sonnet-4-6` | 1M | $3.00 | $15.00 | | Claude Haiku 4.5 | `claude-haiku-4-5` | 200K | $1.00 | $5.00 | **Partner pricing:** The prices above are Anthropic first-party API rates - they also apply to Claude on Microsoft Foundry, which is billed through the Microsoft Marketplace at standard API rates. Claude on Amazon Bedrock and Vertex AI is partner-operated with separate pricing - see [Bedrock](https://aws.amazon.com/bedrock/pricing/) or [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/pricing#claude-models). For WebFetch, use the Pricing row in `shared/live-sources.md`. -**ALWAYS use `claude-opus-5` unless the user explicitly names a different model.** This is non-negotiable. Do not use `claude-sonnet-5`, `claude-sonnet-4-6`, or any other model unless the user literally says "use sonnet" or "use haiku". Never downgrade for cost - that's the user's decision, not yours. Use `claude-fable-5-1` only when the user explicitly asks for Claude Fable 5.1, "fable", or Anthropic's most capable model - it has different API behavior than the Opus family (see below) and pricing that exceeds Opus-tier. **Use only the exact model ID strings from the table - they are complete as-is; never append date suffixes** (`claude-sonnet-4-6`, never `claude-sonnet-4-6-20251114` or any other date-suffixed variant you might recall from training data). If the user requests an older model not in the table (e.g., "opus 4.5", "sonnet 3.7"), read `shared/models.md` for the exact ID - do not construct one yourself. +**ALWAYS use `claude-opus-5-5` unless the user explicitly names a different model.** This is non-negotiable. Do not use `claude-sonnet-5-5`, `claude-sonnet-5`, or any other model unless the user literally says "use sonnet" or "use haiku". Never downgrade for cost - that's the user's decision, not yours. A request that describes a Sonnet by attribute ("cheapest Sonnet", "cheaper Sonnet", "newest Sonnet", "latest Sonnet") resolves to `claude-sonnet-5-5`. Where a second, cheaper model is in play alongside the main one (worker or sub-agent threads, bulk extractors, LLM judges, the executor under an advisor) - because the user asked for one or a guide in this skill calls for it - or the user says "sonnet" or "haiku" without a version, that means the current generation from the table above (`claude-sonnet-5-5`, `claude-haiku-4-5`); previous-generation IDs such as `claude-sonnet-5` are only for users who name that version. Use `claude-fable-5-1` only when the user explicitly asks for Claude Fable 5.1, "fable", or Anthropic's most capable model - it has different API behavior than the Opus family (see below) and pricing that exceeds Opus-tier. **Use only the exact model ID strings from the table - they are complete as-is; never append date suffixes** (`claude-opus-5-5`, never `claude-opus-5-5-20260401` or any other date-suffixed variant you might recall from training data). If the user requests an older model not in the table (e.g., "opus 4.5", "sonnet 3.7"), read `shared/models.md` for the exact ID - do not construct one yourself. ### Claude Fable 5.1 (`claude-fable-5-1`) - most capable widely released model @@ -198,11 +204,19 @@ Claude Fable 5.1 is Anthropic's most capable widely released model, for the most - **Thinking is always on** - omit the `thinking` parameter entirely (or send `{type: "adaptive"}`). Any other explicit configuration is rejected: `{type: "disabled"}` and `{type: "enabled", budget_tokens: N}` both return a 400. Control depth with `output_config.effort` (supports `low` through `xhigh` and `max`). - **The raw chain of thought is never returned** - responses carry regular `thinking` blocks (not `redacted_thinking`): `display: "summarized"` returns a readable summary, `"omitted"` (the default) leaves the `thinking` field as an empty string. Replay rules: pass thinking blocks back unchanged on the same model; other models drop them silently (unbilled - nothing to strip; Claude Mythos 5.1 instead reads them); details in `shared/model-migration.md`. - **Tokenizer** - same tokenizer as Opus 4.8 (introduced with Opus 4.7). Token counts are roughly unchanged when migrating from Opus 4.7/4.8; per-token pricing differs. Coming from Opus 4.6, Sonnet, Haiku, or older, re-baseline with `count_tokens` (the Opus 4.7 tokenizer uses ~1×-1.35× as many tokens). -- **`refusal` stop reason - handle it, and opt into fallbacks by default** - safety classifiers may decline a request (HTTP 200, `stop_reason: "refusal"`, with a `stop_details` category); always check `stop_reason` before reading `content`. **When you write `claude-fable-5-1` or `claude-opus-5` code, include the server-side `fallbacks` parameter by default.** Simplest form: `betas: ["server-side-fallback-2026-07-01"]` + `fallbacks: "default"`, which routes by refusal category so you never maintain a model list. (The older array form - `betas: ["server-side-fallback-2026-06-01"]` + `fallbacks: [{"model": "claude-opus-4-8"}]` - still works; Claude API and Claude Platform on AWS - on Bedrock, Vertex and Foundry, use the SDKs' client-side `BetaRefusalFallbackMiddleware` + `BetaFallbackState`). Tell the user you've enabled it; drop it only if they decline. Full semantics (billing, mid-stream refusals, credit repricing) in `shared/model-migration.md` -> refusal section. **Per-language code examples in `{lang}/claude-api/README.md` § Refusal Fallbacks cover the array form only** - for the `"default"` mode, follow the raw-HTTP shape in `shared/model-migration.md` -> Migrating to Claude Opus 5 -> New API features and swap `fallbacks: [{...}]` for `fallbacks: "default"` plus the `-2026-07-01` header; the rest of the request is unchanged. +- **`refusal` stop reason - handle it, and opt into fallbacks by default** - safety classifiers may decline a request (HTTP 200, `stop_reason: "refusal"`, with a `stop_details` category); always check `stop_reason` before reading `content`. **When you write `claude-fable-5-1`, `claude-opus-5-5`, `claude-opus-5`, or `claude-sonnet-5-5` code, include the server-side `fallbacks` parameter by default** (for `claude-sonnet-5-5`, only the `"default"` form and only on the Claude API; on other platforms use the SDK middleware below, except when the request sends `between_tools`: only Claude Sonnet 5.5 accepts it and the middleware re-sends the same request body on the fallback model, so write the retry yourself and send it without `between_tools` - see `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5 -> Safeguards and fallback). Simplest form: `betas: ["server-side-fallback-2026-07-01"]` + `fallbacks: "default"`, which routes by refusal category so you never maintain a model list. (The older array form - `betas: ["server-side-fallback-2026-06-01"]` + `fallbacks: [{"model": "claude-opus-4-8"}]` - still works; Claude API and Claude Platform on AWS - on Bedrock, Vertex and Foundry, use the SDKs' client-side `BetaRefusalFallbackMiddleware` + `BetaFallbackState`). Tell the user you've enabled it; drop it only if they decline. Full semantics (billing, mid-stream refusals, credit repricing) in `shared/model-migration.md` -> refusal section. **Per-language code examples in `{lang}/claude-api/README.md` § Refusal Fallbacks cover the array form only** - for the `"default"` mode, follow the raw-HTTP shape in `shared/model-migration.md` -> Migrating to Claude Opus 5 -> New API features and swap `fallbacks: [{...}]` for `fallbacks: "default"` plus the `-2026-07-01` header; the rest of the request is unchanged. - **No assistant prefill** - same as the rest of the 4.6+ family. - **30-day data retention required** - Claude Fable 5.1 is not available under zero data retention unless expressly authorized by Anthropic; requests from an org whose retention configuration doesn't meet the requirement return `400 invalid_request_error`. - **Longer turns, different prompting** - single requests on hard tasks can run many minutes (plan timeouts/streaming/progress UX); effort sweeps should include low/medium for routine work; prompts written for prior models are often too prescriptive and reduce output quality. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 -> Behavioral shifts (prompt-tunable) for the recommended prompt snippets. -- **Successor to Claude Fable 5 (`claude-fable-5`, still served) in the same tier at the same per-token price.** Same surface as Claude Fable 5 with three breaking changes - forced tool use (`tool_choice` `any` / `tool`) returns a 400 (use `auto` + a prompt instruction, `strict: true` for schema-valid arguments, or structured outputs); thinking blocks are bound to the producing model (other models drop them, unbilled); and editing earlier turns invalidates thinking blocks ("preserved thinking"; new accounts created on/after 2026-08-31 get a 400 on edited history; later models enforce it for everyone - make every harness append-only and run the three-step check; the opt-in controls are per-platform, see `shared/platform-availability.md`) - plus per-message `effort` (beta `mid-conversation-output-config-2026-07-01`, also on Claude Opus 5), turn-scoped `clear_at: "next_user_message"` system messages (beta), `thinking.display: "updates"` progress notes (beta, all platforms), cache reads at $0.25/MTok (whether Claude Mythos 5.1 shares that rate is open at launch), and content provenance. Covered Model - ZDR orgs get `400 invalid_request_error` as on Claude Fable 5 (ZDR only if expressly authorized by Anthropic); no Priority Tier. Same tokenizer as Claude Fable 5. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5. +- **Successor to Claude Fable 5 (`claude-fable-5`, still served) in the same tier at the same per-token price.** Same surface as Claude Fable 5 with three breaking changes - forced tool use (`tool_choice` `any` / `tool`) returns a 400 (use `auto` + a prompt instruction, `strict: true` for schema-valid arguments, or structured outputs); thinking blocks are bound to the producing model (other models drop them, unbilled); and editing earlier turns invalidates thinking blocks ("preserved thinking"; new accounts created on/after 2026-08-31 get a 400 on edited history on every platform, and enforcement scope is decided per model, and Claude Mythos 5.1 doesn't run this check. Make every harness append-only and run the three-step check; the opt-in controls beta is on the Claude API, Claude Platform on AWS, Bedrock, and Vertex - Foundry unconfirmed, see `shared/platform-availability.md`) - plus per-message `effort` (beta `mid-conversation-output-config-2026-07-01`, also on Claude Opus 5 and Claude Opus 5.5), turn-scoped `clear_at: "next_user_message"` system messages (beta), `thinking.display: "updates"` progress notes (beta, all platforms), cache reads at $0.25/MTok, and content provenance. Covered Model - ZDR orgs get `400 invalid_request_error` as on Claude Fable 5 (ZDR only if expressly authorized by Anthropic); no Priority Tier. Same tokenizer as Claude Fable 5. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5. + +### Claude Opus 5.5 (`claude-opus-5-5`) - the current Opus and the default model + +Successor to Claude Opus 5 in the Opus line at a lower price ($4 / $20 per MTok, cache reads $0.20), same 1M context / 128K output / tokenizer / feature set. Four breaking changes for code running on Claude Opus 5: **thinking can't be disabled** (`{type: "disabled"}` and `budget_tokens` both 400 at every effort level - effort is the only control, and its **default is `medium`**, one level below Claude Opus 5's `high`, so set it explicitly); **forced `tool_choice` `any`/`tool` returns a 400** (use `auto` + `strict: true` and steer from the prompt, or structured outputs); **thinking blocks are tied to the model and the conversation** (preserved thinking: only Claude Fable 5.1 / Claude Mythos 5.1 on the Claude API read its blocks, so a fallback to Claude Opus 5 runs without them; accounts created on or after 2026-08-31 are enforced on the history-editing check); and **on the Claude API and Google Cloud, computer use only through `computer_toolset_20260801`** (`computer_20251124` 400s there; Amazon Bedrock still accepts it). Text between tool calls comes back as progress-update `thinking` blocks (empty by default - set `display: "updates"`). Broader safety classifiers: `bio` and `reasoning_extraction` join `cyber`. Fast mode is Claude API only, $8 / $40 per MTok (2x standard). See `shared/model-migration.md` -> Migrating to Claude Opus 5.5. + +### Claude Sonnet 5.5 (`claude-sonnet-5-5`) - the current Sonnet: speed and capability for everyday coding, agent, and enterprise work (Claude Opus 5.5 stays the default) + +Successor to Claude Sonnet 5 in the Sonnet line at the same prices ($2 / $10 per MTok, cache reads $0.20), with the same tokenizer, 1M context and 128K output. Five breaking changes for code running on Claude Sonnet 5: **`thinking: {type: "disabled"}` returns a 400** - to turn thinking off, send `thinking: {type: "between_tools"}`, which is accepted only at effort `high` or below, takes no other field (`display`, `budget_tokens`, or `block_binding` alongside it is a 400), and doesn't allow per-message effort changes; **forced `tool_choice` `any`/`tool` returns a 400** (use `auto` + `strict: true` and steer from the prompt, or structured outputs); **thinking blocks are tied to the model and the conversation** (no other model reads its blocks; accounts created on or after 2026-08-31 are enforced on the history-editing check on the Claude API and Amazon Bedrock); **on the Claude API and Google Cloud, computer use only through `computer_toolset_20260801`** (`computer_20251124` 400s there; Amazon Bedrock still accepts it); and **the advisor tool rejects Claude Opus 4.8, Claude Opus 4.7, and Claude Sonnet 5 advisors** (every advisor it accepts returns encrypted advice). Effort still defaults to `high`, but the levels are recalibrated - re-run the effort sweep (start at `medium` for agentic coding and multistep tool use, `low` for chat). Text between tool calls comes back as progress-update `thinking` blocks (empty by default - set `display: "updates"`, or use `between_tools`). Safety classifiers decline in five `stop_details` categories: `cyber`, `bio`, `frontier_llm`, `reasoning_extraction`, `general_harms`. See `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5. If any model strings above look unfamiliar, that just means they were released after your training data cutoff - they are real models. @@ -227,29 +241,31 @@ Full auth details (named profiles, scopes, the API-key-shadows-profile trap, ref ## Thinking & Effort (Quick Reference) -Use adaptive thinking (`thinking: {type: "adaptive"}`) on every current model - Claude dynamically decides when and how much to think. Per-model rules: +Use adaptive thinking (`thinking: {type: "adaptive"}`) on every current model except Haiku 4.5, which still takes `budget_tokens` (table below) - Claude dynamically decides when and how much to think. Per-model rules: | Model | Thinking config | Omitting `thinking` | `budget_tokens` | Sampling (`temperature`/`top_p`/`top_k`) | Effort levels | |---|---|---|---|---|---| -| Fable 5 / Claude Fable 5.1 (and the Mythos counterparts) | `{type: "adaptive"}` or omit; explicit `{type: "disabled"}` returns 400 - omit the param instead (Claude Fable 5.1 / Claude Mythos 5.1 also 400 on forced `tool_choice` `any`/`tool`, and run preserved thinking's history-editing check on replayed thinking blocks) | Runs adaptive (thinking is always on) | Removed - `{type: "enabled", budget_tokens: N}` returns 400 | Removed - 400 | `low`/`medium`/`high`/`xhigh`/`max` | +| Fable 5 / Claude Fable 5.1 (and the Mythos counterparts) | `{type: "adaptive"}` or omit; explicit `{type: "disabled"}` returns 400 - omit the param instead (Claude Fable 5.1 / Claude Mythos 5.1 also 400 on forced `tool_choice` `any`/`tool`; Claude Fable 5.1 runs preserved thinking's history-editing check on replayed thinking blocks, Claude Mythos 5.1 does not) | Runs adaptive (thinking is always on) | Removed - `{type: "enabled", budget_tokens: N}` returns 400 | Removed - 400 | `low`/`medium`/`high`/`xhigh`/`max` | +| Claude Opus 5.5 | `{type: "adaptive"}` or omit; `{type: "disabled"}` and `{type: "enabled", budget_tokens}` return 400 at **every** effort level - omit the param and lower effort instead (also 400s on forced `tool_choice` `any`/`tool`, and runs preserved thinking - see `shared/model-migration.md` -> Migrating to Claude Opus 5.5) | Runs **adaptive** | Removed - 400 | Removed - 400 | `low`/`medium`/`high`/`xhigh`/`max` - **default `medium`** (not `high`); per-message effort (beta) supported | | Claude Opus 5 | `{type: "adaptive"}` or omit; `{type: "disabled"}` accepted **only at effort `high` or below** - 400 at `xhigh`/`max`, and see the disabled-thinking pitfall below | Runs **adaptive** (thinking is on by default - unlike Opus 4.8/4.7) | Removed - 400 | Removed - 400 | `low`-`max` (all five) | | Opus 4.8 / 4.7 | `{type: "adaptive"}` is the only on-mode; `{type: "disabled"}` accepted | Runs **without** thinking - set `{type: "adaptive"}` explicitly | Removed - 400 | Removed - 400 | `low`/`medium`/`high`/`xhigh`/`max` | +| Claude Sonnet 5.5 | `{type: "adaptive"}` or omit; `{type: "disabled"}` returns 400 - to turn thinking off send `{type: "between_tools"}` (no other field; 400 at `xhigh`/`max`; effort can't change mid-conversation with it) (also 400s on forced `tool_choice` `any`/`tool`, and runs preserved thinking - see `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5) | Runs **adaptive** | Removed - 400 | Non-default values - 400 | `low`/`medium`/`high`/`xhigh`/`max` - default `high`, levels recalibrated from Claude Sonnet 5; per-message effort (beta) supported with thinking on | | Sonnet 5 | `{type: "adaptive"}` is the only on-mode; `{type: "disabled"}` accepted | Runs adaptive | Removed - 400 | Removed - 400 | `low`/`medium`/`high`/`xhigh`/`max` | | Opus 4.6 / Sonnet 4.6 | `{type: "adaptive"}` (recommended; auto-enables interleaved thinking, no beta header) | Set `{type: "adaptive"}` explicitly | Deprecated - do not use in new code; transitional escape hatch only (see below) | Allowed | `low`/`medium`/`high`/`max` (`xhigh` arrived with Opus 4.7) | -| Older (Sonnet 4.5, Haiku 4.5, ...) - only if explicitly requested | `{type: "enabled", budget_tokens: N}` | No thinking | Required for thinking; must be less than `max_tokens`, minimum 1024 - errors otherwise | Allowed | `effort` works on Opus 4.5 (`low`/`medium`/`high` only - no `xhigh`/`max`); errors on Sonnet 4.5 / Haiku 4.5 | +| Haiku 4.5; older models (Sonnet 4.5, ...) only if explicitly requested | `{type: "enabled", budget_tokens: N}` | No thinking | Required for thinking; must be less than `max_tokens`, minimum 1024 - errors otherwise | Allowed | `effort` works on Opus 4.5 (`low`/`medium`/`high` only - no `xhigh`/`max`); errors on Sonnet 4.5 / Haiku 4.5 | Opus 4.8 keeps the same request surface as 4.7 (no new breaking changes) - see `shared/model-migration.md` -> Migrating to Opus 4.8 for the behavioral re-tuning, and -> Migrating to Opus 4.7 for the full breaking-change list when coming from 4.6 or earlier. With `thinking` disabled, Opus 4.8 may write longer reasoning into the visible response - leave adaptive thinking on, or add a final-answer-only instruction (see the migration guide). -- **Effort (GA, no beta header):** `output_config: {effort: "low"|"medium"|"high"|"xhigh"|"max"}` - inside `output_config`, not top-level; default `high` (equivalent to omitting it). Controls thinking depth and overall token spend; combine with adaptive thinking for the best cost-quality tradeoffs. `xhigh` (added on Opus 4.7, between `high` and `max`) is the best setting for most coding and agentic use cases on Fable 5 / Opus 4.7/4.8 / Sonnet 5, and the default in Claude Code; effort matters more on those models than on any prior model in their tier - re-tune it when migrating, and run long-horizon/agentic tasks at `high`/`xhigh` with the full task spec given up front. Use a minimum of `high` for intelligence-sensitive work, `max` when correctness matters more than cost, and `low` for subagents or simple tasks - lower effort means fewer and more-consolidated tool calls, less preamble, and terser confirmations (`high` is often the sweet spot balancing quality and token efficiency). -- **Choosing an effort level (cost tuning):** Effort is the first quality-trading lever, after the free wins (caching first) - it trades thoroughness against token spend within one model, and the top of the range earns its cost only on hard problems (raise to `max` only when measurement shows headroom at the level below). Which workloads repay higher effort is a property of the workload: coding and long-horizon agentic work respond strongly; chat, classification, and high-volume or latency-sensitive routes often don't and do well at `low`, with `medium` as the cost-saving step-down where quality holds (the per-level defaults above cover the rest). Measure on a sample of real requests before raising a default, and tune per route rather than globally. Before building a multi-model cost cascade, measure the simpler alternative first - the most capable model at lower effort on the same tasks: lower effort on the newest models often matches or exceeds prior-generation performance at high effort (on Fable 5, lower effort often exceeds `xhigh` on prior models), and one model means one cache namespace (caches are model-scoped, so a cascade forfeits cache reuse across its models; a mid-conversation top-level `effort` change still invalidates the messages cache, though the per-message effort system message avoids that on Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5 - `shared/prompt-caching.md` § Invalidation hierarchy). Judge cost per completed task, not per request - a cheaper request that needs more turns or retries to finish the job isn't cheaper. For the measured effort/cost tradeoffs by workload and the full lever order, `shared/cost-optimization.md` § 2.6. -- **Thinking display - `"omitted"` by default on Fable 5 / Claude Fable 5.1 / Mythos 5 / Claude Mythos 5.1 / Opus 5 / 4.8 / 4.7 / Sonnet 5:** `display: "summarized"` returns a readable summary of the reasoning; `"omitted"` (the default on all eight - a silent change from Opus 4.6 and Sonnet 4.6, where it was `"summarized"`) streams `thinking` blocks with empty text. `display` controls visibility only - thinking happens and is billed the same under every setting; the raw chain of thought is never exposed on any model. If you stream reasoning to users, the default looks like a long pause before output - set `thinking: {type: "adaptive", display: "summarized"}` explicitly. (Independent of display, echo thinking blocks back unchanged when continuing on the same model; other models silently ignore them (Claude Fable 5.1 / Claude Mythos 5.1 read them) - see the migration guide.) On Claude Fable 5.1 / Claude Mythos 5.1 / Claude Fable 5, `display: "updates"` (beta `thinking-display-updates-2026-08-18`, every platform) hides reasoning like `"omitted"` but returns the model's between-tool-call progress notes as short `thinking` block summaries - see `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features. -- **When the user asks for "extended thinking", a "thinking budget", or `budget_tokens`:** always use Fable 5/5.1, Opus 5, 4.8, 4.7, or 4.6 with `thinking: {type: "adaptive"}` - the fixed thinking-token-budget concept is deprecated and adaptive thinking replaces it. Do NOT use `budget_tokens` for new 4.6/4.7/4.8 code and do NOT switch to an older model just because the user mentions it. *Gradual-migration carve-out:* `budget_tokens` is still functional on Opus 4.6 and Sonnet 4.6 only, as a transitional escape hatch for existing code that needs a hard token ceiling before you've tuned `effort` - see `shared/model-migration.md` -> Transitional escape hatch. It is fully removed on Fable 5/5.1, Opus 5/4.7/4.8, and Sonnet 5. +- **Effort (GA, no beta header):** `output_config: {effort: "low"|"medium"|"high"|"xhigh"|"max"}` - inside `output_config`, not top-level; default `high` (equivalent to omitting it) on every current model except Claude Opus 5.5, whose default is `medium` (thinking table above) - set it explicitly there. Controls thinking depth and overall token spend; combine with adaptive thinking for the best cost-quality tradeoffs. `xhigh` (added on Opus 4.7, between `high` and `max`) is the best setting for most coding and agentic use cases on Fable 5 / Opus 4.7/4.8 / Sonnet 5, and the default in Claude Code; effort matters more on those models than on any prior model in their tier - re-tune it when migrating, and run long-horizon/agentic tasks at `high`/`xhigh` with the full task spec given up front. Use a minimum of `high` for intelligence-sensitive work, `max` when correctness matters more than cost, and `low` for subagents or simple tasks - lower effort means fewer and more-consolidated tool calls, less preamble, and terser confirmations (`high` is often the sweet spot balancing quality and token efficiency). +- **Choosing an effort level (cost tuning):** Effort is the first quality-trading lever, after the free wins (caching first) - it trades thoroughness against token spend within one model, and the top of the range earns its cost only on hard problems (raise to `max` only when measurement shows headroom at the level below). Which workloads repay higher effort is a property of the workload: coding and long-horizon agentic work respond strongly; chat, classification, and high-volume or latency-sensitive routes often don't and do well at `low`, with `medium` as the cost-saving step-down where quality holds (the per-level defaults above cover the rest). Measure on a sample of real requests before raising a default, and tune per route rather than globally. Before building a multi-model cost cascade, measure the simpler alternative first - the most capable model at lower effort on the same tasks: lower effort on the newest models often matches or exceeds prior-generation performance at high effort (on Fable 5, lower effort often exceeds `xhigh` on prior models), and one model means one cache namespace (caches are model-scoped, so a cascade forfeits cache reuse across its models; a mid-conversation top-level `effort` change still invalidates the messages cache, though the per-message effort system message avoids that on Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Opus 5 / Claude Sonnet 5.5 (with adaptive thinking) - `shared/prompt-caching.md` § Invalidation hierarchy). Judge cost per completed task, not per request - a cheaper request that needs more turns or retries to finish the job isn't cheaper. For the measured effort/cost tradeoffs by workload and the full lever order, `shared/cost-optimization.md` § 2.6. +- **Thinking display - `"omitted"` by default on Fable 5 / Claude Fable 5.1 / Mythos 5 / Claude Mythos 5.1 / Opus 5.5 / 5 / 4.8 / 4.7 / Sonnet 5 / Claude Sonnet 5.5:** `display: "summarized"` returns a readable summary of the reasoning; `"omitted"` (the default on all ten - a silent change from Opus 4.6 and Sonnet 4.6, where it was `"summarized"`) streams `thinking` blocks with empty text. `display` controls visibility only - thinking happens and is billed the same under every setting; the raw chain of thought is never exposed on any model. If you stream reasoning to users, the default looks like a long pause before output - set `thinking: {type: "adaptive", display: "summarized"}` explicitly. (Independent of display, echo thinking blocks back unchanged when continuing on the same model; other models silently ignore them (Claude Fable 5.1 / Claude Mythos 5.1 read them, and Claude Sonnet 5.5 reads Claude Sonnet 5, Opus 4.8, Haiku 4.5, and earlier models' blocks) - see the migration guide.) On Claude Fable 5.1 / Claude Mythos 5.1 / Claude Fable 5 / Claude Opus 5.5 / Claude Sonnet 5.5, `display: "updates"` (beta `thinking-display-updates-2026-08-18`, every platform) hides reasoning like `"omitted"` but returns the model's between-tool-call progress notes as short `thinking` block summaries - see `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features. +- **When the user asks for "extended thinking", a "thinking budget", or `budget_tokens`:** always use Fable 5/5.1, Opus 5.5, 5, 4.8, 4.7, or 4.6 with `thinking: {type: "adaptive"}` - the fixed thinking-token-budget concept is deprecated and adaptive thinking replaces it. Do NOT use `budget_tokens` for new 4.6/4.7/4.8 code and do NOT switch to an older model just because the user mentions it. *Gradual-migration carve-out:* `budget_tokens` is still functional on Opus 4.6 and Sonnet 4.6 only, as a transitional escape hatch for existing code that needs a hard token ceiling before you've tuned `effort` - see `shared/model-migration.md` -> Transitional escape hatch. It is fully removed on Fable 5/5.1, Opus 5.5/5/4.7/4.8, and Sonnet 5. --- ## Compaction (Quick Reference) -**Beta, Fable 5/5.1, Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, Sonnet 5, and Sonnet 4.6.** For long-running conversations that may exceed the 1M context window, enable server-side compaction. The API automatically summarizes earlier context when it approaches the trigger threshold (default: 150K tokens). Requires beta header `compact-2026-01-12`. +**Beta, Fable 5/5.1, Opus 5.5, Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, Sonnet 5.5, Sonnet 5, and Sonnet 4.6.** For long-running conversations that may exceed the 1M context window, enable server-side compaction. The API automatically summarizes earlier context when it approaches the trigger threshold (default: 150K tokens). Requires beta header `compact-2026-01-12`. **Critical:** Append `response.content` (not just the text) back to your messages on every turn. Compaction blocks in the response must be preserved - the API uses them to replace the compacted history on the next request. Extracting only the text string and appending that will silently lose the compaction state. @@ -261,7 +277,7 @@ See `{lang}/claude-api/README.md` (Compaction section) for code examples. Full d **Prefix match.** Any byte change anywhere in the prefix invalidates everything after it. Render order is `tools` -> `system` -> `messages`. Keep stable content first (frozen system prompt, deterministic tool list), put volatile content (timestamps, per-request IDs, varying questions) after the last `cache_control` breakpoint. -**Mid-conversation operator instructions** (Claude Opus 5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, Claude Mythos 5.1; not Claude Sonnet 5; no beta header): append `{"role": "system", ...}` to `messages[]` instead of editing top-level `system`. Preserves the cached history prefix and is the prompt-injection-safe operator channel. See `shared/prompt-caching.md` § Mid-conversation system messages. +**Mid-conversation operator instructions** (Claude Opus 5, Claude Opus 5.5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, Claude Mythos 5.1, Claude Sonnet 5.5; not Claude Sonnet 5; no beta header): append `{"role": "system", ...}` to `messages[]` instead of editing top-level `system`. Preserves the cached history prefix and is the prompt-injection-safe operator channel. See `shared/prompt-caching.md` § Mid-conversation system messages. **Top-level auto-caching** (`cache_control: {type: "ephemeral"}` on `messages.create()`) is the simplest option when you don't need fine-grained placement. Max 4 breakpoints per request. Minimum cacheable prefix is model-dependent (512-4096 tokens - see `shared/prompt-caching.md` § API reference) - shorter prefixes silently won't cache. @@ -273,11 +289,11 @@ For placement patterns, architectural guidance, and the silent-invalidator audit ## Fast Mode (Quick Reference) -**Research preview, Claude Opus 5 / Opus 4.8 only** - Claude API and Managed Agents, not Bedrock / Google Cloud / Foundry. Opus 4.7 fast mode has been removed: `speed: "fast"` on 4.7 returns an error. Fast mode on Claude Opus 5 is priced at $10 / $50 per MTok. Fast mode runs the same model at up to 2.5x higher output tokens per second, at premium pricing. Three things are required on every request: use the **beta** messages endpoint (`client.beta.messages....`), pass the beta flag `fast-mode-2026-02-01`, and set `speed: "fast"` as a top-level request parameter (not a header, not in `extra_body`). +**Research preview, Claude Opus 5 / Claude Opus 5.5 / Opus 4.8 only** - Claude API and Managed Agents, not Bedrock / Google Cloud / Foundry. Opus 4.7 fast mode has been removed: `speed: "fast"` on 4.7 returns an error. Fast mode on Claude Opus 5 is priced at $10 / $50 per MTok; on Claude Opus 5.5, $8 / $40. Fast mode runs the same model at up to 2.5x higher output tokens per second, at premium pricing. Three things are required on every request: use the **beta** messages endpoint (`client.beta.messages....`), pass the beta flag `fast-mode-2026-02-01`, and set `speed: "fast"` as a top-level request parameter (not a header, not in `extra_body`). ```python client.beta.messages.create( - model="claude-opus-5", max_tokens=4096, + model="claude-opus-5-5", max_tokens=4096, speed="fast", betas=["fast-mode-2026-02-01"], messages=[...], ) @@ -295,17 +311,17 @@ client.beta.messages.create( `response.usage.speed` reports which speed was used. Fast mode has its own rate limit separate from standard Opus; on 429, either retry after the `retry-after` delay or drop `speed` and fall back to standard (note: switching speed invalidates prompt cache). Not available with Batch API, Priority Tier, Claude Platform on AWS, or third-party platforms. -**Priority Tier is not supported on every current model.** It is supported on Claude Fable 5, Opus 4.8, and the older current models, but Claude Opus 5, Claude Sonnet 5, Claude Fable 5.1, Claude Mythos 5.1, Claude Mythos 5, and Mythos Preview are excluded - a Priority Tier request naming one of them fails validation. +**Priority Tier is not supported on every current model.** It is supported on Claude Fable 5, Opus 4.8, and the older current models, but Claude Opus 5.5, Claude Opus 5, Claude Sonnet 5, Claude Sonnet 5.5, Claude Fable 5.1, Claude Mythos 5.1, Claude Mythos 5, and Mythos Preview are excluded - a Priority Tier request naming one of them fails validation. --- ## Task Budgets (Quick Reference) -**Beta, Claude Opus 5 / Fable 5 / Claude Fable 5.1 (confirm at launch) / Sonnet 5 / Opus 4.8 / 4.7.** A task budget gives Claude a token ceiling for an agentic loop so it paces itself and finishes gracefully instead of being cut off - distinct from `max_tokens`, which is an enforced per-response ceiling the model is not aware of. Minimum `total`: 20,000. Set `task_budget` inside `output_config` on `client.beta.messages.stream(...)` with beta flag `task-budgets-2026-03-13` - use streaming so the large `max_tokens` doesn't hit HTTP timeouts (full details: `shared/model-migration.md` -> Task Budgets): +**Beta, Claude Opus 5 / Claude Opus 5.5 / Fable 5 / Claude Fable 5.1 (confirm at launch) / Claude Sonnet 5.5 / Opus 4.8 / 4.7 (not Claude Sonnet 5).** A task budget gives Claude a token ceiling for an agentic loop so it paces itself and finishes gracefully instead of being cut off - distinct from `max_tokens`, which is an enforced per-response ceiling the model is not aware of. Minimum `total`: 20,000. Set `task_budget` inside `output_config` on `client.beta.messages.stream(...)` with beta flag `task-budgets-2026-03-13` - use streaming so the large `max_tokens` doesn't hit HTTP timeouts (full details: `shared/model-migration.md` -> Task Budgets): ```python with client.beta.messages.stream( - model="claude-opus-5", max_tokens=128000, + model="claude-opus-5-5", max_tokens=128000, output_config={"effort": "high", "task_budget": {"type": "tokens", "total": 64000}}, betas=["task-budgets-2026-03-13"], messages=[...], tools=[...], @@ -325,7 +341,7 @@ When targeting Claude on a third-party platform, use that platform's dedicated c ### Amazon Bedrock -Use the **Mantle** client (Messages-API Bedrock endpoint). Bedrock model IDs take an `anthropic.` prefix (e.g. `"anthropic.claude-opus-5"`). Region is required. +Use the **Mantle** client (Messages-API Bedrock endpoint). Bedrock model IDs take an `anthropic.` prefix (e.g. `"anthropic.claude-opus-5-5"`). Region is required. | Language | Client | |---|---| @@ -353,7 +369,7 @@ The Go and Ruby SDKs do not currently support Foundry. For Ruby, use the standar ### Google Cloud Vertex AI -Two required constructor args: GCP `project_id` and `region`. Vertex model IDs take **no prefix** - current-generation models (Opus 4.8/4.7/4.6, Sonnet 5, Sonnet 4.6) use the bare first-party ID (e.g. `"claude-opus-5"`); dated-snapshot models use an `@` version separator (e.g. `claude-opus-4-5@20251101`, **not** `claude-opus-4-5-20251101`). Auth is GCP ADC (`gcloud auth application-default login`); no Anthropic API key. `region` can be `"global"` (recommended), a multi-region (`"us"`/`"eu"`), or a specific region. After construction, use the same `messages.create` / `.stream` surface. +Two required constructor args: GCP `project_id` and `region`. Vertex model IDs take **no prefix** - current-generation models (Opus 5.5/5/4.8/4.7/4.6, Sonnet 5.5, Sonnet 5, Sonnet 4.6) use the bare first-party ID (e.g. `"claude-opus-5-5"`); dated-snapshot models use an `@` version separator (e.g. `claude-opus-4-5@20251101`, **not** `claude-opus-4-5-20251101`). Auth is GCP ADC (`gcloud auth application-default login`); no Anthropic API key. `region` can be `"global"` (recommended), a multi-region (`"us"`/`"eu"`), or a specific region. After construction, use the same `messages.create` / `.stream` surface. | Language | Client | |---|---| @@ -373,7 +389,7 @@ Two required constructor args: GCP `project_id` and `region`. Vertex model IDs t ```python client.beta.messages.create( - model="claude-opus-5", max_tokens=4096, + model="claude-opus-5-5", max_tokens=4096, betas=["context-management-2025-06-27"], context_management={"edits": [{"type": "clear_tool_uses_20250919"}]}, tools=[...], messages=[...], @@ -386,7 +402,7 @@ Strategy types: `clear_tool_uses_20250919` (clears old tool results; optional `c ## Mid-Conversation System Messages (Quick Reference) -**Claude Opus 5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, and Claude Mythos 5.1; not Claude Sonnet 5; no beta header.** Append `{"role": "system", "content": "..."}` to the `messages` array (not the top-level `system` field) to add an operator instruction mid-conversation without invalidating the cached prefix. Use the regular `client.messages.create` - there is no beta. A mid-conversation system message must follow a `user` message (or an `assistant` message ending in server-tool use), and must be either the last entry in `messages` or be followed by an `assistant` turn - it cannot be `messages[0]`. Availability: `shared/platform-availability.md`. See `shared/prompt-caching.md` § Mid-conversation system messages. A beta extension shipped with Claude Fable 5.1: `output_config: {effort: ...}` with `content: []` changes effort from that point on without a cache reset (beta `mid-conversation-output-config-2026-07-01`; Claude Fable 5.1, Claude Mythos 5.1, Claude Opus 5; Claude API). An effort-only message (empty `content`) is exempt from the placement rules above - it can sit anywhere in `messages`, including first or between an assistant turn and the next user turn; the rules apply to text and `clear_at` messages. For a per-turn reminder, give the message `clear_at: "next_user_message"` (beta `mid-conversation-system-clear-at-2026-08-21`): it renders for one turn, then stays in the transcript cleared - never delete earlier copies (on Claude Fable 5.1 deleting one invalidates later thinking blocks); without the beta, a text block after the tool results, earlier copies kept. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features. +**Claude Opus 5, Claude Opus 5.5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, Claude Mythos 5.1, and Claude Sonnet 5.5; not Claude Sonnet 5; no beta header.** Append `{"role": "system", "content": "..."}` to the `messages` array (not the top-level `system` field) to add an operator instruction mid-conversation without invalidating the cached prefix. Use the regular `client.messages.create` - there is no beta. A mid-conversation system message must follow a `user` message (or an `assistant` message ending in server-tool use), and must be either the last entry in `messages` or be followed by an `assistant` turn - it cannot be `messages[0]`. Availability: `shared/platform-availability.md`. See `shared/prompt-caching.md` § Mid-conversation system messages. A beta extension shipped with Claude Fable 5.1: `output_config: {effort: ...}` with `content: []` changes effort from that point on without a cache reset (beta `mid-conversation-output-config-2026-07-01`; Claude Fable 5.1, Claude Mythos 5.1, Claude Opus 5.5, Claude Opus 5, and Claude Sonnet 5.5 with thinking on; Claude API and Google Cloud). An effort-only message (empty `content`) is exempt from the placement rules above - it can sit anywhere in `messages`, including first or between an assistant turn and the next user turn; the rules apply to text and `clear_at` messages. For a per-turn reminder, give the message `clear_at: "next_user_message"` (beta `mid-conversation-system-clear-at-2026-08-21`): it renders for one turn, then stays in the transcript cleared - never delete earlier copies (on Claude Fable 5.1, Claude Opus 5.5, and Claude Sonnet 5.5 deleting one invalidates later thinking blocks); without the beta, a text block after the tool results, earlier copies kept. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features. --- @@ -398,7 +414,7 @@ Availability: `shared/platform-availability.md`. For agents on Bedrock / Vertex **Mandatory flow:** Agent (once) -> Session (every run). `model`/`system`/`tools` live on the agent, never the session. See `shared/managed-agents-overview.md` for the full reading guide, beta headers, and pitfalls. -**Beta headers:** `managed-agents-2026-04-01` - the SDK sets this automatically for all `client.beta.{agents,environments,sessions,vaults,memory_stores,deployments,deployment_runs}.*` calls. Files API and Skills API are out of beta - no beta header needed (see the API Drift table above for the migration guides). +**Beta headers:** `managed-agents-2026-04-01` - the SDK sets this automatically for all `client.beta.{agents,environments,sessions,vaults,deployments,deployment_runs}.*` calls. Memory stores use `agent-memory-2026-07-22` instead, which the SDK sets on `client.beta.memory_stores.*` calls; sending both headers on a memory store request returns a 400. Files API and Skills API are out of beta - no beta header needed (see the API Drift table above for the migration guides). **Subcommands** - invoke directly with `/claude-api `: @@ -406,23 +422,25 @@ Availability: `shared/platform-availability.md`. For agents on Bedrock / Vertex |---|---| | `managed-agents-onboard` | Walk the user through setting up a Managed Agent from scratch. **Read `shared/managed-agents-onboarding.md` immediately** and follow its interview script: **describe -> configure the agent (propose, don't interrogate) -> environment -> session** (same arc as the Console quickstart, auth deferred to the session step) - defaults and inline suggestions do the work, with a silent viability gate (job vs tools/credentials/data) before any code is emitted. Do not summarize - run the interview. | -**Reading guide:** Start with `shared/managed-agents-overview.md`, then the topical `shared/managed-agents-*.md` files (core, environments, tools, events, outcomes, multiagent, webhooks, memory, scheduled-deployments, client-patterns, onboarding, api-reference). For Python, TypeScript, Go, Ruby, PHP, and Java, read `{lang}/managed-agents/README.md` for code examples. For cURL, read `curl/managed-agents.md`. **Agents are persistent - create once, reference by ID.** Define agents and environments as version-controlled YAML applied with the `ant` CLI - this is the recommended flow (see `shared/anthropic-cli.md`): the CLI owns the control plane (creating and updating agents), your code owns the data plane (`sessions.create` with the stored agent ID). Call `agents.create()` in code only when you must provision programmatically; either way, store the returned agent ID and pass it to every subsequent `sessions.create`; never call `agents.create()` in the request path. If a binding you need isn't shown in the language README, WebFetch the relevant entry from `shared/live-sources.md` rather than guess. C# has beta Managed Agents support via `client.Beta.Agents` and related namespaces - see `csharp/claude-api/README.md` for details, or `curl/managed-agents.md` for raw HTTP reference. +**Reading guide:** Start with `shared/managed-agents-overview.md`, then the topical `shared/managed-agents-*.md` files (core, environments, tools, events, outcomes, multiagent, webhooks, memory, scheduled-deployments, client-patterns, onboarding, api-reference). For Python, TypeScript, Go, Ruby, PHP, and Java, read `{lang}/managed-agents/README.md` for code examples. For cURL, read `curl/managed-agents.md`. **Agents are persistent - create once, reference by ID.** Define agents and environments as version-controlled files synced with `ant apply` - this is the recommended flow (see `shared/anthropic-cli.md`): the CLI owns the control plane (creating and updating agents), your code owns the data plane (`sessions.create` with the stored agent ID). Call `agents.create()` in code only when you must provision programmatically; either way, store the returned agent ID and pass it to every subsequent `sessions.create`; never call `agents.create()` in the request path. If a binding you need isn't shown in the language README, WebFetch the relevant entry from `shared/live-sources.md` rather than guess. C# has beta Managed Agents support via `client.Beta.Agents` and related namespaces - see `csharp/claude-api/README.md` for details, or `curl/managed-agents.md` for raw HTTP reference. **When the user wants to set up a Managed Agent from scratch** (e.g. "how do I get started", "walk me through creating one", "set up a new agent"): read `shared/managed-agents-onboarding.md` and run its interview - same flow as the `managed-agents-onboard` subcommand. **When the user asks "how do I write the client code for X":** reach for `shared/managed-agents-client-patterns.md` - covers lossless stream reconnect, `processed_at` queued/processed gate, interrupt, `tool_confirmation` round-trip, the correct idle/terminated break gate, post-idle status race, stream-first ordering, file-mount gotchas, etc. For credentials, lead with vault `environment_variable` credentials - the first-class mechanism; secrets are substituted at egress and never enter the sandbox (`shared/managed-agents-tools.md` -> Vaults). Keeping credentials host-side via custom tools is the fallback where vault credentials don't fit (e.g. self-hosted sandboxes). +**When the task is a deliverable - default the kickoff to an outcome, not a plain message.** If the session's job is to produce something checkable (an artifact, a report, a PR, a dataset, a fixed set of changes), read `shared/managed-agents-outcomes.md` and kick off with `user.define_outcome` plus a starter rubric you draft from the task (5-10 concrete, independently gradeable criteria; comment it as a starter to tune). Reserve plain `user.message` for genuinely conversational sessions. Trigger on intent, not just the word: "keep working until it's right", "make sure the output is actually good", "don't stop at a first draft" all mean outcomes. + **When the user asks about tool approvals, permission policies, or "auto mode"** (which tool calls need a human, letting the server evaluate calls, `evaluated_permission` / `evaluation` on tool-use events): read `shared/managed-agents-tools.md` § Permission Policies - `always_allow` / `always_ask` / `auto` and the three `auto` outcomes (runs, denied as high-risk, pauses when indeterminate). For attaching a terminal to a live session (`ant beta:sessions connect`): `shared/anthropic-cli.md`. **When the user wants the agent to run on a schedule** (cron, "every night", "weekly report"): read `shared/managed-agents-scheduled-deployments.md` - deployments fire sessions autonomously on a cron cadence, with per-firing run records and lifecycle controls (pause/unpause/archive). -**When the agent's work fans out** (research across several sources, per-file or per-record work, "look into N things, then summarize") **or one loop would fill its context with reading:** read `shared/managed-agents-multiagent.md` and recommend a multiagent session - start with just `{"type": "self"}` in the roster so the agent can delegate to copies of itself, then move reading-heavy sub-tasks to a cheaper worker agent (e.g. Claude Haiku 4.5) referenced by ID. +**When the agent's work fans out** (research across several sources, per-file or per-record work, "look into N things, then summarize") **or one loop would fill its context with reading:** read `shared/managed-agents-multiagent.md` and recommend a multiagent session - start with just `{"type": "self"}` in the roster so the agent can delegate to copies of itself, then move reading-heavy sub-tasks to a cheaper worker agent (e.g. Claude Haiku 4.5, or Claude Sonnet 5.5 when the worker needs more judgment) referenced by ID. --- ## Server Tools (Quick Reference) -Server-side tools run on Anthropic's infrastructure - no client-side execution loop. Declare in `tools`; results arrive as content blocks in the same response. **No beta header** unless noted. **Prefer the latest type variant your model supports.** The `_20260209` web search / web fetch variants below (dynamic filtering) require Opus 5/4.8/4.7/4.6, Sonnet 5, or Sonnet 4.6; the basic variants for older models are listed after the table. +Server-side tools run on Anthropic's infrastructure - no client-side execution loop. Declare in `tools`; results arrive as content blocks in the same response. **No beta header** unless noted. **Prefer the latest type variant your model supports.** The `_20260209` web search / web fetch variants below (dynamic filtering) require Opus 5.5/5/4.8/4.7/4.6, Sonnet 5.5, Sonnet 5, or Sonnet 4.6; the basic variants for older models are listed after the table. | Tool | `type` | `name` | Key optional params | Result block type | |---|---|---|---|---| @@ -456,7 +474,7 @@ Server-side tools run on Anthropic's infrastructure - no client-side execution l **Message Batches (no beta; availability: `shared/platform-availability.md`):** `client.messages.batches.create(requests=[{custom_id, params}, ...])` -> poll `client.messages.batches.retrieve(id).processing_status` until `"ended"` -> stream `client.messages.batches.results(id)`. Each result has `.custom_id` + `.result.type` (`succeeded`/`errored`/`canceled`/`expired`); on success read `.result.message.content`. Python wraps requests as `Request(custom_id=..., params=MessageCreateParamsNonStreaming(...))`. Results arrive in **any order** - key by `custom_id`, never by position. -**Models API (no beta; availability: `shared/platform-availability.md`):** `client.models.list()` (auto-paginates) and `client.models.retrieve("claude-opus-5")`. Each model object has `id`, `display_name`, `created_at`, and - since Mar 2026 - `max_input_tokens` (the context window), `max_tokens` (the output cap), and `capabilities`. There is no `context_window` field. +**Models API (no beta; availability: `shared/platform-availability.md`):** `client.models.list()` (auto-paginates) and `client.models.retrieve("claude-opus-5-5")`. Each model object has `id`, `display_name`, `created_at`, and - since Mar 2026 - `max_input_tokens` (the context window), `max_tokens` (the output cap), and `capabilities`. There is no `context_window` field. **Stop details (GA, Opus 4.7+):** `response.stop_details` is populated **only when `stop_reason == "refusal"`** (fields: `type: "refusal"`, `category` - an open set, e.g. `"cyber"`, `"bio"`, `"reasoning_extraction"`, `"frontier_llm"`, or `null`; see the docs for the full list - and `explanation`). It is `null` for every other `stop_reason` (`end_turn`, `max_tokens`, `tool_use`, `pause_turn`, ...) - always guard before reading. @@ -488,17 +506,29 @@ The Quick Task Reference below uses the `{lang}/claude-api/FILE.md` path notatio **Long-running conversations (may exceed context window):** -> Read `{lang}/claude-api/README.md` - see Compaction section -**Migrating to a newer model (Fable 5.1 / Fable 5 / Opus 5 / Opus 4.8 / Opus 4.7 / Opus 4.6 / Sonnet 5 / Sonnet 4.6), replacing a retired model, or translating `budget_tokens` / prefill patterns to the current API:** +**Migrating to a newer model (Sonnet 5.5 / Opus 5.5 / Fable 5.1 / Fable 5 / Opus 5 / Opus 4.8 / Opus 4.7 / Opus 4.6 / Sonnet 5 / Sonnet 4.6), replacing a retired model, or translating `budget_tokens` / prefill patterns to the current API:** -> Read `shared/model-migration.md` **Upgrading the Anthropic SDK package itself across a major version (`anthropic` 0.x -> 1.x: `httpx2`, awaited async `.with_raw_response`, removed deprecated parameters / aliases / Text Completions, Python >= 3.10) - or writing new code against a project already on 1.x:** -> Read `{lang}/claude-api/sdk-upgrade.md` (currently Python only; other SDKs have no bundled major-version guide yet - use that SDK's CHANGELOG via `shared/live-sources.md`) +**Building an eval set for a Claude app (or "how do I know if my change helped"):** +-> Read `shared/evals/build-eval.md` - it loads `shared/evals/eval-audit.md` (the health checklist every eval must satisfy) before Step 0. +**Checking whether an existing eval is trustworthy ("is my eval any good?"):** +-> Read `shared/evals/eval-audit.md` and run it against the eval; report per its section 6. +**Iteratively improving an app against an eval (prompt tuning, hill-climbing):** +-> Read `shared/evals/eval-hillclimb.md` - runs Step 0 -> Step 5 with a train/test split; test is scored every round and is the headline. +**Rendering an eval-hillclimb HTML report:** +-> Run `shared/evals/report/build-report.mjs` when it is on disk, else `shared/evals/report/build-report-lite.mjs` (always extracted with this skill) - both consume the `_state.json` / `vN/` layout produced by the hillclimb guide and write the same `trajectory/scores.tsv`. Don't write a parallel one. +**Migrating to, prompting, or tuning Claude Opus 5.5 (thinking can't be disabled, effort tuning and the `medium` default, forced tool use, computer toolset, progress updates, safeguard false positives, visual inputs / design outputs):** +-> Read `shared/model-migration.md` -> Migrating to Claude Opus 5.5; the preserved-thinking mechanics it points at are under Migrating to Claude Fable 5.1 from Claude Fable 5 +**Migrating to, prompting, or tuning Claude Sonnet 5.5 (`between_tools` instead of disabled thinking, recalibrated effort, forced tool use, computer toolset, advisor pairings, progress updates, tool use in chat, mid-turn user messages, verification at low effort, safeguard categories):** +-> Read `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5 **Prompting or tuning Fable 5/5.1 (long turns, effort, verbosity, autonomous runs, sub-agents):** -> Read `shared/model-migration.md` -> Migrating to Claude Fable 5.1 -> Behavioral shifts (prompt-tunable) + Long-running agent recommendations **Prompting or tuning Claude Fable 5.1 (progress updates, parallel tool calls, writing density / formatting, autonomy, test sprawl, whole-file rewrites) or making a harness compatible with preserved thinking's history-editing check (history edits, compaction, per-turn reminders):** --> Read `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features + Behavioral shifts (prompt-tunable); for the history-editing check itself (the three-step check, the append-only edit table, compaction shapes), Breaking change 3 in the same section +-> Read `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> New API features + Behavioral shifts (prompt-tunable); for the history-editing check itself (the three-step check, the append-only edit table, compaction shapes), Breaking change 3 in the same section; to find, measure and fix the edits an *existing* harness makes (capture, diff, replay with `drop_block`, one fix per cause, model switches), run `preserved-thinking-migration` (Subcommands table) - it reads `shared/preserved-thinking-migration.md` **Prompt caching / optimize caching / "why is my cache hit rate low":** -> Read `shared/prompt-caching.md` (prefix-stability design, breakpoint placement, anti-patterns that silently invalidate cache) + `{lang}/claude-api/README.md` (Prompt Caching section) -**Auditing or cleaning up prompts, skills, or tool descriptions ("is this prompt outdated", "remove the cruft", "this was written for an older model"):** +**Auditing or cleaning up prompts, tool descriptions, skills, or agent configuration files such as `CLAUDE.md` ("is this prompt outdated", "remove the cruft", "this was written for an older model"):** -> Read `shared/prompt-audit.md` - dated-pattern tables with greppable signals, the keep list (what NOT to delete), and the report + proposed-diff output contract **Count tokens in a file / prompt / diff ("how many tokens is X"):** -> Read `shared/token-counting.md` - use `messages.count_tokens`, never `tiktoken` @@ -544,29 +574,30 @@ Live documentation URLs are in `shared/live-sources.md`. ## Common Pitfalls - Don't truncate inputs when passing files or content to the API. If the content is too long to fit in the context window, notify the user and discuss options (chunking, summarization, etc.) rather than silently truncating. -- **Prefill removed (Fable 5, Claude Fable 5.1, Opus 5, Sonnet 5, and the 4.6/4.7/4.8 family):** Assistant message prefills (last-assistant-turn prefills) return a 400 error on Fable 5, Claude Fable 5.1, Opus 5, Sonnet 5, Opus 4.6, Opus 4.7, Opus 4.8, and Sonnet 4.6. Use structured outputs (`output_config.format`) or system prompt instructions to control response format instead. (One exception: the fallback-credit prefill claim - when redeeming a credit with `fallback_has_prefill_claim: true`, the server accepts the echoed assistant message; see the migration guide's refusal section.) +- **Prefill removed (Fable 5, Claude Fable 5.1, Opus 5, Claude Opus 5.5, Sonnet 5, Claude Sonnet 5.5, and the 4.6/4.7/4.8 family):** Assistant message prefills (last-assistant-turn prefills) return a 400 error on Fable 5, Claude Fable 5.1, Opus 5, Claude Opus 5.5, Sonnet 5, Claude Sonnet 5.5, Opus 4.6, Opus 4.7, Opus 4.8, and Sonnet 4.6. Use structured outputs (`output_config.format`) or system prompt instructions to control response format instead. (One exception: the fallback-credit prefill claim - when redeeming a credit with `fallback_has_prefill_claim: true`, the server accepts the echoed assistant message; see the migration guide's refusal section.) - **Confirm migration scope before editing:** When a user asks to migrate code to a newer Claude model without naming a specific file, directory, or file list, **ask which scope to apply first** - the entire working directory, a specific subdirectory, or a specific set of files. Do not start editing until the user confirms. Imperative phrasings like "migrate my codebase", "move my project to X", "upgrade to Sonnet 4.6", or bare "migrate to Opus 4.8" are **still ambiguous** - they tell you what to do but not where, so ask. Proceed without asking only when the prompt names an exact file, a specific directory, or an explicit file list ("migrate `app.py`", "migrate everything under `services/`", "update `a.py` and `b.py`"). See `shared/model-migration.md` Step 0. - **`max_tokens` defaults:** Don't lowball `max_tokens` - hitting the cap truncates output mid-thought and requires a retry. For non-streaming requests, default to `~16000` (keeps responses under SDK HTTP timeouts). For streaming requests, default to `~64000` (timeouts aren't a concern, so give the model room). Only go lower when you have a hard reason: classification (`~256`), cost caps, deliberately short outputs, or **`max_tokens: 0`** for cache pre-warming (see `shared/prompt-caching.md` -> Pre-warming). -- **Disabling thinking on Claude Opus 5 has two failure modes - prefer low/medium effort instead.** Only affects code that explicitly opts out; thinking is on by default, so watch for a disabled-thinking setting carried forward from Opus 4.8. With `thinking: {type: "disabled"}`, the model occasionally writes a tool call into its **visible text** instead of a `tool_use` block: the turn succeeds, the call never runs, no error is raised, and in an agentic loop that text pollutes later turns. It can also leak `` tags into the response. Turning thinking on and lowering `effort` fixes both and still cuts cost. If a route must stay thinking-off: **delete** any don't-think/don't-reason rule (it makes tag leakage worse), don't name thinking tags, and add the combined instruction *"When you use a tool, you may say a brief sentence first. If no tool can express what the user asked for, say so instead of guessing. Do not include internal or system XML tags in your response."* Details: `shared/model-migration.md` -> Two failure modes when thinking is disabled. -- **128K output tokens:** Fable 5, Claude Fable 5.1, Opus 5, Opus 4.6, Opus 4.7, Opus 4.8, Sonnet 5, and Sonnet 4.6 support up to 128K `max_tokens`, but the SDKs require streaming for values that large to avoid HTTP timeouts. Use `.stream()` with `.get_final_message()` / `.finalMessage()`. -- **Forced tool use removed (Claude Fable 5.1 / Claude Mythos 5.1, as on Mythos Preview):** `tool_choice: {type: "any"}` and `{type: "tool", name: ...}` return a 400 (`tool_choice: type "tool" and "any" are not supported for this model.`), on `count_tokens` and Batches too. Use `{type: "auto"}` plus an explicit instruction naming the tool, `strict: true` on the tool to keep schema-valid arguments, or structured outputs (`output_config.format`) when the forced call only existed to get JSON back. `{type: "none"}` is unaffected; `disable_parallel_tool_use` still works with `auto` (at most one call). -- **Tool call JSON parsing (Fable 5, Claude Fable 5.1, Opus 5, and the 4.6/4.7/4.8 family):** Fable 5, Claude Fable 5.1, Opus 5, Opus 4.6, Opus 4.7, Opus 4.8, and Sonnet 4.6 may produce different JSON string escaping in tool call `input` fields (e.g., Unicode or forward-slash escaping). Always parse tool inputs with `json.loads()` / `JSON.parse()` - never do raw string matching on the serialized input. +- **Disabling thinking on Claude Opus 5 has two failure modes - prefer low/medium effort instead.** (On Claude Opus 5.5 it can't be disabled at all - `{type: "disabled"}` is a 400 at every effort level; use `low` effort. On Claude Sonnet 5.5, `{type: "disabled"}` is also a 400 - try thinking on at `low` effort first, and if a route must stay thinking-off, send `{type: "between_tools"}` at `high` effort or below.) Only affects code that explicitly opts out; thinking is on by default, so watch for a disabled-thinking setting carried forward from Opus 4.8. With `thinking: {type: "disabled"}`, the model occasionally writes a tool call into its **visible text** instead of a `tool_use` block: the turn succeeds, the call never runs, no error is raised, and in an agentic loop that text pollutes later turns. It can also leak `` tags into the response. Turning thinking on and lowering `effort` fixes both and still cuts cost. If a route must stay thinking-off: **delete** any don't-think/don't-reason rule (it makes tag leakage worse), don't name thinking tags, and add the combined instruction *"When you use a tool, you may say a brief sentence first. If no tool can express what the user asked for, say so instead of guessing. Do not include internal or system XML tags in your response."* Details: `shared/model-migration.md` -> Two failure modes when thinking is disabled. +- **128K output tokens:** Fable 5, Claude Fable 5.1, Opus 5, Claude Opus 5.5, Opus 4.6, Opus 4.7, Opus 4.8, Claude Sonnet 5.5, Sonnet 5, and Sonnet 4.6 support up to 128K `max_tokens`, but the SDKs require streaming for values that large to avoid HTTP timeouts. Use `.stream()` with `.get_final_message()` / `.finalMessage()`. +- **Forced tool use removed (Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5):** `tool_choice: {type: "any"}` and `{type: "tool", name: ...}` return a 400 (`tool_choice: type "tool" and "any" are not supported for this model.`), on `count_tokens` and Batches too. Use `{type: "auto"}` plus an explicit instruction naming the tool, `strict: true` on the tool to keep schema-valid arguments, or structured outputs (`output_config.format`) when the forced call only existed to get JSON back. `{type: "none"}` is unaffected; `disable_parallel_tool_use` still works with `auto` (at most one call). +- **Tool call JSON parsing (Fable 5, Claude Fable 5.1, Opus 5, Claude Opus 5.5, and the 4.6/4.7/4.8 family):** Fable 5, Claude Fable 5.1, Opus 5, Claude Opus 5.5, Opus 4.6, Opus 4.7, Opus 4.8, and Sonnet 4.6 may produce different JSON string escaping in tool call `input` fields (e.g., Unicode or forward-slash escaping). Always parse tool inputs with `json.loads()` / `JSON.parse()` - never do raw string matching on the serialized input. - **Structured outputs (all models):** Use `output_config: {format: {...}}` instead of the deprecated `output_format` parameter on `messages.create()`. This is a general API change, not 4.6-specific. - **Don't reimplement SDK functionality:** The SDK provides high-level helpers - use them instead of building from scratch. Specifically: use `stream.finalMessage()` instead of wrapping `.on()` events in `new Promise()`; use typed exception classes (`Anthropic.RateLimitError`, etc.) instead of string-matching error messages; use SDK types (`Anthropic.MessageParam`, `Anthropic.Tool`, `Anthropic.Message`, etc.) instead of redefining equivalent interfaces. - **Error handling - catch a chain, not one broad class.** A single `except APIStatusError` / `catch (AnthropicServiceException)` / `rescue APIError` loses the distinction between retryable (429, >=500, network) and non-retryable (400/404) failures. Write a most-specific-first chain - e.g. `NotFoundError` -> `RateLimitError` -> `APIStatusError` -> `APIConnectionError` (or the Go equivalent: `errors.As` into `*anthropic.Error` then `switch apierr.StatusCode { case 404: ...; case 429: ...; default: ... }`). Per-language class names and namespaces are in `shared/error-codes.md`. - **Don't research SDK types - write first.** If a type name isn't shown in the documentation included in this skill, write the code file from the namespace/package tables in the language-specific doc and let the compiler's error point you to the right name. Do not spend turns on WebFetch, SDK-repo clones, or compiling-and-running a separate reflection program to discover type names before writing - produce the source file first, then fix what the compiler reports. A quick `strings` / `jar tf` / `javap` against the installed SDK is acceptable for locating names (it returns in seconds), but don't escalate beyond that. A file with a wrong type name is recoverable; a session spent on discovery with no file written is not. - **Bash and text editor tools are Anthropic-defined, schema-less.** Declare `{"type": "bash_20250124", "name": "bash"}` / `{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}` - no `input_schema`. A custom tool with your own schema named `"bash"` is a different tool. Handler paths and security checks are in `shared/tool-use-concepts.md` § Client-Side Tools. -- **Advisor tool model pairing.** The advisor tool's `model` must be at least as capable as the request's top-level `model` - e.g. executor `claude-sonnet-5` -> advisor `claude-opus-4-8` or `claude-opus-4-7`. An invalid pair returns 400. Pairing table in `shared/tool-use-concepts.md` § Advisor. Availability: `shared/platform-availability.md`. +- **Advisor tool model pairing.** The advisor tool's `model` must be at least as capable as the request's top-level `model` - e.g. executor `claude-sonnet-5-5` -> advisor `claude-opus-5-5`. An invalid pair returns 400; a `claude-sonnet-5-5` executor accepts only the advisors its row in the pairing table lists (not Claude Opus 4.8 / 4.7 / 4.6, Claude Sonnet 5, or Sonnet 4.6). Pairing table (and which advisors return plaintext vs encrypted `advisor_redacted_result` advice) in `shared/tool-use-concepts.md` § Advisor. Availability: `shared/platform-availability.md`. - **Agent Skills != Managed Agents.** To have Claude generate a `.pptx`/`.xlsx`/etc. via Agent Skills, call `client.beta.messages.create` with `container={"skills": [...]}`, the `code_execution_20260521` tool, and the `code-execution-2025-08-25` beta (Skills is out of beta - no `skills-2025-10-02` header needed). Do not use `client.beta.agents` / `sessions` / `environments` here - those are the Managed Agents surface, not Agent Skills. - **MCP connector needs both halves.** `mcp_servers=[{type:"url", url, name}]` alone is rejected as a validation error - also add `tools=[{type:"mcp_toolset", mcp_server_name:}]` with beta `mcp-client-2025-11-20`. Availability: `shared/platform-availability.md`. - **`inference_geo` is a direct top-level request parameter** - `client.messages.create(..., inference_geo="us")` / `.inferenceGeo("us")`. Do not put it in `extra_body` / `putAdditionalBodyProperty`. (Messages API only - on Managed Agents, `inference_geo` instead nests inside the agent's `model` object, never top-level; see `shared/managed-agents-core.md` § Pinning inference geography.) Supported on Opus 4.6 / Sonnet 4.6 and later; availability: `shared/platform-availability.md`. `response.usage.inference_geo` reports where inference ran. -- **Fine-grained tool streaming is not a beta feature.** Set `eager_input_streaming: true` on the tool definition and call the regular `client.messages.stream(...)`. There is no beta header and no `client.beta.*` path. +- **Fine-grained tool streaming is not a beta feature; this skill's default is to turn it on for streaming + client tools (the API itself still defaults to buffered).** Set `eager_input_streaming: true` on the tool definition and call the regular `client.messages.stream(...)`. There is no beta header and no `client.beta.*` path. Do not also send the legacy `fine-grained-tool-streaming-2025-05-14` beta header. Python's `@beta_tool(eager_input_streaming=True)` accepts it directly; TypeScript's `betaZodTool()` does not, so spread it on: `{ ...betaZodTool({...}), eager_input_streaming: true }`. With the field on, the API no longer coerces or validates the input, so the accumulated `partial_json` may be incomplete (`max_tokens`) or invalid - guard the parse (`shared/tool-use-concepts.md` -> Eager input streaming). - **Cache diagnostics is beta.** Use `client.beta.messages.*` with beta `cache-diagnosis-2026-04-07`. Pass `diagnostics: {previous_message_id: null}` on the first turn and `diagnostics: {previous_message_id: }` on subsequent turns; the result is on `response.diagnostics`. Availability: `shared/platform-availability.md`. - **Memory tool type is `memory_20250818`.** Declare `{"type": "memory_20250818", "name": "memory"}`. Go uses the beta-namespace type `{OfMemoryTool20250818: &anthropic.BetaMemoryTool20250818Param{}}` on `client.Beta.Messages.New`; Python/TypeScript/Ruby/PHP/C# use the non-beta `client.messages.create`; Java has both a non-beta `MemoryTool20250818` and a beta tool-runner path. Python/TypeScript provide `BetaAbstractMemoryTool` / `betaMemoryTool` helpers for implementing the backend. -- **Use a model the feature actually supports.** Some features are restricted to specific model tiers - fast mode is Claude Opus 5 / Opus 4.8 only (and Claude API only), task budgets (Messages API only - Managed Agents session budgets have no model-tier restriction) are Claude Opus 5 / Fable 5 / Claude Fable 5.1 (confirm at launch) / Sonnet 5 / Opus 4.8 / 4.7 only, and the advisor tool requires a valid executor<->advisor pair. If the user's prompt names a model that the feature doesn't support, use a supported model instead and note the substitution in the output. +- **Use a model the feature actually supports.** Some features are restricted to specific model tiers - fast mode is Claude Opus 5 / Claude Opus 5.5 / Opus 4.8 only (and Claude API only), task budgets (Messages API only - Managed Agents session budgets have no model-tier restriction) are Claude Opus 5 / Claude Opus 5.5 / Fable 5 / Claude Fable 5.1 (confirm at launch) / Claude Sonnet 5.5 / Opus 4.8 / 4.7 only (not Claude Sonnet 5), and the advisor tool requires a valid executor<->advisor pair. If the user's prompt names a model that the feature doesn't support, use a supported model instead and note the substitution in the output. - **Don't define custom types for SDK data structures:** The SDK exports types for all API objects. Use `Anthropic.MessageParam` for messages, `Anthropic.Tool` for tool definitions, `Anthropic.ToolUseBlock` / `Anthropic.ToolResultBlockParam` for tool results, `Anthropic.Message` for responses. Defining your own `interface ChatMessage { role: string; content: unknown }` duplicates what the SDK already provides and loses type safety. - **Report and document output:** For tasks that produce reports, documents, or visualizations, the code execution sandbox has `python-docx`, `python-pptx`, `matplotlib`, `pillow`, and `pypdf` pre-installed. Claude can generate formatted files (DOCX, PDF, charts) and return them via the Files API - consider this for "report" or "document" type requests instead of plain stdout text. - **Server-tool errors don't raise.** Web search and web fetch errors return HTTP 200 with a `web_search_tool_result` / `web_fetch_tool_result` block whose `content` is a single error object (e.g. `{error_code: "max_uses_exceeded"}`) - not a raised exception. For web search, a success `content` is a *list*; an error `content` is an *object* - branch on that before indexing. - **Managed Agents web tools ignore the environment's `networking`.** `web_search` / `web_fetch` run on Anthropic's servers in cloud *and* self-hosted environments, and Console org-level web settings apply to the Messages API only. Restrict them per tool with `allowed_domains` **or** `blocked_domains` (never both; 1-64 plain hostnames per list, subdomains covered; IPs, bare TLDs, single-label and `localhost`-style names rejected on both tools; a path suffix is allowed only on `web_search`) on the toolset `configs` entry - `shared/managed-agents-tools.md` § Web search & web fetch settings. +- **Eval / hillclimb work has dedicated guides:** If the user says "hillclimb", "improve my eval score", "iterate on my prompt against an eval", or "build me an eval" - load `shared/evals/eval-hillclimb.md` or `shared/evals/build-eval.md` rather than improvising. The bundled HTML report builder is `shared/evals/report/build-report.mjs` when it is on disk, else `shared/evals/report/build-report-lite.mjs` (always extracted with this skill); don't write a parallel one. - **Code execution output block type:** `code_execution_20260521` returns `bash_code_execution_tool_result` (with `.content.stdout`), **not** the legacy bare `code_execution_tool_result`. Iterate `response.content` and match on the correct type. - **Tool search: never defer everything.** The search tool itself must not have `defer_loading: true`, and at least one tool in `tools` must be non-deferred, or the API returns 400 `All tools have defer_loading set`. diff --git a/skills/claude-api/csharp/claude-api/README.md b/skills/claude-api/csharp/claude-api/README.md index 4b04b5089..23cbbc9ef 100644 --- a/skills/claude-api/csharp/claude-api/README.md +++ b/skills/claude-api/csharp/claude-api/README.md @@ -35,7 +35,7 @@ Write from this table instead of reflecting the SDK assembly. Endpoint column te | Programmatic tool calling | non-beta | `CodeExecutionTool20260120`, `ToolResultBlockParam`, `ContentBlockParam` | | Task budgets | beta | `BetaOutputConfig` with `TaskBudget = new BetaTokenTaskBudget { ... }` | | Tool search | non-beta | `new ToolUnion(new ToolSearchToolRegex20251119 { Type = ToolSearchToolRegex20251119Type.ToolSearchToolRegex20251119 })` - `Type` must be set explicitly. | -| Web search | non-beta | `new ToolUnion(new WebSearchTool20260209())` - the latest variant with dynamic filtering (Claude Fable 5.1 + Claude Opus 5 + Opus 4.8/4.7/4.6 + Claude Sonnet 5 + Sonnet 4.6). For older models or Vertex, use `WebSearchTool20250305()` | +| Web search | non-beta | `new ToolUnion(new WebSearchTool20260209())` - the latest variant with dynamic filtering (Claude Fable 5.1 + Claude Opus 5.5 + Claude Opus 5 + Opus 4.8/4.7/4.6 + Claude Sonnet 5.5 + Claude Sonnet 5 + Sonnet 4.6). For older models or Vertex, use `WebSearchTool20250305()` | ### Discovering type and member names @@ -58,7 +58,7 @@ AnthropicClient client = new(); var message = await client.Messages.Create(new MessageCreateParams { - Model = "claude-opus-5", + Model = "claude-opus-5-5", MaxTokens = 1024, Messages = [ new() { Role = Role.User, Content = "Hello, Claude" } ], }); @@ -77,7 +77,7 @@ AnthropicClient client = new(); var response = await client.Beta.Messages.Create(new MessageCreateParams { - Model = "claude-opus-5", + Model = "claude-opus-5-5", MaxTokens = 4096, Betas = [""], Messages = [ new() { Role = Role.User, Content = "..." } ], @@ -123,7 +123,7 @@ using Anthropic.Models.Messages; var parameters = new MessageCreateParams { - Model = "claude-opus-5", + Model = "claude-opus-5-5", MaxTokens = 16000, Messages = [new() { Role = Role.User, Content = "What is the capital of France?" }] }; @@ -144,7 +144,8 @@ foreach (var text in response.Content.Select(b => b.Value).OfType()) **Adaptive thinking is the recommended mode for Claude 4.6+ models.** Claude decides dynamically when and how much to think. -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (below). `new ThinkingConfigEnabled { BudgetTokens = N }` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (below). `new ThinkingConfigEnabled { BudgetTokens = N }` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `Thinking` (or send `ThinkingConfigAdaptive`, which is equivalent); `ThinkingConfigDisabled` returns a 400 at every effort, as does a thinking budget. Control depth with `OutputConfig.Effort` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `Thinking` runs adaptive (`ThinkingConfigAdaptive` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `ThinkingConfigDisabled` is accepted only at effort `high` or lower; pairing it with `xhigh`/`max` returns a 400. > **Older models:** Use `new ThinkingConfigEnabled { BudgetTokens = N }` (budget must be < `MaxTokens`, min 1024). @@ -153,11 +154,11 @@ using Anthropic.Models.Messages; var response = await client.Messages.Create(new MessageCreateParams { - Model = "claude-opus-5", + Model = "claude-opus-5-5", MaxTokens = 16000, // ThinkingConfigParam? implicitly converts from the concrete variant classes - // no wrapper needed. - // display opt-in: default is omitted (empty thinking text) on Fable 5 / Mythos 5 / Claude Opus 5 / Opus 4.8 / 4.7 + // display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 Thinking = new ThinkingConfigAdaptive { Display = Display.Summarized }, Messages = [ @@ -201,7 +202,7 @@ using Anthropic.Models.Beta.Messages; var betaParams = new MessageCreateParams // no Beta prefix - see unprefixed list above { - Model = "claude-opus-5", + Model = "claude-opus-5-5", MaxTokens = 16000, Betas = ["compact-2026-01-12"], ContextManagement = new BetaContextManagementConfig @@ -290,7 +291,7 @@ Verify hits via `response.Usage.CacheCreationInputTokens` / `response.Usage.Cach ```csharp MessageTokensCount result = await client.Messages.CountTokens(new MessageCountTokensParams { - Model = "claude-opus-5", + Model = "claude-opus-5-5", Messages = [new() { Role = Role.User, Content = "Hello" }], }); long tokens = result.InputTokens; @@ -320,7 +321,7 @@ new MessageParam { ```csharp var response = await client.Beta.Messages.Create(new MessageCreateParams { - Model = "claude-opus-5", MaxTokens = 4096, + Model = "claude-opus-5-5", MaxTokens = 4096, Speed = Speed.Fast, Betas = ["fast-mode-2026-02-01"], Messages = [...], @@ -332,14 +333,14 @@ var response = await client.Beta.Messages.Create(new MessageCreateParams { ```csharp var page = await client.Models.List(); foreach (var m in page.Items) { Console.WriteLine(m.ID); } -var one = await client.Models.Retrieve("claude-opus-5"); +var one = await client.Models.Retrieve("claude-opus-5-5"); ``` ## Long Output (128k) + Prefill Set `MaxTokens = 128000` on `client.Messages` and use the streaming path (see `streaming.md`). On Claude 4+ models, 128k output is native - no `output-128k-*` beta header or beta namespace is needed. -**Prefilling the assistant message** (putting a trailing `Role.Assistant` message in the input) is **not supported** on Claude Fable 5.1, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6 - requests return a 400. Use `OutputConfig.Format` (structured outputs) instead. +**Prefilling the assistant message** (putting a trailing `Role.Assistant` message in the input) is **not supported** on Claude Fable 5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6 - requests return a 400. Use `OutputConfig.Format` (structured outputs) instead. ## Stop Details diff --git a/skills/claude-api/csharp/claude-api/batches.md b/skills/claude-api/csharp/claude-api/batches.md index 3b4003ce6..629f98366 100644 --- a/skills/claude-api/csharp/claude-api/batches.md +++ b/skills/claude-api/csharp/claude-api/batches.md @@ -5,7 +5,7 @@ ```csharp var batch = await client.Messages.Batches.Create(new() { Requests = [ - new() { CustomID = "req-1", Params = new() { Model = "claude-opus-5", MaxTokens = 1024, Messages = [...] } }, + new() { CustomID = "req-1", Params = new() { Model = "claude-opus-5-5", MaxTokens = 1024, Messages = [...] } }, ], }); // Poll client.Messages.Batches.Retrieve(batch.ID) until ProcessingStatus == "ended", diff --git a/skills/claude-api/csharp/claude-api/streaming.md b/skills/claude-api/csharp/claude-api/streaming.md index be16108e5..5d0b5ad0a 100644 --- a/skills/claude-api/csharp/claude-api/streaming.md +++ b/skills/claude-api/csharp/claude-api/streaming.md @@ -7,7 +7,7 @@ using Anthropic.Models.Messages; var parameters = new MessageCreateParams { - Model = Model.ClaudeOpus4_8, + Model = "claude-opus-5-5", MaxTokens = 64000, Messages = [new() { Role = Role.User, Content = "Write a haiku" }] }; diff --git a/skills/claude-api/csharp/claude-api/tool-use.md b/skills/claude-api/csharp/claude-api/tool-use.md index e6cc63751..c97dbc102 100644 --- a/skills/claude-api/csharp/claude-api/tool-use.md +++ b/skills/claude-api/csharp/claude-api/tool-use.md @@ -14,7 +14,7 @@ using Anthropic.Models.Messages; var parameters = new MessageCreateParams { - Model = Model.ClaudeSonnet4_6, + Model = "claude-opus-5-5", MaxTokens = 16000, Tools = [ new Tool { diff --git a/skills/claude-api/curl/examples.md b/skills/claude-api/curl/examples.md index 9a623c9bb..78b19edc1 100644 --- a/skills/claude-api/curl/examples.md +++ b/skills/claude-api/curl/examples.md @@ -18,7 +18,7 @@ curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 16000, "messages": [ {"role": "user", "content": "What is the capital of France?"} @@ -38,7 +38,7 @@ response=$(curl -s https://api.anthropic.com/v1/messages \ -H "Content-Type: application/json" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -d '{"model":"claude-opus-5","max_tokens":16000,"messages":[{"role":"user","content":"Hello"}]}') + -d '{"model":"claude-opus-5-5","max_tokens":16000,"messages":[{"role":"user","content":"Hello"}]}') # Print the first text block (-r strips the JSON quotes) echo "$response" | jq -r '.content[0].text' @@ -65,7 +65,7 @@ curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 64000, "stream": true, "messages": [{"role": "user", "content": "Write a haiku"}] @@ -104,7 +104,7 @@ curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 16000, "tools": [{ "name": "get_weather", @@ -129,7 +129,7 @@ curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 16000, "tools": [{ "name": "get_weather", @@ -167,7 +167,7 @@ curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 16000, "system": [ {"type": "text", "text": "", "cache_control": {"type": "ephemeral"}} @@ -182,18 +182,19 @@ For 1-hour TTL: `"cache_control": {"type": "ephemeral", "ttl": "1h"}`. Top-level ## Extended Thinking -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `thinking` (or send `{"type": "adaptive"}`, which is equivalent); `{"type": "disabled"}` returns a 400 at every effort, as does a thinking budget. Control depth with `output_config.effort` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `"thinking"` runs adaptive (`{"type": "adaptive"}` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `{"type": "disabled"}` is accepted only at effort `high` or lower; pairing it with `xhigh`/`max` returns a 400. > **Older models:** Use `"type": "enabled"` with `"budget_tokens": N` (must be < `max_tokens`, min 1024). ```bash -# Fable 5 / Claude Opus 5 / Opus 4.8 / 4.7 / 4.6: adaptive thinking (recommended) +# Fable 5 / Claude Opus 5.5 / Claude Opus 5 / Opus 4.8 / 4.7 / 4.6: adaptive thinking (recommended) curl https://api.anthropic.com/v1/messages \ -H "Content-Type: application/json" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ - "model": "claude-opus-5", + "model": "claude-opus-5-5", "max_tokens": 16000, "thinking": { "type": "adaptive", diff --git a/skills/claude-api/curl/managed-agents.md b/skills/claude-api/curl/managed-agents.md index 8f52314b6..d0e4fe4b3 100644 --- a/skills/claude-api/curl/managed-agents.md +++ b/skills/claude-api/curl/managed-agents.md @@ -65,7 +65,7 @@ curl -X POST https://api.anthropic.com/v1/agents \ "${HEADERS[@]}" \ -d '{ "name": "Coding Assistant", - "model": "claude-opus-5", + "model": "claude-opus-5-5", "tools": [{ "type": "agent_toolset_20260401" }] }' # -> { "id": "agent_abc123", ... } @@ -89,7 +89,7 @@ curl -X POST https://api.anthropic.com/v1/agents \ "${HEADERS[@]}" \ -d '{ "name": "Code Reviewer", - "model": "claude-opus-5", + "model": "claude-opus-5-5", "system": "You are a senior code reviewer. Be thorough and constructive.", "tools": [ { "type": "agent_toolset_20260401" }, @@ -281,7 +281,6 @@ curl -X DELETE https://api.anthropic.com/v1/sessions/$SESSION_ID \ curl -X POST https://api.anthropic.com/v1/files \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -H "anthropic-beta: files-api-2025-04-14" \ -F "file=@path/to/file.txt" \ -F "purpose=agent" ``` @@ -297,13 +296,12 @@ List files the agent wrote to `/mnt/session/outputs/` during a session, then dow curl "https://api.anthropic.com/v1/files?scope_id=$SESSION_ID" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -H "anthropic-beta: files-api-2025-04-14,managed-agents-2026-04-01" + -H "anthropic-beta: managed-agents-2026-04-01" # Download a specific file curl "https://api.anthropic.com/v1/files/$FILE_ID/content" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -H "anthropic-beta: files-api-2025-04-14,managed-agents-2026-04-01" \ -o downloaded_file.txt ``` @@ -326,7 +324,7 @@ curl -X POST https://api.anthropic.com/v1/agents \ "${HEADERS[@]}" \ -d '{ "name": "MCP Agent", - "model": "claude-opus-5", + "model": "claude-opus-5-5", "mcp_servers": [ { "type": "url", "name": "my-tools", "url": "https://my-mcp-server.example.com/sse" } ], @@ -357,7 +355,7 @@ curl -X POST https://api.anthropic.com/v1/agents \ "${HEADERS[@]}" \ -d '{ "name": "Restricted Agent", - "model": "claude-opus-5", + "model": "claude-opus-5-5", "tools": [ { "type": "agent_toolset_20260401", diff --git a/skills/claude-api/go/claude-api/README.md b/skills/claude-api/go/claude-api/README.md index 2267a7d17..540ee4dda 100644 --- a/skills/claude-api/go/claude-api/README.md +++ b/skills/claude-api/go/claude-api/README.md @@ -27,11 +27,11 @@ client := anthropic.NewClient( --- -## Model Constants +## Model IDs -The Go SDK provides typed model constants: `anthropic.ModelClaudeFable5`, `anthropic.ModelClaudeOpus4_8`, `anthropic.ModelClaudeOpus4_7`, `anthropic.ModelClaudeSonnet4_6`, `anthropic.ModelClaudeHaiku4_5_20251001`. Default to Claude Opus 5 unless the user specifies otherwise; if they ask for Fable or the most powerful model, use `anthropic.ModelClaudeFable5` (see `shared/models.md` for the full resolution table). +`anthropic.Model` is an alias for `string`, so pass the model as its plain id: `Model: "claude-opus-5-5"`. Default to Claude Opus 5.5 unless the user specifies otherwise; if they ask for Fable or the most powerful model, use `"claude-fable-5-1"`; if they ask for a cheaper tier, use the current generation - `"claude-sonnet-5-5"` or `"claude-haiku-4-5"` (see `shared/models.md` for the full resolution table). -`anthropic.Model` is an alias for `string`, so a model with no typed constant yet - including Claude Opus 5 - is passed as the plain id: `Model: "claude-opus-5"`. Check the SDK release notes for a typed `Claude Opus 5` constant before assuming one exists. +The SDK also ships typed `anthropic.ModelClaude*` constants, but they lag model launches - a given SDK release may have constants only for previous-generation models. Do not pick a model because it has a typed constant; the string id works for every model on every SDK version. Check the SDK release notes before assuming a typed constant exists for a current model. --- @@ -39,7 +39,7 @@ The Go SDK provides typed model constants: `anthropic.ModelClaudeFable5`, `anthr ```go response, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{ - Model: "claude-opus-5", + Model: "claude-opus-5-5", MaxTokens: 16000, Messages: []anthropic.MessageParam{ anthropic.NewUserMessage(anthropic.NewTextBlock("What is the capital of France?")), @@ -69,9 +69,10 @@ Derived from `anthropic-sdk-go/message.go` (`ThinkingConfigParamUnion`, `Thinkin ```go // There is no ThinkingConfigParamOfAdaptive helper - construct the union // struct-literal directly and take the address of the variant. -adaptive := anthropic.ThinkingConfigAdaptiveParam{} +// display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 +adaptive := anthropic.ThinkingConfigAdaptiveParam{Display: anthropic.ThinkingConfigAdaptiveDisplaySummarized} params := anthropic.MessageNewParams{ - Model: anthropic.ModelClaudeSonnet4_6, + Model: "claude-opus-5-5", MaxTokens: 16000, Thinking: anthropic.ThinkingConfigParamUnion{OfAdaptive: &adaptive}, Messages: []anthropic.MessageParam{ @@ -95,11 +96,12 @@ for _, block := range resp.Content { } ``` -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (above). `ThinkingConfigParamOfEnabled(budgetTokens)` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (above). `ThinkingConfigParamOfEnabled(budgetTokens)` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - leave `Thinking` unset (or send the adaptive union, which is equivalent); `OfDisabled` returns a 400 at every effort, as does `ThinkingConfigParamOfEnabled`. Control depth with `Effort` under `OutputConfig` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - leaving `Thinking` unset runs adaptive (the adaptive union is equivalent), unlike Opus 4.8/4.7 where leaving it unset meant no thinking. > **Older models:** Use `anthropic.ThinkingConfigParamOfEnabled(N)` (budget must be < `MaxTokens`, min 1024). -To disable: `anthropic.ThinkingConfigParamUnion{OfDisabled: &anthropic.ThinkingConfigDisabledParam{}}`. On Claude Opus 5 that is accepted only at effort `high` or lower - pairing it with `xhigh`/`max` returns a 400. +To disable: `anthropic.ThinkingConfigParamUnion{OfDisabled: &anthropic.ThinkingConfigDisabledParam{}}`. On Claude Opus 5 that is accepted only at effort `high` or lower - pairing it with `xhigh`/`max` returns a 400; on Claude Opus 5.5 it returns a 400 at every effort (lower `Effort` instead). --- @@ -158,7 +160,7 @@ Use `Beta.Messages.New` with `ContextManagement` on `BetaMessageNewParams`. Ther ```go params := anthropic.BetaMessageNewParams{ - Model: "claude-opus-5", // also supported: ModelClaudeOpus4_8, ModelClaudeSonnet4_6 + Model: "claude-opus-5-5", MaxTokens: 16000, Betas: []anthropic.AnthropicBeta{"compact-2026-01-12"}, ContextManagement: anthropic.BetaContextManagementConfigParam{ diff --git a/skills/claude-api/go/claude-api/streaming.md b/skills/claude-api/go/claude-api/streaming.md index 72e44cb07..7b5eb9ee2 100644 --- a/skills/claude-api/go/claude-api/streaming.md +++ b/skills/claude-api/go/claude-api/streaming.md @@ -4,7 +4,7 @@ ```go stream := client.Messages.NewStreaming(context.Background(), anthropic.MessageNewParams{ - Model: anthropic.ModelClaudeOpus4_8, + Model: "claude-opus-5-5", MaxTokens: 64000, Messages: []anthropic.MessageParam{ anthropic.NewUserMessage(anthropic.NewTextBlock("Write a haiku")), diff --git a/skills/claude-api/go/claude-api/tool-use.md b/skills/claude-api/go/claude-api/tool-use.md index e9a301cff..5ccae9913 100644 --- a/skills/claude-api/go/claude-api/tool-use.md +++ b/skills/claude-api/go/claude-api/tool-use.md @@ -44,7 +44,7 @@ runner := client.Beta.Messages.NewToolRunner( []anthropic.BetaTool{weatherTool}, anthropic.BetaToolRunnerParams{ BetaMessageNewParams: anthropic.BetaMessageNewParams{ - Model: anthropic.ModelClaudeOpus4_8, + Model: "claude-opus-5-5", MaxTokens: 16000, Messages: []anthropic.BetaMessageParam{ anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("What's the weather in Paris?")), @@ -120,7 +120,7 @@ func main() { for { resp, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{ - Model: anthropic.ModelClaudeSonnet4_6, + Model: "claude-opus-5-5", MaxTokens: 16000, Messages: messages, Tools: tools, @@ -204,11 +204,11 @@ Server-side - no tool_result round-trip. The advisor model must be >= the execut ```go response, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{ - Model: anthropic.ModelClaudeSonnet4_6, + Model: "claude-sonnet-5-5", // executor MaxTokens: 4096, Tools: []anthropic.BetaToolUnionParam{ {OfAdvisorTool20260301: &anthropic.BetaAdvisorTool20260301Param{ - Model: anthropic.ModelClaudeOpus4_8, + Model: "claude-opus-5-5", // advisor }}, }, Messages: []anthropic.BetaMessageParam{ /* ... */ }, diff --git a/skills/claude-api/go/managed-agents/README.md b/skills/claude-api/go/managed-agents/README.md index a8e2c9658..eff43602b 100644 --- a/skills/claude-api/go/managed-agents/README.md +++ b/skills/claude-api/go/managed-agents/README.md @@ -2,7 +2,7 @@ > **Bindings not shown here:** This README covers the most common managed-agents flows for Go. If you need a class, method, namespace, field, or behavior that isn't shown, WebFetch the Go SDK repo **or the relevant docs page** from `shared/live-sources.md` rather than guess. Do not extrapolate from cURL shapes or another language's SDK. -> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `agents.New` and pass it to every subsequent `sessions.New`; do not call `agents.New` in the request path. **Recommended:** define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. +> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `agents.New` and pass it to every subsequent `sessions.New`; do not call `agents.New` in the request path. **Recommended:** define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. ## Installation @@ -65,7 +65,7 @@ fmt.Println(environment.ID) // env_... agent, err := client.Beta.Agents.New(ctx, anthropic.BetaAgentNewParams{ Name: "Coding Assistant", Model: anthropic.BetaManagedAgentsModelConfigParams{ - ID: "claude-opus-5", + ID: "claude-opus-5-5", Type: anthropic.BetaManagedAgentsModelConfigParamsTypeModelConfig, }, System: anthropic.String("You are a helpful coding assistant."), @@ -383,7 +383,7 @@ if err != nil { agent, err := client.Beta.Agents.New(ctx, anthropic.BetaAgentNewParams{ Name: "GitHub Assistant", Model: anthropic.BetaManagedAgentsModelConfigParams{ - ID: "claude-opus-5", + ID: "claude-opus-5-5", Type: anthropic.BetaManagedAgentsModelConfigParamsTypeModelConfig, }, MCPServers: []anthropic.BetaManagedAgentsURLMCPServerParams{{ diff --git a/skills/claude-api/java/claude-api/README.md b/skills/claude-api/java/claude-api/README.md index 87a900865..98f711389 100644 --- a/skills/claude-api/java/claude-api/README.md +++ b/skills/claude-api/java/claude-api/README.md @@ -33,7 +33,7 @@ Write from this table instead of `javap`/jar inspection. Endpoint column tells y | Strict tool use | non-beta | `Tool`, `Tool.InputSchema` | | Task budgets | beta | `.outputConfig(BetaOutputConfig.builder().taskBudget(BetaTokenTaskBudget.builder()...))` | | Tool search | non-beta | `.addTool(ToolSearchToolRegex20251119.builder()...)` from `com.anthropic.models.messages` | -| Web search | non-beta | `WebSearchTool20260209` from `com.anthropic.models.messages` - the latest variant with dynamic filtering (Claude Fable 5.1 + Claude Opus 5 + Opus 4.8/4.7/4.6 + Claude Sonnet 5 + Sonnet 4.6). For older models or Vertex, use `WebSearchTool20250305` | +| Web search | non-beta | `WebSearchTool20260209` from `com.anthropic.models.messages` - the latest variant with dynamic filtering (Claude Fable 5.1 + Claude Opus 5.5 + Claude Opus 5 + Opus 4.8/4.7/4.6 + Claude Sonnet 5.5 + Claude Sonnet 5 + Sonnet 4.6). For older models or Vertex, use `WebSearchTool20250305` | ### Discovering type and member names @@ -81,7 +81,7 @@ import com.anthropic.models.messages.MessageCreateParams; import com.anthropic.models.messages.Message; MessageCreateParams params = MessageCreateParams.builder() - .model("claude-opus-5") // .model(String) overload - use it for ids with no typed Model constant yet + .model("claude-opus-5-5") // .model(String) overload - works for every model id; typed Model.* constants lag model launches .maxTokens(16000L) .addUserMessage("What is the capital of France?") .build(); @@ -98,20 +98,21 @@ response.content().stream() **Adaptive thinking is the recommended mode for Claude 4.6+ models.** Claude decides dynamically when and how much to think. The builder has a direct `.thinking(ThinkingConfigAdaptive)` overload - no manual union wrapping. -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (below). `ThinkingConfigEnabled.builder().budgetTokens(N)` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (below). `ThinkingConfigEnabled.builder().budgetTokens(N)` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `.thinking(...)` (or send `ThinkingConfigAdaptive`, which is equivalent); `ThinkingConfigDisabled` returns a 400 at every effort, as does a thinking budget. Control depth with `.outputConfig(OutputConfig.builder().effort(...))` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `.thinking(...)` runs adaptive (`ThinkingConfigAdaptive` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `ThinkingConfigDisabled` is accepted only at effort `HIGH` or lower; pairing it with `XHIGH`/`MAX` returns a 400. > **Older models:** Use `.thinking(ThinkingConfigEnabled.builder().budgetTokens(N).build())` (budget must be < `maxTokens`, min 1024). ```java import com.anthropic.models.messages.ContentBlock; import com.anthropic.models.messages.MessageCreateParams; -import com.anthropic.models.messages.Model; import com.anthropic.models.messages.ThinkingConfigAdaptive; MessageCreateParams params = MessageCreateParams.builder() - .model(Model.CLAUDE_SONNET_4_6) + .model("claude-opus-5-5") .maxTokens(16000L) - .thinking(ThinkingConfigAdaptive.builder().build()) + // display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 + .thinking(ThinkingConfigAdaptive.builder().display(ThinkingConfigAdaptive.Display.SUMMARIZED).build()) .addUserMessage("Solve this step by step: 27 * 453") .build(); @@ -171,7 +172,7 @@ import com.anthropic.models.messages.MessageCountTokensParams; long tokens = client.messages().countTokens( MessageCountTokensParams.builder() - .model(Model.CLAUDE_SONNET_4_6) + .model("claude-opus-5-5") .addUserMessage("Hello") .build() ).inputTokens(); diff --git a/skills/claude-api/java/claude-api/streaming.md b/skills/claude-api/java/claude-api/streaming.md index fb09ab981..683b15355 100644 --- a/skills/claude-api/java/claude-api/streaming.md +++ b/skills/claude-api/java/claude-api/streaming.md @@ -7,7 +7,7 @@ import com.anthropic.core.http.StreamResponse; import com.anthropic.models.messages.RawMessageStreamEvent; MessageCreateParams params = MessageCreateParams.builder() - .model(Model.CLAUDE_OPUS_4_8) + .model("claude-opus-5-5") .maxTokens(64000L) .addUserMessage("Write a haiku") .build(); diff --git a/skills/claude-api/java/claude-api/tool-use.md b/skills/claude-api/java/claude-api/tool-use.md index d5df6a16c..f233cf1a4 100644 --- a/skills/claude-api/java/claude-api/tool-use.md +++ b/skills/claude-api/java/claude-api/tool-use.md @@ -29,7 +29,7 @@ static class GetWeather implements Supplier { BetaToolRunner toolRunner = client.beta().messages().toolRunner( MessageCreateParams.builder() - .model("claude-opus-5") + .model("claude-opus-5-5") .maxTokens(16000L) .putAdditionalHeader("anthropic-beta", "structured-outputs-2025-11-13") .addTool(GetWeather.class) @@ -57,7 +57,7 @@ import com.anthropic.models.beta.messages.ToolRunnerCreateParams; BetaMemoryToolHandler memoryHandler = new FileSystemMemoryToolHandler(sandboxRoot); MessageCreateParams createParams = MessageCreateParams.builder() - .model("claude-opus-5") + .model("claude-opus-5-5") .maxTokens(4096L) .addTool(BetaMemoryTool20250818.builder().build()) .addUserMessage("Remember that my favorite color is blue") @@ -96,7 +96,7 @@ Tool tool = Tool.builder() .build(); MessageCreateParams params = MessageCreateParams.builder() - .model(Model.CLAUDE_SONNET_4_6) + .model("claude-opus-5-5") .maxTokens(16000L) .addTool(tool) .addUserMessage("Weather in Paris?") @@ -140,7 +140,7 @@ record Book(String title, String author) {} record BookList(List books) {} StructuredMessageCreateParams params = MessageCreateParams.builder() - .model(Model.CLAUDE_SONNET_4_6) + .model("claude-opus-5-5") .maxTokens(16000L) .outputConfig(BookList.class) // returns a typed builder .addUserMessage("List 3 classic novels") @@ -190,7 +190,7 @@ import com.anthropic.models.beta.messages.BetaCodeExecutionTool20260120; import com.anthropic.models.beta.messages.BetaRequestMcpServerUrlDefinition; MessageCreateParams params = MessageCreateParams.builder() - .model(Model.CLAUDE_OPUS_4_8) + .model("claude-opus-5-5") .maxTokens(16000L) .addBeta("mcp-client-2025-11-20") .addTool(BetaToolBash20250124.builder().build()) diff --git a/skills/claude-api/java/managed-agents/README.md b/skills/claude-api/java/managed-agents/README.md index ff1f11598..dcc27ff93 100644 --- a/skills/claude-api/java/managed-agents/README.md +++ b/skills/claude-api/java/managed-agents/README.md @@ -2,7 +2,7 @@ > **Bindings not shown here:** This README covers the most common managed-agents flows for Java. If you need a class, method, namespace, field, or behavior that isn't shown, WebFetch the Java SDK repo **or the relevant docs page** from `shared/live-sources.md` rather than guess. Do not extrapolate from cURL shapes or another language's SDK. -> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `client.beta().agents().create` and pass it to every subsequent `client.beta().sessions().create`; do not call `agents().create` in the request path. **Recommended:** define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. +> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `client.beta().agents().create` and pass it to every subsequent `client.beta().sessions().create`; do not call `agents().create` in the request path. **Recommended:** define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. ## Installation @@ -57,7 +57,7 @@ import com.anthropic.models.beta.sessions.SessionCreateParams; // 1. Create the agent (reusable, versioned) var agent = client.beta().agents().create(AgentCreateParams.builder() .name("Coding Assistant") - .model("claude-opus-5") + .model("claude-opus-5-5") .system("You are a helpful coding assistant.") .addTool(BetaManagedAgentsAgentToolset20260401Params.builder() .type(BetaManagedAgentsAgentToolset20260401Params.Type.AGENT_TOOLSET_20260401) @@ -296,7 +296,7 @@ import com.anthropic.models.beta.agents.BetaManagedAgentsUrlMcpServerParams; // Agent declares MCP server (no auth here - auth goes in a vault) var agent = client.beta().agents().create(AgentCreateParams.builder() .name("GitHub Assistant") - .model("claude-opus-5") + .model("claude-opus-5-5") .addMcpServer(BetaManagedAgentsUrlMcpServerParams.builder() .type(BetaManagedAgentsUrlMcpServerParams.Type.URL) .name("github") diff --git a/skills/claude-api/php/claude-api/README.md b/skills/claude-api/php/claude-api/README.md index 8d8f7d5ca..27f2d69df 100644 --- a/skills/claude-api/php/claude-api/README.md +++ b/skills/claude-api/php/claude-api/README.md @@ -26,7 +26,7 @@ use Anthropic\Bedrock\MantleClient; $client = new MantleClient(awsRegion: 'us-east-1'); ``` -Model IDs on Bedrock take an `anthropic.` prefix - e.g. `model: 'anthropic.claude-opus-5'`. +Model IDs on Bedrock take an `anthropic.` prefix - e.g. `model: 'anthropic.claude-opus-5-5'`. ### Google Vertex AI @@ -58,7 +58,7 @@ $client = Foundry\Client::withCredentials( ```php $message = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, messages: [ ['role' => 'user', 'content' => 'What is the capital of France?'], @@ -97,9 +97,9 @@ foreach ($message->content as $block) { use Anthropic\Messages\ThinkingBlock; $message = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, - thinking: ['type' => 'adaptive', 'display' => 'summarized'], // display opt-in: default is omitted (empty thinking text) on Fable 5 / Mythos 5 / Claude Opus 5 / Opus 4.8 / 4.7 + thinking: ['type' => 'adaptive', 'display' => 'summarized'], // display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 messages: [ ['role' => 'user', 'content' => 'Solve: 27 * 453'], ], @@ -117,7 +117,8 @@ foreach ($message->content as $block) { } ``` -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (above). `['type' => 'enabled', 'budgetTokens' => N]` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking (above). `['type' => 'enabled', 'budgetTokens' => N]` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `thinking:` (or send `['type' => 'adaptive']`, which is equivalent); `['type' => 'disabled']` returns a 400 at every effort, as does a thinking budget. Control depth with the `outputConfig:` effort instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `thinking:` runs adaptive (`['type' => 'adaptive']` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `['type' => 'disabled']` is accepted only at effort `high` or lower; pairing it with `xhigh`/`max` returns a 400. > **Older models:** Use `thinking: ['type' => 'enabled', 'budgetTokens' => N]` (budget must be < `maxTokens`, min 1024). @@ -131,7 +132,7 @@ foreach ($message->content as $block) { ```php $message = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, system: [ ['type' => 'text', 'text' => $longSystemPrompt, 'cacheControl' => ['type' => 'ephemeral']], diff --git a/skills/claude-api/php/claude-api/batches.md b/skills/claude-api/php/claude-api/batches.md index d42350d25..3cd4a818b 100644 --- a/skills/claude-api/php/claude-api/batches.md +++ b/skills/claude-api/php/claude-api/batches.md @@ -4,7 +4,7 @@ ```php $batch = $client->messages->batches->create(requests: [ - ['customId' => 'req-1', 'params' => ['model' => 'claude-opus-5', 'maxTokens' => 1024, 'messages' => [...]]], + ['customId' => 'req-1', 'params' => ['model' => 'claude-opus-5-5', 'maxTokens' => 1024, 'messages' => [...]]], ['customId' => 'req-2', 'params' => [...]], ]); // Poll $client->messages->batches->retrieve($batch->id) until processingStatus === 'ended', diff --git a/skills/claude-api/php/claude-api/streaming.md b/skills/claude-api/php/claude-api/streaming.md index 86577eae4..b0f2cbbc5 100644 --- a/skills/claude-api/php/claude-api/streaming.md +++ b/skills/claude-api/php/claude-api/streaming.md @@ -9,7 +9,7 @@ use Anthropic\Messages\RawContentBlockDeltaEvent; use Anthropic\Messages\TextDelta; $stream = $client->messages->createStream( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 64000, messages: [ ['role' => 'user', 'content' => 'Write a haiku'], diff --git a/skills/claude-api/php/claude-api/tool-use.md b/skills/claude-api/php/claude-api/tool-use.md index 55c5f3ebc..226323079 100644 --- a/skills/claude-api/php/claude-api/tool-use.md +++ b/skills/claude-api/php/claude-api/tool-use.md @@ -31,7 +31,7 @@ $weatherTool = new BetaRunnableTool( $runner = $client->beta->messages->toolRunner( maxTokens: 16000, messages: [['role' => 'user', 'content' => 'What is the weather in Paris?']], - model: 'claude-opus-5', + model: 'claude-opus-5-5', tools: [$weatherTool], ); @@ -68,7 +68,7 @@ $tools = [ $messages = [['role' => 'user', 'content' => 'What is the weather in SF?']]; $response = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, tools: $tools, messages: $messages, @@ -95,7 +95,7 @@ while ($response->stopReason === 'tool_use') { // camelCase property $messages[] = ['role' => 'user', 'content' => $toolResults]; $response = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, tools: $tools, messages: $messages, @@ -139,7 +139,7 @@ class Person implements StructuredOutputModel } $message = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, messages: [['role' => 'user', 'content' => 'Generate a profile for Alice, age 30']], outputConfig: ['format' => Person::class], @@ -155,7 +155,7 @@ Types are inferred from PHP type hints. Use `#[Constrained(description: '...')]` ```php $message = $client->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, messages: [['role' => 'user', 'content' => 'Extract: John (john@co.com), Enterprise plan']], outputConfig: [ @@ -194,7 +194,7 @@ foreach ($message->content as $block) { use Anthropic\Beta\Messages\BetaRequestMCPServerURLDefinition; $response = $client->beta->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, mcpServers: [ BetaRequestMCPServerURLDefinition::with( @@ -211,7 +211,7 @@ $response = $client->beta->messages->create( ```php $response = $client->beta->messages->create( - model: 'claude-opus-5', + model: 'claude-opus-5-5', maxTokens: 16000, outputConfig: ['taskBudget' => ['type' => 'tokens', 'total' => 64000]], tools: [...], @@ -226,7 +226,7 @@ Pass the previous response's `id` on the next request; print the `diagnostics` o ```php $r2 = $client->beta->messages->create( - model: 'claude-opus-5', maxTokens: 1024, + model: 'claude-opus-5-5', maxTokens: 1024, diagnostics: ['previousMessageId' => $r1->id], betas: ['cache-diagnosis-2026-04-07'], messages: [...], diff --git a/skills/claude-api/php/managed-agents/README.md b/skills/claude-api/php/managed-agents/README.md index 13e8969cf..76063fbdd 100644 --- a/skills/claude-api/php/managed-agents/README.md +++ b/skills/claude-api/php/managed-agents/README.md @@ -2,7 +2,7 @@ > **Bindings not shown here:** This README covers the most common managed-agents flows for PHP. If you need a class, method, namespace, field, or behavior that isn't shown, WebFetch the PHP SDK repo **or the relevant docs page** from `shared/live-sources.md` rather than guess. Do not extrapolate from cURL shapes or another language's SDK. -> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `$client->beta->agents->create` and pass it to every subsequent `->sessions->create`; do not call `agents->create` in the request path. **Recommended:** define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. +> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `$client->beta->agents->create` and pass it to every subsequent `->sessions->create`; do not call `agents->create` in the request path. **Recommended:** define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. ## Installation @@ -48,7 +48,7 @@ use Anthropic\Beta\Agents\BetaManagedAgentsAgentToolset20260401Params; // 1. Create the agent (reusable, versioned) $agent = $client->beta->agents->create( name: 'Coding Assistant', - model: 'claude-opus-5', + model: 'claude-opus-5-5', system: 'You are a helpful coding assistant.', tools: [ BetaManagedAgentsAgentToolset20260401Params::with( @@ -307,7 +307,7 @@ use Anthropic\Beta\Sessions\BetaManagedAgentsAgentParams; // Agent declares MCP server (no auth here - auth goes in a vault) $agent = $client->beta->agents->create( name: 'GitHub Assistant', - model: 'claude-opus-5', + model: 'claude-opus-5-5', mcpServers: [ BetaManagedAgentsURLMCPServerParams::with( type: 'url', diff --git a/skills/claude-api/python/claude-api/README.md b/skills/claude-api/python/claude-api/README.md index 862273033..cb611611c 100644 --- a/skills/claude-api/python/claude-api/README.md +++ b/skills/claude-api/python/claude-api/README.md @@ -33,7 +33,7 @@ Use `with_options()` to override client settings for a single call without mutat ```python client.with_options(timeout=5.0, max_retries=5).messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=1024, messages=[{"role": "user", "content": "Hello"}], ) @@ -90,7 +90,7 @@ Set `ANTHROPIC_LOG=debug` (or `info`) to enable SDK logging via the standard `lo ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[ {"role": "user", "content": "What is the capital of France?"} @@ -109,7 +109,7 @@ for block in response.content: ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, system="You are a helpful coding assistant. Always provide examples in Python.", messages=[{"role": "user", "content": "How do I read a JSON file?"}] @@ -145,7 +145,7 @@ with open("image.png", "rb") as f: image_data = base64.standard_b64encode(f.read()).decode("utf-8") response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -168,7 +168,7 @@ response = client.messages.create( ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -198,7 +198,7 @@ Use top-level `cache_control` to automatically cache the last cacheable block in ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, cache_control={"type": "ephemeral"}, # auto-caches the last cacheable block system="You are an expert on this large document...", @@ -212,7 +212,7 @@ For fine-grained control, add `cache_control` to specific content blocks: ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, system=[{ "type": "text", @@ -224,7 +224,7 @@ response = client.messages.create( # With explicit TTL (time-to-live) response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, system=[{ "type": "text", @@ -249,16 +249,17 @@ If `cache_read_input_tokens` is zero across repeated identical-prefix requests, ## Extended Thinking -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `thinking` (or send `{"type": "adaptive"}`, which is equivalent); `{"type": "disabled"}` returns a 400 at every effort, as does a thinking budget. Control depth with `output_config.effort` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `thinking` runs adaptive (`{"type": "adaptive"}` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `{"type": "disabled"}` is accepted only at effort `high` or lower; pairing it with `xhigh`/`max` returns a 400. > **Older models:** Use `thinking: {type: "enabled", budget_tokens: N}` (must be < `max_tokens`, min 1024). ```python -# Fable 5 / Claude Opus 5 / Opus 4.8 / 4.7 / 4.6: adaptive thinking (recommended) +# Fable 5 / Claude Opus 5.5 / Claude Opus 5 / Opus 4.8 / 4.7 / 4.6: adaptive thinking (recommended) response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, - thinking={"type": "adaptive", "display": "summarized"}, # display opt-in: default is omitted (empty thinking text) on Fable 5 / Mythos 5 / Claude Opus 5 / Opus 4.8 / 4.7 + thinking={"type": "adaptive", "display": "summarized"}, # display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 output_config={"effort": "high"}, # low | medium | high | xhigh | max messages=[{"role": "user", "content": "Solve this step by step..."}] ) @@ -317,7 +318,7 @@ To access raw headers or other response metadata, use `.with_raw_response`: ```python raw = client.messages.with_raw_response.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=1024, messages=[{"role": "user", "content": "Hello"}], ) @@ -363,7 +364,7 @@ class ConversationManager: # Usage conversation = ConversationManager( client=anthropic.Anthropic(), - model="claude-opus-5", + model="claude-opus-5-5", system="You are a helpful assistant." ) @@ -381,7 +382,7 @@ response2 = conversation.send("What's my name?") # Claude remembers "Alice" ### Compaction (long conversations) -> **Beta, Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6.** When conversations approach the 200K context window, compaction automatically summarizes earlier context server-side. The API returns a `compaction` block; you must pass it back on subsequent requests - append `response.content`, not just the text. +> **Beta, Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6.** When conversations approach the 200K context window, compaction automatically summarizes earlier context server-side. The API returns a `compaction` block; you must pass it back on subsequent requests - append `response.content`, not just the text. ```python import anthropic @@ -394,7 +395,7 @@ def chat(user_message: str) -> str: response = client.beta.messages.create( betas=["compact-2026-01-12"], - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=messages, context_management={ @@ -476,7 +477,7 @@ A `stop_reason: "refusal"` on the final response means the whole chain refused. ```python # Automatic caching (simplest - caches the last cacheable block) response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, cache_control={"type": "ephemeral"}, system=large_document_text, # e.g., 50KB of context @@ -492,14 +493,14 @@ response = client.messages.create( ```python # Default to Opus for most tasks response = client.messages.create( - model="claude-opus-5", # $5.00/$25.00 per 1M tokens + model="claude-opus-5-5", # $4.00/$20.00 per 1M tokens max_tokens=16000, messages=[{"role": "user", "content": "Explain quantum computing"}] ) # Use Sonnet for high-volume production workloads standard_response = client.messages.create( - model="claude-sonnet-5", # $2.00/$10.00 per 1M tokens + model="claude-sonnet-5-5", # $2.00/$10.00 per 1M tokens max_tokens=16000, messages=[{"role": "user", "content": "Summarize this document"}] ) @@ -516,12 +517,12 @@ simple_response = client.messages.create( ```python count_response = client.messages.count_tokens( - model="claude-opus-5", + model="claude-opus-5-5", messages=messages, system=system ) -estimated_input_cost = count_response.input_tokens * 0.000005 # $5/1M tokens +estimated_input_cost = count_response.input_tokens * 0.000004 # $4/1M tokens print(f"Estimated input cost: ${estimated_input_cost:.4f}") ``` diff --git a/skills/claude-api/python/claude-api/batches.md b/skills/claude-api/python/claude-api/batches.md index 313eec069..b00d9d333 100644 --- a/skills/claude-api/python/claude-api/batches.md +++ b/skills/claude-api/python/claude-api/batches.md @@ -26,7 +26,7 @@ message_batch = client.messages.batches.create( Request( custom_id="request-1", params=MessageCreateParamsNonStreaming( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Summarize climate change impacts"}] ) @@ -34,7 +34,7 @@ message_batch = client.messages.batches.create( Request( custom_id="request-2", params=MessageCreateParamsNonStreaming( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Explain quantum computing basics"}] ) @@ -130,7 +130,7 @@ message_batch = client.messages.batches.create( Request( custom_id=f"analysis-{i}", params=MessageCreateParamsNonStreaming( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, system=shared_system, messages=[{"role": "user", "content": question}] diff --git a/skills/claude-api/python/claude-api/files-api.md b/skills/claude-api/python/claude-api/files-api.md index 1ec884d4c..8c0db2f6b 100644 --- a/skills/claude-api/python/claude-api/files-api.md +++ b/skills/claude-api/python/claude-api/files-api.md @@ -40,7 +40,7 @@ print(f"Size: {uploaded.size_bytes} bytes") ```python response = client.beta.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -69,7 +69,7 @@ image_file = client.beta.files.upload( ) response = client.beta.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -147,7 +147,7 @@ questions = [ for question in questions: response = client.beta.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", diff --git a/skills/claude-api/python/claude-api/sdk-upgrade.md b/skills/claude-api/python/claude-api/sdk-upgrade.md index 94acc244a..4a290fcdb 100644 --- a/skills/claude-api/python/claude-api/sdk-upgrade.md +++ b/skills/claude-api/python/claude-api/sdk-upgrade.md @@ -162,18 +162,18 @@ print(completion.completion) # After message = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=256, messages=[{"role": "user", "content": "Why is the sky blue?"}], ) print("".join(block.text for block in message.content if block.type == "text")) ``` -**[DECIDE] The model.** Code still on Text Completions usually pins a retired model (`claude-2.x`, `claude-instant-*`), which 404s regardless of SDK version. Keep a model that is still served; otherwise switch to `claude-opus-5` so the code runs, say so prominently in the report, and point the user at `/claude-api migrate` for validating prompts against the new model - a completions-era prompt is exactly what `shared/prompt-audit.md` exists for. +**[DECIDE] The model.** Code still on Text Completions usually pins a retired model (`claude-2.x`, `claude-instant-*`), which 404s regardless of SDK version. Keep a model that is still served; otherwise switch to `claude-opus-5-5` so the code runs, say so prominently in the report, and point the user at `/claude-api migrate` for validating prompts against the new model - a completions-era prompt is exactly what `shared/prompt-audit.md` exists for. ## Step 6: Removed request parameters -- **[BREAKS] `temperature`, `top_p`, `top_k`** are no longer accepted by `messages.create()` / `.stream()` / `.parse()`, their `beta.messages` counterparts, or `beta.messages.tool_runner()` (passing them is a `TypeError`), and are gone from the per-request `params` TypedDict of `messages.batches.create()` (a type checker flags the key; at runtime the SDK still forwards it). Delete them - they are gone from the 1.x signatures, not from the API, and whether a model still honours them is a model question (`shared/model-migration.md`): Opus 4.7 and later return a 400 for any request that carries one (the default value included), Claude Sonnet 5 rejects non-default values, and every still-served model before those accepts them - the Claude 4.6 / 4.5 line (Opus 4.6, Sonnet 4.6, Opus 4.5, Sonnet 4.5, Haiku 4.5) and the deprecated-but-still-served Claude 4 models (`shared/models.md` -> Deprecated Models). So **[DECIDE]** when the call pins one of those accepting models and visibly depends on the setting (a documented determinism requirement, an A/B on temperature), move it into `extra_body` instead of deleting it - `extra_body={"temperature": 0.2}` is merged into the request JSON as-is - and for a `messages.batches.create()` request leave the key in that request's `params` dict (it is forwarded, see above). A call that pins a retired model (`shared/models.md` -> Retired Models) is the `migrate` flow's problem first: it needs a replacement model, and the replacement decides whether the setting survives. Say which calls kept a setting this way in the report. When a test existed only to assert that these parameters pass through, keep it meaningful by asserting on parameters that still exist (`stop_sequences`, `metadata`, `service_tier`, `max_tokens`) rather than deleting it. +- **[BREAKS] `temperature`, `top_p`, `top_k`** are no longer accepted by `messages.create()` / `.stream()` / `.parse()`, their `beta.messages` counterparts, or `beta.messages.tool_runner()` (passing them is a `TypeError`), and are gone from the per-request `params` TypedDict of `messages.batches.create()` (a type checker flags the key; at runtime the SDK still forwards it). Delete them - they are gone from the 1.x signatures, not from the API, and whether a model still honours them is a model question (`shared/model-migration.md`): Opus 4.7 and later return a 400 for any request that carries one (the default value included), Claude Sonnet 5.5 and Claude Sonnet 5 reject non-default values, and every still-served model before those accepts them - the Claude 4.6 / 4.5 line (Opus 4.6, Sonnet 4.6, Opus 4.5, Sonnet 4.5, Haiku 4.5) and the deprecated-but-still-served Claude 4 models (`shared/models.md` -> Deprecated Models). So **[DECIDE]** when the call pins one of those accepting models and visibly depends on the setting (a documented determinism requirement, an A/B on temperature), move it into `extra_body` instead of deleting it - `extra_body={"temperature": 0.2}` is merged into the request JSON as-is - and for a `messages.batches.create()` request leave the key in that request's `params` dict (it is forwarded, see above). A call that pins a retired model (`shared/models.md` -> Retired Models) is the `migrate` flow's problem first: it needs a replacement model, and the replacement decides whether the setting survives. Say which calls kept a setting this way in the report. When a test existed only to assert that these parameters pass through, keep it meaningful by asserting on parameters that still exist (`stop_sequences`, `metadata`, `service_tier`, `max_tokens`) rather than deleting it. ```python # Before diff --git a/skills/claude-api/python/claude-api/streaming.md b/skills/claude-api/python/claude-api/streaming.md index 2f2070ff1..69788059f 100644 --- a/skills/claude-api/python/claude-api/streaming.md +++ b/skills/claude-api/python/claude-api/streaming.md @@ -4,7 +4,7 @@ ```python with client.messages.stream( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, messages=[{"role": "user", "content": "Write a story"}] ) as stream: @@ -16,7 +16,7 @@ with client.messages.stream( ```python async with async_client.messages.stream( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, messages=[{"role": "user", "content": "Write a story"}] ) as stream: @@ -30,7 +30,7 @@ async with async_client.messages.stream( ```python for event in client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, messages=[{"role": "user", "content": "Write a story"}], stream=True, @@ -46,13 +46,13 @@ No final-message accumulation is done for you in this form. Claude may return text, thinking blocks, or tool use. Handle each appropriately: -> **Fable 5 / Claude Opus 5 / Opus 4.8 / Opus 4.7 / Opus 4.6:** Use `thinking: {type: "adaptive"}`. On Claude Opus 5 adaptive is also what you get by omitting `thinking` entirely. On older models, use `thinking: {type: "enabled", budget_tokens: N}` instead. +> **Fable 5 / Claude Opus 5.5 / Claude Opus 5 / Opus 4.8 / Opus 4.7 / Opus 4.6:** Use `thinking: {type: "adaptive"}`. On Claude Opus 5.5 and Claude Opus 5 adaptive is also what you get by omitting `thinking` entirely (Claude Opus 5.5 accepts no other setting - `disabled` and `budget_tokens` both 400). On older models, use `thinking: {type: "enabled", budget_tokens: N}` instead. ```python with client.messages.stream( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, - thinking={"type": "adaptive", "display": "summarized"}, # display opt-in: default is omitted (empty thinking text) on Fable 5 / Mythos 5 / Claude Opus 5 / Opus 4.8 / 4.7 + thinking={"type": "adaptive", "display": "summarized"}, # display opt-in: default is omitted (empty thinking text) on Fable 5/5.1, Mythos 5/5.1, Claude Opus 5.5, Claude Opus 5, Opus 4.8/4.7, Claude Sonnet 5.5, and Claude Sonnet 5 messages=[{"role": "user", "content": "Analyze this problem"}] ) as stream: for event in stream: @@ -73,20 +73,96 @@ with client.messages.stream( ## Streaming with Tool Use -The Python tool runner supports streaming: pass `stream=True` to `client.beta.messages.tool_runner(...)` and each iteration yields a stream you consume event-by-event, with `get_final_message()` for the accumulated message per turn (see `shared/tool-use-concepts.md` -> Tool Runner vs Manual Loop). Use the manual-loop pattern below only when you're not using the tool runner and need per-token streaming with tools: +The Python tool runner supports streaming: pass `stream=True` to `client.beta.messages.tool_runner(...)` and each iteration yields a stream you consume event-by-event, with `get_final_message()` for the accumulated message per turn (see `shared/tool-use-concepts.md` -> Tool Runner vs Manual Loop). Declare tool-runner tools with `@beta_tool(eager_input_streaming=True)` so their inputs stream as they are generated (default rule: `shared/tool-use-concepts.md` -> Eager input streaming). The runner never calls your function on unparseable input; the `ValueError` surfaces while you iterate the per-turn stream, so wrap the `for ... in runner` loop and, on failure, restart a new runner from a history you mirror while iterating, in this order for each yielded stream: take `message = stream.get_final_message()`, append it (the assistant turn), then check its `stop_reason` - on `max_tokens` with a `tool_use` present or on `refusal`, stop right there and never call `generate_tool_call_response()` for that turn (it executes the tools) - and only for a turn that continues append `runner.generate_tool_call_response()` (the matching `tool_result` user turn), exactly as `tool-use.md` does to resume `pause_turn`. The Python runner exposes no `params` read, a consumed runner cannot be iterated again, and a history missing the tool-result half of a continued turn is rejected by the API. `pause_turn` you resume yourself; a truncated text answer is simply the final message. + +Use the manual-loop pattern below only when you're not using the tool runner and need per-token streaming with tools. Set `eager_input_streaming: True` on each user-defined tool. With eager streaming the server no longer validates the input: the Python SDK's tolerant parser returns a partial object for a truncated input (check `stop_reason == "max_tokens"`) and can return a silently truncated one for malformed JSON (validate the parsed input before running the tool); only JSON it cannot parse at all raises `ValueError` **from the stream iterator**, so that guard wraps the stream, not the final-message read. Schema validation is not path validation: the model-supplied `path` is untrusted output, so confine it to a project root before writing (`shared/tool-use-concepts.md` -> the text-editor security note): ```python -with client.messages.stream( - model="claude-opus-5", - max_tokens=64000, - tools=tools, - messages=messages -) as stream: - for text in stream.text_stream: - print(text, end="", flush=True) +import json +from pathlib import Path - response = stream.get_final_message() - # Continue with tool execution if response.stop_reason == "tool_use" +ROOT = Path.cwd().resolve() + +tools = [ + { + "name": "write_file", + "description": "Write text to a file at the given path", + "eager_input_streaming": True, # stream large inputs as generated + "input_schema": { + "type": "object", + "properties": { + "path": {"type": "string"}, + "contents": {"type": "string"}, + }, + "required": ["path", "contents"], + }, + } +] + +messages = [{"role": "user", "content": task}] +json_retries = 0 + +while True: + try: + with client.messages.stream( + model="claude-opus-5-5", + max_tokens=64000, + tools=tools, + messages=messages, + ) as stream: + for event in stream: + if event.type == "text": + print(event.text, end="", flush=True) + elif event.type == "input_json": + # Tool input fragment - arrives immediately with eager streaming + print(event.partial_json, end="", flush=True) + response = stream.get_final_message() + json_retries = 0 # the cap is on consecutive failures of one turn + except ValueError: + # JSON the SDK could not parse at all. It raised before the tool_use + # block completed, so there is no tool_use_id to answer; re-issue the + # turn (bounded). API errors are not ValueError and propagate. + json_retries += 1 + if json_retries > 2: + raise + continue + + # Server-side tool hit its iteration limit: append the turn and re-send + if response.stop_reason == "pause_turn": + messages.append({"role": "assistant", "content": response.content}) + continue + + tool_uses = [b for b in response.content if b.type == "tool_use"] + if response.stop_reason == "refusal" or not tool_uses: + # end_turn, a text-only answer, or a refusal (which can cut a + # tool_use off mid-input): nothing to run + break + if response.stop_reason == "max_tokens": + # A truncated tool input parses as a valid partial object; don't run it. + raise RuntimeError("tool input truncated; retry with a higher max_tokens") + + # The SDK's tolerant parser can return a silently truncated or mistyped + # input (for example at an unescaped inner quote), so validate first. + tool_results = [] + for block in tool_uses: + args = block.input + if not (isinstance(args, dict) and isinstance(args.get("path"), str) + and isinstance(args.get("contents"), str)): + tool_results.append({"type": "tool_result", "tool_use_id": block.id, "is_error": True, + "content": json.dumps({"INVALID_JSON": json.dumps(args)})}) + continue + # `path` is untrusted model output: resolve it and reject anything that + # escapes the project root (`..`, absolute paths, symlinks) before the + # write - schema validation alone does not check this. + target = (ROOT / args["path"]).resolve() + if not target.is_relative_to(ROOT): + tool_results.append({"type": "tool_result", "tool_use_id": block.id, "is_error": True, + "content": "path escapes the project root"}) + continue + tool_results.append({"type": "tool_result", "tool_use_id": block.id, + "content": run_tool(block.name, {**args, "path": str(target)})}) + messages.append({"role": "assistant", "content": response.content}) + messages.append({"role": "user", "content": tool_results}) ``` --- @@ -95,7 +171,7 @@ with client.messages.stream( ```python with client.messages.stream( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, messages=[{"role": "user", "content": "Hello"}] ) as stream: @@ -142,7 +218,7 @@ def stream_with_progress(client, **kwargs): ```python try: with client.messages.stream( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=64000, messages=[{"role": "user", "content": "Write a story"}] ) as stream: diff --git a/skills/claude-api/python/claude-api/tool-use.md b/skills/claude-api/python/claude-api/tool-use.md index 9aac895d1..8ef0a676c 100644 --- a/skills/claude-api/python/claude-api/tool-use.md +++ b/skills/claude-api/python/claude-api/tool-use.md @@ -27,7 +27,7 @@ def get_weather(location: str, unit: str = "celsius") -> str: # The tool runner handles the agentic loop automatically runner = client.beta.messages.tool_runner( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=[get_weather], messages=[{"role": "user", "content": "What's the weather in Paris?"}], @@ -60,7 +60,7 @@ max_restarts = 5 # cap pause_turn restarts, mirroring max_continuations advice restarts = 0 while True: runner = client.beta.messages.tool_runner( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=tools, # may mix @beta_tool functions and server-tool definitions messages=messages, @@ -109,7 +109,7 @@ async with stdio_client(StdioServerParameters(command="mcp-server")) as (read, w tools_result = await mcp_client.list_tools() # tool_runner is sync - returns the runner, not a coroutine runner = client.beta.messages.tool_runner( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Use the available tools"}], tools=[async_mcp_tool(t, mcp_client) for t in tools_result.tools], @@ -127,7 +127,7 @@ from anthropic.lib.tools.mcp import mcp_message prompt = await mcp_client.get_prompt(name="my-prompt") response = await client.beta.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[mcp_message(m) for m in prompt.messages], ) @@ -140,7 +140,7 @@ from anthropic.lib.tools.mcp import mcp_resource_to_content resource = await mcp_client.read_resource(uri="file:///path/to/doc.txt") response = await client.beta.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -181,7 +181,7 @@ messages = [{"role": "user", "content": user_input}] # Agentic loop: keep going until Claude stops calling tools while True: response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=messages @@ -228,7 +228,7 @@ final_text = next(b.text for b in response.content if b.type == "text") ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=[{"role": "user", "content": "What's the weather in Paris?"}] @@ -243,7 +243,7 @@ for block in response.content: result = execute_tool(tool_name, tool_input) followup = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=[ @@ -280,7 +280,7 @@ for block in response.content: # Send all results back at once if tool_results: followup = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=[ @@ -308,14 +308,16 @@ tool_result = { ## Tool Choice +`tool_choice` is `{"type": "auto"}` by default. Forcing a call (`{"type": "any"}` or `{"type": "tool", "name": ...}`) returns a 400 on Claude Opus 5.5, Claude Sonnet 5.5, Claude Fable 5.1, and Claude Mythos 5.1; Claude Opus 5, Claude Sonnet 5, and older models accept it. Steer with the prompt instead, and keep the schema guarantee with `strict: true`: + ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, - tools=tools, - tool_choice={"type": "tool", "name": "get_weather"}, # Force specific tool - messages=[{"role": "user", "content": "What's the weather in Paris?"}] + tools=[{**tool, "strict": True} for tool in tools], # schemas must set additionalProperties: false + messages=[{"role": "user", "content": "What's the weather in Paris? Use the get_weather tool."}] ) +# auto does not guarantee a call - check for a tool_use block and re-prompt if none came back ``` --- @@ -330,7 +332,7 @@ import anthropic client = anthropic.Anthropic() response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -357,7 +359,7 @@ uploaded = client.beta.files.upload(file=open("sales_data.csv", "rb")) # 2. Pass to code execution via container_upload block response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -401,7 +403,7 @@ for block in response.content: ```python # First request: set up environment response1 = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Install tabulate and create data.json with sample data"}], tools=[{"type": "code_execution_20260120", "name": "code_execution"}] @@ -413,7 +415,7 @@ container_id = response1.container.id # Second request: reuse the same container response2 = client.messages.create( container=container_id, - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Read data.json and display as a formatted table"}], tools=[{"type": "code_execution_20260120", "name": "code_execution"}] @@ -453,7 +455,7 @@ import anthropic client = anthropic.Anthropic() response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Remember that my preferred language is Python."}], tools=[{"type": "memory_20250818", "name": "memory"}], @@ -479,7 +481,7 @@ memory = MyMemoryTool() # Use with tool runner runner = client.beta.messages.tool_runner( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, tools=[memory], messages=[{"role": "user", "content": "Remember my preferences"}], @@ -514,7 +516,7 @@ class ContactInfo(BaseModel): client = anthropic.Anthropic() response = client.messages.parse( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -533,7 +535,7 @@ print(contact.interests) # ["API", "SDKs"] ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{ "role": "user", @@ -567,7 +569,7 @@ data = json.loads(text) ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Book a flight to Tokyo for 2 passengers on March 15"}], tools=[{ @@ -592,7 +594,7 @@ response = client.messages.create( ```python response = client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=16000, messages=[{"role": "user", "content": "Plan a trip to Paris next month"}], output_config={ diff --git a/skills/claude-api/python/managed-agents/README.md b/skills/claude-api/python/managed-agents/README.md index 825dd5fc1..526eeefe3 100644 --- a/skills/claude-api/python/managed-agents/README.md +++ b/skills/claude-api/python/managed-agents/README.md @@ -2,7 +2,7 @@ > **Bindings not shown here:** This README covers the most common managed-agents flows for Python. If you need a class, method, namespace, field, or behavior that isn't shown, WebFetch the Python SDK repo **or the relevant docs page** from `shared/live-sources.md` rather than guess. Do not extrapolate from cURL shapes or another language's SDK. -> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `agents.create` and pass it to every subsequent `sessions.create`; do not call `agents.create` in the request path. **Recommended:** define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. +> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `agents.create` and pass it to every subsequent `sessions.create`; do not call `agents.create` in the request path. **Recommended:** define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. ## Installation @@ -51,7 +51,7 @@ print(environment.id) # env_... # 1. Create the agent (reusable, versioned) agent = client.beta.agents.create( name="Coding Assistant", - model="claude-opus-5", + model="claude-opus-5-5", tools=[{"type": "agent_toolset_20260401", "default_config": {"enabled": True}}], ) @@ -71,7 +71,7 @@ import os agent = client.beta.agents.create( name="Code Reviewer", - model="claude-opus-5", + model="claude-opus-5-5", system="You are a senior code reviewer.", tools=[ {"type": "agent_toolset_20260401"}, @@ -126,6 +126,34 @@ client.beta.sessions.events.send( --- +## Define an Outcome (default kickoff for deliverables) + +When the session's job is to produce something checkable - an artifact, a report, a PR - kick off with `user.define_outcome` instead of `user.message`: the harness grades each iteration against your rubric and the agent revises until it passes. Send one or the other, never both. See [Outcomes](../../shared/managed-agents-outcomes.md) for the event reference and rubric-writing guidance. + +```python +STARTER_RUBRIC = """# Report rubric - starter, tune the criteria +- Output is a single `report.md` in /mnt/session/outputs/ +- Every claim cites a source URL +- Includes a summary table with one row per competitor +- Prices are current as of the run date and each row says where it was read from +- No placeholder text, TODOs, or empty sections remain +""" + +client.beta.sessions.events.send( + session_id=session.id, + events=[ + { + "type": "user.define_outcome", + "description": "Write a competitor-pricing report as report.md", + "rubric": {"type": "text", "content": STARTER_RUBRIC}, + "max_iterations": 5, # optional; default 3, max 20 + } + ], +) +``` + +--- + ## Stream Events (SSE) ```python @@ -314,7 +342,7 @@ client.beta.sessions.archive(session_id="sesn_011CZxAbc123Def456") # Agent declares MCP server (no auth here - auth goes in a vault) agent = client.beta.agents.create( name="MCP Agent", - model="claude-opus-5", + model="claude-opus-5-5", mcp_servers=[ {"type": "url", "name": "my-tools", "url": "https://my-mcp-server.example.com/sse"}, ], diff --git a/skills/claude-api/ruby/claude-api/README.md b/skills/claude-api/ruby/claude-api/README.md index 0349b0ca0..87024e190 100644 --- a/skills/claude-api/ruby/claude-api/README.md +++ b/skills/claude-api/ruby/claude-api/README.md @@ -26,7 +26,7 @@ client = Anthropic::Client.new(api_key: "your-api-key") ```ruby message = client.messages.create( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 16000, messages: [ { role: "user", content: "What is the capital of France?" } @@ -44,13 +44,14 @@ end ## Extended Thinking -> **Fable 5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Claude Opus 5.5, Claude Opus 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6. +> **Claude Opus 5.5:** thinking is always on - omit `thinking` (or send `{ type: "adaptive" }`, which is equivalent); `{ type: "disabled" }` returns a 400 at every effort, as does a thinking budget. Control depth with `output_config.effort` instead - the default is `medium` on this model, where Claude Opus 5 defaults to `high`. > **Claude Opus 5:** thinking is on by default - omitting `thinking:` runs adaptive (`{ type: "adaptive" }` is equivalent), unlike Opus 4.8/4.7 where omitting it meant no thinking. `{ type: "disabled" }` is accepted only at effort `high` or lower; pairing it with `xhigh`/`max` returns a 400. > **Older models:** Use `thinking: { type: "enabled", budget_tokens: N }` (must be < `max_tokens`, min 1024). ```ruby message = client.messages.create( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 16000, thinking: { type: "adaptive" }, messages: [{ role: "user", content: "Solve: 27 * 453" }] @@ -72,7 +73,7 @@ end ```ruby message = client.messages.create( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 16000, system_: [ { type: "text", text: long_system_prompt, cache_control: { type: "ephemeral" } } @@ -110,7 +111,7 @@ end ```ruby response = client.beta.messages.create( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 16000, output_config: { task_budget: { type: :tokens, total: 64_000 } }, tools: [...], diff --git a/skills/claude-api/ruby/claude-api/streaming.md b/skills/claude-api/ruby/claude-api/streaming.md index fa23fe384..31ca4ea54 100644 --- a/skills/claude-api/ruby/claude-api/streaming.md +++ b/skills/claude-api/ruby/claude-api/streaming.md @@ -4,7 +4,7 @@ ```ruby stream = client.messages.stream( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 64000, messages: [{ role: "user", content: "Write a haiku" }] ) diff --git a/skills/claude-api/ruby/claude-api/tool-use.md b/skills/claude-api/ruby/claude-api/tool-use.md index d23af793c..26db0e2ee 100644 --- a/skills/claude-api/ruby/claude-api/tool-use.md +++ b/skills/claude-api/ruby/claude-api/tool-use.md @@ -24,7 +24,7 @@ class GetWeather < Anthropic::BaseTool end client.beta.messages.tool_runner( - model: :"claude-opus-5", + model: :"claude-opus-5-5", max_tokens: 16000, tools: [GetWeather.new], messages: [{ role: "user", content: "What's the weather in San Francisco?" }] diff --git a/skills/claude-api/ruby/managed-agents/README.md b/skills/claude-api/ruby/managed-agents/README.md index e21d77f1f..ded99d492 100644 --- a/skills/claude-api/ruby/managed-agents/README.md +++ b/skills/claude-api/ruby/managed-agents/README.md @@ -2,7 +2,7 @@ > **Bindings not shown here:** This README covers the most common managed-agents flows for Ruby. If you need a class, method, namespace, field, or behavior that isn't shown, WebFetch the Ruby SDK repo **or the relevant docs page** from `shared/live-sources.md` rather than guess. Do not extrapolate from cURL shapes or another language's SDK. -> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `client.beta.agents.create` and pass it to every subsequent `client.beta.sessions.create`; do not call `agents.create` in the request path. **Recommended:** define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. +> **Agents are persistent - create once, reference by ID.** Store the agent ID returned by `client.beta.agents.create` and pass it to every subsequent `client.beta.sessions.create`; do not call `agents.create` in the request path. **Recommended:** define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md` (its live-docs URL is in `shared/live-sources.md`). The CLI owns the control plane (create/update); your code owns the data plane (sessions with the stored ID). The examples below show in-code creation for when you must provision programmatically; in production the create call belongs in setup, not in the request path. ## Installation @@ -51,7 +51,7 @@ puts "Environment ID: #{environment.id}" # env_... # 1. Create the agent (reusable, versioned) agent = client.beta.agents.create( name: "Coding Assistant", - model: :"claude-opus-5", + model: :"claude-opus-5-5", system_: "You are a helpful coding assistant.", tools: [{type: "agent_toolset_20260401"}] ) @@ -265,7 +265,7 @@ client.beta.sessions.delete(session.id) # Agent declares MCP server (no auth here - auth goes in a vault) agent = client.beta.agents.create( name: "GitHub Assistant", - model: :"claude-opus-5", + model: :"claude-opus-5-5", mcp_servers: [ { type: "url", diff --git a/skills/claude-api/shared/admin-api.md b/skills/claude-api/shared/admin-api.md index 66a15a74b..a688f7b5c 100644 --- a/skills/claude-api/shared/admin-api.md +++ b/skills/claude-api/shared/admin-api.md @@ -120,7 +120,7 @@ client.beta.organization.workspaces.members.add( client.beta.organization.api_keys.update("apikey_...", status="inactive", name="New Key Name") # Rate limit reports (optional filters: model=..., group_type=...) -client.beta.organization.rate_limits.list(model="claude-opus-5") +client.beta.organization.rate_limits.list(model="claude-opus-5-5") client.beta.organization.workspaces.rate_limits.list("wrkspc_...") # Service accounts + WIF (org:admin OAuth token required) diff --git a/skills/claude-api/shared/anthropic-cli.md b/skills/claude-api/shared/anthropic-cli.md index 8eea1e5ec..5b79371e5 100644 --- a/skills/claude-api/shared/anthropic-cli.md +++ b/skills/claude-api/shared/anthropic-cli.md @@ -4,14 +4,14 @@ The `ant` CLI exposes every Claude API resource as a shell subcommand. Compared ## When to use the CLI vs the SDK -**CLI for the control plane, SDK for the data plane.** Agents and environments are relatively static resources you define, configure, and debug with `ant` - check the YAML into your repo, apply from CI, inspect from a terminal. Sessions are dynamic and driven by your application through the SDK - create per task, stream events, react to tool calls, integrate into your product. Both hit the same API; the split is about where the call lives, not what's possible. +**CLI for the control plane, SDK for the data plane.** Agents and environments are relatively static resources you define, configure, and debug with `ant` - keep them as files in your repo, sync them with `ant apply` (by hand or from CI), inspect from a terminal. Sessions are dynamic and driven by your application through the SDK - create per task, stream events, react to tool calls, integrate into your product. Both hit the same API; the split is about where the call lives, not what's possible. | | Control plane -> `ant` | Data plane -> SDK | |---|---|---| | Resources | agents, environments, skills, vaults, files | sessions, events | | Cadence | Once per deploy / ad-hoc | Every task / every turn | -| Lives in | `*.yaml` in your repo + CI + terminal | Application code | -| Typical calls | `create < agent.yaml`, `update --version N`, `list`, `retrieve`, `archive`, `--debug` | `sessions.create()`, `events.stream()`, `events.send()` | +| Lives in | `agents/`, `environments/`, `claude-lock.json` in your repo + CI + terminal | Application code | +| Typical calls | `ant apply`, `list`, `retrieve`, `archive`, `--debug` | `sessions.create()`, `events.stream()`, `events.send()` | ## Install and auth @@ -24,7 +24,7 @@ xattr -d com.apple.quarantine "$(brew --prefix)/bin/ant" curl -fsSL "https://github.com/anthropics/anthropic-cli/releases/download/v${VERSION}/ant_${VERSION}_$(uname -s | tr A-Z a-z)_$(uname -m | sed -e s/x86_64/amd64/ -e s/aarch64/arm64/).tar.gz" \ | sudo tar -xz -C /usr/local/bin ant -# Or from source (Go 1.22+) +# Or from source (Go 1.25+) go install github.com/anthropics/anthropic-cli/cmd/ant@latest ``` @@ -60,7 +60,7 @@ curl https://api.anthropic.com/v1/messages \ -H "anthropic-version: 2023-06-01" \ -H "anthropic-beta: oauth-2025-04-20" \ -H "content-type: application/json" \ - -d '{"model": "claude-opus-5", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}' + -d '{"model": "claude-opus-5-5", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}' # .env format - sets ANTHROPIC_AUTH_TOKEN (and ANTHROPIC_BASE_URL if the profile has one). # Output is bare KEY=value (no `export`), so use `set -a` to auto-export for child processes: @@ -82,7 +82,7 @@ Beta resources (agents, sessions, environments, deployments, skills, vaults, mem ```sh ant models list -ant messages create --model claude-opus-5 --max-tokens 1024 --message '{role: user, content: "Hello"}' +ant messages create --model claude-opus-5-5 --max-tokens 1024 --message '{role: user, content: "Hello"}' ant beta:agents retrieve --agent-id agent_01... ant beta:sessions:events list --session-id session_01... ``` @@ -112,7 +112,7 @@ ant beta:agents list --transform '{id,name,model}' --format jsonl **Extract a scalar for shell use:** pair `--transform` with `-r` (`--raw-output` - prints strings unquoted, jq-style): ```sh -AGENT_ID=$(ant beta:agents create --name "My Agent" --model '{id: claude-sonnet-5}' \ +AGENT_ID=$(ant beta:agents create --name "My Agent" --model '{id: claude-sonnet-5-5}' \ --transform id -r) ``` @@ -123,7 +123,7 @@ AGENT_ID=$(ant beta:agents create --name "My Agent" --model '{id: claude-sonnet- ```sh ant beta:agents create \ --name "Research Agent" \ - --model '{id: claude-opus-5}' \ + --model '{id: claude-opus-5-5}' \ --tool '{type: agent_toolset_20260401}' \ --tool '{type: custom, name: search_docs, input_schema: {type: object, properties: {query: {type: string}}}}' ``` @@ -133,7 +133,7 @@ ant beta:agents create \ ```sh ant beta:agents create <<'YAML' name: Research Agent -model: claude-opus-5 +model: claude-opus-5-5 system: | You are a research assistant. Cite sources for every claim. tools: @@ -144,9 +144,9 @@ YAML **`@file` references** - inline a file's contents into any string-valued field. Inside structured flag values, quote the path. Binary files are auto-base64'd; force with `@file://` (text) or `@data://` (base64). Escape a literal leading `@` as `\@`. ```sh -ant beta:agents create --name "Researcher" --model '{id: claude-sonnet-5}' --system @./prompts/researcher.txt +ant beta:agents create --name "Researcher" --model '{id: claude-sonnet-5-5}' --system @./prompts/researcher.txt -ant messages create --model claude-opus-5 --max-tokens 1024 \ +ant messages create --model claude-opus-5-5 --max-tokens 1024 \ --message '{role: user, content: [ {type: document, source: {type: base64, media_type: application/pdf, data: "@./scan.pdf"}}, {type: text, text: "Extract the text from this scanned document."} @@ -156,32 +156,59 @@ ant messages create --model claude-opus-5 --max-tokens 1024 \ Flags that natively take a file path (e.g. `--file` on `beta:files upload`) accept a bare path without `@`. -## Version-controlled Managed Agents resources +## Version-controlled Managed Agents resources (`ant apply`) -This is the recommended flow for defining agents and environments - check the YAML into your repo and sync via `create` (first time) / `update` (thereafter). See `shared/managed-agents-core.md` for the field reference. +This is the recommended flow for defining agents, environments, skills, memory stores and deployments: one file (or skill directory) per resource in your repo, synced with `ant apply` (needs `ant` 1.30.0 or later - check `ant --version`). It prints a plan, creates or updates what differs, and records each resource's ID in `claude-lock.json`. See `shared/managed-agents-core.md` for the field reference, and the `ant apply` page in `shared/live-sources.md` for `--force`, `--prune`, `--lock-file`, renamed or deleted files and CI setup (written for a person at a terminal; the rules below still apply). -```yaml -# summarizer.agent.yaml +``` +agents/summarizer.md # YAML frontmatter = agent config, Markdown body = system prompt +environments/cloud.yaml # the environment create body +skills/pr-summary/SKILL.md # a skill is a directory with SKILL.md at its root +memory_stores/notes.yaml +deployments/nightly.md # frontmatter = deployment create body, Markdown body = the message that starts each run +claude-lock.json # written by ant apply - commit it +``` + +```markdown +--- +# agents/summarizer.md name: Summarizer -model: claude-sonnet-5 -system: | - You are a helpful assistant that writes concise summaries. +model: claude-sonnet-5-5 tools: - type: agent_toolset_20260401 +--- + +You are a helpful assistant that writes concise summaries. +``` + +```yaml +# environments/cloud.yaml +name: summarizer-env +config: {type: cloud, networking: {type: unrestricted}} ``` ```sh -# Create (once) - capture the ID -AGENT_ID=$(ant beta:agents create < summarizer.agent.yaml --transform id -r) - -# Update (CI) - needs ID + current version (optimistic lock) -ant beta:agents update --agent-id "$AGENT_ID" --version 1 < summarizer.agent.yaml +ant apply --dry-run -v agents/summarizer.md environments/cloud.yaml # print the plan with every field, change nothing +ant apply agents/summarizer.md environments/cloud.yaml # print the plan, then ask (y)es / (n)o / (d)etails - needs a terminal +ant apply # later: reconcile every file claude-lock.json already tracks ``` -Same pattern for environments (`ant beta:environments create|update < env.yaml`), then start a session with both IDs: +- **Name the files you wrote; pass `.` or a directory only when the user asks for the whole tree.** A directory is walked to any depth and everything that looks like a resource is applied: any file that has a top-level `type:`, sits directly in `agents/`, `environments/`, `memory_stores/` or `deployments/`, or is named after one of them (`environment_staging.yaml`), plus any directory holding a `SKILL.md`. Claude Code plugins, conda (`environment.yml`) and Kubernetes (`deployments/`) use the same names, and a cloned repo can hold files its user never read. +- **Without a terminal (a coding agent's shell), `ant apply` prints the plan and exits; it applies only with `--yes`.** If you are a coding agent running this for a user, that flag is their approval, not yours: show them the dry-run plan and add `--yes` (or answer the prompt) only once they say go ahead. The plan also covers whatever `claude-lock.json` already tracks: if it would create or change anything you did not write, or a file you did not write sits at a path you need, stop and ask; never add `--force` or `--prune` on your own. +- **Reference other resources by path, not ID** (relative to the file that names it): `skills: [../skills/pr-summary]` on an agent; `agent: ../agents/summarizer.md` and `environment_id: ../environments/cloud.yaml` on a deployment. `ant apply` also applies whatever the files you pass reference, in dependency order, and fills in the IDs. For a resource these files don't manage, write its ID (`agent_01...`, `env_01...`); anything else is sent as written. +- **Commit `claude-lock.json`** (the first run writes it where you run the command - use the repo root). The next run uses it to update the same resources instead of creating duplicates. A resource created any other way (Console, `ant beta:agents create`, an SDK) cannot be adopted: a file describing it creates a second one. +- **To change a resource, edit its file and run `ant apply` again** (an agent gets a new version; whatever references it is updated in the same run). +- **CI in the user's own repository:** run from the directory that holds `claude-lock.json` (normally the repo root) and name the resource directories the project has, not `.` (a walk of `.` also applies look-alike files elsewhere in the repo): `ant apply --dry-run agents environments` on pull requests, `ant apply --yes agents environments` only on push to the default branch (there the merge is the approval), then commit `claude-lock.json`. +- **Not managed:** vaults and credentials (`ant beta:vaults`, `ant beta:vaults:credentials`, or an SDK), uploaded files, sessions. + +**One-off provisioning** can still use `ant beta:agents create <<'YAML'` (see Input above) and `ant beta:agents update --agent-id ... --version N`; you keep track of the IDs yourself. + +Start a session with the IDs from `claude-lock.json` (each `resources` key is the file's path as the plan prints it): ```sh -ant beta:sessions create --agent "$AGENT_ID" --environment-id "$ENV_ID" --title "Task" +AGENT_ID=$(jq -r '.resources["./agents/summarizer.md"].id' claude-lock.json) +ENV_ID=$(jq -r '.resources["./environments/cloud.yaml"].id' claude-lock.json) +SID=$(ant beta:sessions create --agent "$AGENT_ID" --environment-id "$ENV_ID" --title "Task" --transform id -r) ant beta:sessions:events send --session-id "$SID" \ --event '{type: user.message, content: [{type: text, text: "Summarize X"}]}' ant beta:sessions:events list --session-id "$SID" --transform 'content.0.text' -r diff --git a/skills/claude-api/shared/claude-platform-on-aws.md b/skills/claude-api/shared/claude-platform-on-aws.md index 124b22d04..f2a9f84f2 100644 --- a/skills/claude-api/shared/claude-platform-on-aws.md +++ b/skills/claude-api/shared/claude-platform-on-aws.md @@ -1,6 +1,6 @@ # Claude Platform on AWS -**Anthropic-operated** access to the Claude Developer Platform through AWS infrastructure - SigV4 authentication, AWS IAM access control, and AWS Marketplace billing. Because Anthropic operates it, **the API surface matches first-party with same-day parity** - for per-feature exceptions, see `shared/platform-availability.md` (the single source of truth; do not rely on an inline exception list here). Model IDs are the bare first-party strings (`claude-opus-5`, `claude-sonnet-5`) - **no provider prefix**. +**Anthropic-operated** access to the Claude Developer Platform through AWS infrastructure - SigV4 authentication, AWS IAM access control, and AWS Marketplace billing. Because Anthropic operates it, **the API surface matches first-party with same-day parity** - for per-feature exceptions, see `shared/platform-availability.md` (the single source of truth; do not rely on an inline exception list here). Model IDs are the bare first-party strings (`claude-opus-5-5`, `claude-sonnet-5-5`) - **no provider prefix**. > **Not the same as Amazon Bedrock.** Bedrock is partner-operated (AWS runs the service; release schedules vary, feature subset, `anthropic.`-prefixed model IDs). Claude Platform on AWS and Bedrock coexist; pick by whether you need AWS-native IAM/billing with full Anthropic API parity (this page) vs. Bedrock's own ecosystem. @@ -25,7 +25,7 @@ from anthropic import AnthropicAWS client = AnthropicAWS() # region + workspace_id from env; see below client.messages.create( - model="claude-opus-5", + model="claude-opus-5-5", max_tokens=1024, messages=[{"role": "user", "content": "Hello"}], ) @@ -55,5 +55,5 @@ The client resolves AWS credentials via the standard precedence chain: explicit ## What to tell users - Treat it as first-party: every section of this skill applies unchanged. Do **not** apply Bedrock's feature-availability mask. Three Managed Agents differences only: (1) a session can run autonomously (no user events) for at most **6 hours** before it needs reauthentication - send any user-role event to continue; (2) sessions on **self-hosted** environments **cannot attach memory stores** (rejected at session create) - cloud environments attach them as usual; (3) self-hosted workers authenticate with IAM/SigV4 or an AWS-Console API key plus the `AnthropicSelfHostedEnvironmentAccess` managed policy - Console-generated environment keys don't work against the AWS endpoint. -- Model IDs are bare (`claude-opus-5`). Do **not** add an `anthropic.` prefix. +- Model IDs are bare (`claude-opus-5-5`). Do **not** add an `anthropic.` prefix. - A missing region or `workspace_id` throws at client-construction time (no request is sent). A **403** means the request reached the server - check for a **wrong** `workspace_id` or a missing IAM action on the principal. See the IAM actions reference in `shared/live-sources.md`. diff --git a/skills/claude-api/shared/cost-optimization.md b/skills/claude-api/shared/cost-optimization.md index 01d3a8324..06ee0d4de 100644 --- a/skills/claude-api/shared/cost-optimization.md +++ b/skills/claude-api/shared/cost-optimization.md @@ -11,7 +11,7 @@ The levers divide into two kinds, and the order of the steps is load-bearing: **Where this workflow sits**: the `prompt-audit` subcommand (`shared/prompt-audit.md`) audits the prompt surface (prompts, skills, tool descriptions) alone; this workflow is the holistic cost pass - request shape, caching, loop structure, output, batching, effort, model - and runs that audit as one sub-lever of input hygiene (§ 2.2) rather than restating its patterns; and once the project has an eval, the levers become a hillclimb - one change at a time against the eval, keep or revert (Step 3). -Measured expectations quoted below are snapshots of Anthropic's published runs (sources at the end). They are directional, not guarantees - the validation loop in Step 3 is what makes a number true for this project - and both sources are fetched live - the platform guide through `shared/live-sources.md`, the cookbook at its URL in the Sources section below: wherever a fetched page differs from this snapshot, the page wins. +Measured expectations below give the direction and rough size of each effect from Anthropic's published runs (sources at the end); the per-model figures live on those pages and change with each model release, so most are not restated here. They are directional, not guarantees - the validation loop in Step 3 is what makes a number true for this project - and fetching the Pricing and Cost Optimization pages is a required step, not background (Step 0 -> Fetch before you size). For measured figures, wherever a fetched page differs from what is quoted here, the page wins. For which history edits are valid, the Preserved thinking page governs (§ 2.1); the cost guide's and the cookbook's client-side prune recipes do not account for it. --- @@ -22,10 +22,12 @@ Measured expectations quoted below are snapshots of Anthropic's published runs ( 1. **Scope.** If the request names files or directories, that is the scope. Otherwise it is every place the project calls the Claude API - request builders, agent loops, batch jobs. Note distinct traffic classes (an interactive path and a nightly job are different workloads even on one key): the profile, the ranking, and every validation later run per class, and "cost per task" means nothing blended across classes. **Also establish which platform** the code targets (first-party Anthropic API, Claude Platform on AWS, Bedrock, Vertex, or Foundry) - feature availability varies, and it filters which levers are even on the table. 2. **Quality bar.** Find the project's eval, test suite, or outcome checks for its LLM calls. If none exists, say so prominently in the report: without one, savings cannot be told apart from regressions. Do not stop - free wins are safe to propose regardless - but mark every tradeoff lever "needs an eval before applying", and ask the user what outcome check they can provide. An eval only validates the traffic class it covers: mark levers on uncovered paths the same way. If the only check is the user's own manual review, it gates free wins - it never clears a tradeoff. The full no-eval endgame - including a minimal eval recipe that unblocks tradeoffs - is in Step 3. 3. **Baseline cost per task.** The baseline is whatever honest number is cheapest to obtain, in this order: - - **From history, free**: with Admin API access, pull Step 1's usage and cost reports forward and compute the baseline from them - the reports supply the dollars, but the per-task denominator must come from the user or the application's own logs; or roll up the application's own logged `usage` objects per task, not per request - four token counts, each at its own rate: regular input, cache writes (1.25x input for the 5-minute duration, 2x for 1-hour), cache reads (0.1x input), and output - multiplier structure as published on the pricing page; confirm it when you fetch the rates. + - **From history, free**: with Admin API access, pull Step 1's usage and cost reports forward and compute the baseline from them - the reports supply the dollars, but the per-task denominator must come from the user or the application's own logs; or roll up the application's own logged `usage` objects per task, not per request - four token counts, each at its own rate: regular input, cache writes (1.25x input for the 5-minute duration, 2x for 1-hour), cache reads (a fraction of base input that differs by model), and output - multiplier structure as published on the pricing page; take the values from it when you fetch the rates, not from memory. - **From a baseline run, paid**: run the project's eval (or, with no eval, replay a representative sample of real requests) and roll up the same way. This spends real API money: state the expected cost - from Step 1's token estimates and live pricing, and "estimated - pending Step 1" is an acceptable first answer - **and get the user's approval before running it.** If the user declines the spend, estimate the baseline from the code and any bill figure they can read off the Console, label it an estimate, and continue. - For current per-model rates, WebFetch the **Pricing** URL from `shared/live-sources.md` - prices change; do not quote remembered ones (if the pricing fetch fails, effective realized rates come from dividing cost-report amounts by the usage report's matching token counts - same model, same token type). For counting tokens in prompts and files, see `shared/token-counting.md` (`count_tokens` returns the count without running inference). Sanity-check an estimated baseline against any known monthly bill: divergence usually means multi-turn history growth the single-turn estimate missed. + **Fetch before you size - this gates every rate and every measured figure in the audit.** Before any dollar amount, multiplier, or published figure is written into the report or into code, WebFetch two rows from `shared/live-sources.md`: **Pricing** (per-model rates and multipliers) and **Cost Optimization** (current measured expectations and the starting-model recommendation). Fetch both in one turn. When a lever that edits conversation history, `system`, or `tools` (§ 2.2, § 2.3, a model switch) enters the shortlist, also fetch the Preserved thinking page (URL in § 2.1) before proposing its diff, for which models and accounts run the check. Record in the report which pages were fetched and when, so a reader can see it happened, and cite the fetched page beside each figure taken from it. Remembered rates or figures, for any model, are not a source. If a fetch fails, say so in the report. For rates: with Admin API access, derive effective realized rates by dividing cost-report amounts by the usage report's matching token counts (same model, same token type); otherwise ask the user for the current rates. For measured expectations: size them in relative buckets with no figures. Only if none of these is available and the user still wants a number may one appear, and then labeled "unverified - from memory" every place it appears. + + For counting tokens in prompts and files, see `shared/token-counting.md` (`count_tokens` returns the count without running inference). Sanity-check an estimated baseline against any known monthly bill: divergence usually means multi-turn history growth the single-turn estimate missed. ## Step 1: Profile where the tokens go @@ -38,13 +40,13 @@ If the user has an **Admin API key** (`sk-ant-admin01-...` - a different key typ - **Token profile**: `GET /v1/organizations/usage_report/messages` with `group_by[]=model` and `bucket_width=1d` (the default page is 7 daily buckets - raise `limit`, up to 31; the `group_by` dimensions also include `api_key_id`, `workspace_id`, `service_tier`, and `context_window`, among others). Each result splits into exactly the quantities the levers below act on: `uncached_input_tokens`, `cache_read_input_tokens`, `cache_creation.ephemeral_5m_input_tokens` / `ephemeral_1h_input_tokens`, and `output_tokens`. - **Dollar profile**: `GET /v1/organizations/cost_report` (daily granularity, USD as decimal strings in cents) with `group_by[]=description`; description-grouped results carry structured `model`, `cost_type`, `token_type`, and `service_tier` fields - `token_type` makes the cache split readable directly in dollars. Code execution appears under a `Code Execution Usage` description; Priority Tier costs are not included in this endpoint - track those through the usage endpoint's `service_tier` dimension. - Data appears within about 5 minutes of a request completing; poll at most once per minute for sustained use. -- Caveats by platform: Claude Enterprise (claude.ai) organizations use the Analytics API instead, and the endpoints are not currently available on Claude Platform on AWS - there, ask the user to read the totals off the Console's Usage and Cost pages and relay them. +- Caveats by platform: Claude Enterprise (claude.ai) organizations use the Analytics API instead, and the endpoints are not currently available on Claude Platform on AWS - there, ask the user to read the totals off the Console's Usage and Cost pages and relay them. The Usage and Cost page does not list Amazon Bedrock, Google Cloud, or Microsoft Foundry traffic: if the code targets one of those, do not assume these reports cover it - ask whether a Console organization and Admin key exist for that traffic, and otherwise profile from the application's logged `usage` objects or the platform's own billing view. The measured profile answers directly: the real cache hit rate (`cache_read_input_tokens` against uncached input), how much traffic already rides the batch tier, the input/output balance, and where spend concentrates by model, key, and workspace. **Check that the measured footprint plausibly matches the audited code** (same models, a believable order of magnitude): the report covers the whole organization, and a key shared across projects blends their traffic - making per-project reads, including Step 3's post-cutover confirmation, unattributable. On a mismatch, reconcile against the code estimate, scope usage-report queries by `api_key_ids[]` / `workspace_ids[]` where the separation exists (the cost report takes neither filter - it segments only by workspace, via `group_by`), and recommend per-project keys or workspaces as a measurement prerequisite where it doesn't. Optimization effort follows the audited scope's spend, not the org blend. ### Estimate it from the code -Without Admin API access (no Admin key, a Claude Enterprise organization, or Claude Platform on AWS - whose feature availability `shared/claude-platform-on-aws.md` covers) - and even with it, for the structural facts no usage report can show - read the request-building code: +Without Admin API access (no Admin key, a Claude Enterprise organization, Claude Platform on AWS - whose feature availability `shared/claude-platform-on-aws.md` covers - or another cloud platform these reports do not cover) - and even with it, for the structural facts no usage report can show - read the request-building code: > **Per-model defaults, parameter support, and per-platform feature availability change across releases.** For any "what happens when `thinking`/`effort` is omitted", "does this model accept `effort`", "what levels does it support", or "is this feature available on Bedrock/Vertex/Foundry" question, read the answer from SKILL.md -> Thinking & Effort, `shared/models.md`, or `shared/platform-availability.md` (or the live Models API) - never assume, and never encode the answer in this guide. @@ -54,13 +56,14 @@ Without Admin API access (no Admin key, a Claude Enterprise organization, or Cla - **Loop**: how many turns deep, and do bulky tool results accumulate across them? - **Media**: are images, PDFs, or large files entering the context at full size? - **Output**: how long are visible responses, and what is `max_tokens` set to? -- **Model and effort**: which model, which effort, and was either ever swept against an eval? Look up what the model does when both are omitted (SKILL.md -> Thinking & Effort) - an unset default that runs thinking is a hidden output-token line item. +- **Model and effort**: which model, which effort, and was either ever swept against an eval? Look up what the model does when both are omitted (SKILL.md -> Thinking & Effort) - an unset default that runs thinking is a hidden output-token line item, and because default effort differs by model, an unset `effort` can run a level higher or lower after a model change. Note too whether the model is a generation or two behind the current one in its tier - moving up is a lever (§ 2.7). - **Caching**: are there `cache_control` breakpoints already, and what do `cache_read_input_tokens` / `cache_creation_input_tokens` show in practice? - **Latency tolerance**: is a user waiting on every response, or can some work batch? +- **Price modifiers**: does any request set `speed`, `inference_geo`, or another parameter billed at a premium over the standard rate (rates: the Pricing URL in `shared/live-sources.md`)? If a meaningful share of responses end in `stop_reason: "refusal"`, size that as its own line item - some refusals are billed (`shared/model-migration.md` -> `refusal` stop reason). The token counts alone show neither. ### Ask for the app's own usage logs first -Before ranking on estimates, **ask the user whether the application already logs `response.usage` per request** - and if so, to paste a representative day's worth. That turns cache hit rate, the input/output split, and thinking-token spend from guesses into measurements at zero API cost, and it decides which tier of the ranking table below applies. If the app doesn't log usage yet, note that adding it is itself a free-win diff (Step 3) and proceed on the code estimate. +Before ranking on estimates, **ask the user whether the application already logs `response.usage` per request** - and if so, to paste a representative day's worth. That turns cache hit rate, the input/output split, and thinking-token spend from guesses into measurements at zero API cost, and it decides which tier of the ranking table below applies. Two fields sharpen it where the response carries them: `usage.output_tokens_details.thinking_tokens` meters thinking spend directly, and a request that runs a server-side step such as compaction itemizes that step's tokens under `usage.iterations` - sum the iterations rather than reading the top-level counts alone. If the app doesn't log usage yet, note that adding it is itself a free-win diff (Step 3) and proceed on the code estimate. **Estimating cache hit rate without usage data.** If the app logs request timestamps, simulate the TTL walk: sort timestamps, count a hit whenever the gap to the previous request is <= TTL (reads refresh the entry), and run it for each cache TTL the platform offers (see `shared/prompt-caching.md`) - the difference between durations is the longer-TTL lever's ceiling on the user's real traffic. If only aggregate volume is known, approximate with Poisson arrivals: hit rate ~ `1 - e^(-lambda·TTL)` where lambda is requests per second. Either beats comparing average gap to TTL, which ignores burstiness. @@ -76,12 +79,12 @@ Before touching code, size each lever the profile makes applicable so the shortl **Before sizing, drop any lever the target platform doesn't support** (`shared/platform-availability.md` is the single source of truth - do not assume 1P availability carries to Bedrock, Vertex, Foundry, or Claude Platform on AWS). A lever that can't ship on the user's platform isn't worth ranking; list it under "skipped" with the availability reason instead. -Within whichever unit applies, size each lever from the measured (or estimated) spend components and the measured expectations quoted in Step 2 - for example: +Within whichever unit applies, size each lever from the measured (or estimated) spend components and the measured expectations described in Step 2 and quantified on the Cost Optimization page - use the copy fetched in Step 0 for any per-model figure (if that fetch failed, carry the ceiling as a relative bucket) - for example: -- **Caching ceiling**: the spend on input that is shared and byte-stable across requests - the would-be prefix - re-billed at 0.1x. (0.025x on Claude Fable 5.1 - whether Claude Mythos 5.1 shares that rate is open at launch - so its cost per task sits at or under the Claude Fable 5 figures quoted below.) Blend the measured `uncached_input_tokens` with the code profile here: unique per-request payload can never cache, so on a workload that is mostly payload (or already well cached) this ceiling is honestly small. Sanity-bound the result against the published agent-loop range (a factor of 2.5 to 3.7 off at 81% to 90% hit rates). +- **Caching ceiling**: the spend on input that is shared and byte-stable across requests - the would-be prefix - re-billed at the cache-read rate of the model in use - a fraction of base input that differs by model (the Pricing URL in `shared/live-sources.md`; `shared/prompt-caching.md` § API reference carries the break-even arithmetic). Blend the measured `uncached_input_tokens` with the code profile here: unique per-request payload can never cache, so on a workload that is mostly payload (or already well cached) this ceiling is honestly small. Sanity-bound the result against the published agent-loop range (a several-fold reduction at high hit rates - current figures: the Cost Optimization URL in `shared/live-sources.md`). - **Batch ceiling**: 50% of the spend on standard-tier traffic that no one is waiting on. The model-grouped profile cannot see that split - segment first: group by `service_tier` to find what already batches, use a finer `bucket_width` to spot scheduled spikes, and ask the user which traffic can wait. - **Input-hygiene ceiling**: the share of input spend going to reference material, tool schemas, or oversized media that the § 2.2 levers would remove or defer. -- **Effort/model ceiling**: the published tradeoff curves applied to the biggest spend concentrations - carried as a range, since the quality cost is unknown until the eval runs. +- **Effort/model ceiling**: the published tradeoff curves (the Cost Optimization page) applied to the biggest spend concentrations - carried as a range, since the quality cost is unknown until the eval runs. Ceilings that claim the same tokens (caching an inlined document versus deleting it) are mutually exclusive: compute each ceiling unconditionally, rank, then deflate each for its overlap with the levers above it, so the shortlist can never sum past the bill. @@ -91,19 +94,23 @@ Present the ranked shortlist with the profile evidence behind each number - labe Free wins may be applied directly when the request asked for edits (a bare subcommand invocation has not asked - propose). Tradeoff levers (2.6 onward) are always presented with their measured quality cost and applied only on the user's explicit acceptance - never trade accuracy for cost silently. And every run that exercises the model - the baseline, each lever's validation pass - spends real API money: get explicit approval before each one, with the expected cost, or once as a Step 3 measurement budget that covers them. -Pricing multipliers quoted below (cache read/write rates, batch discount) are current as of writing - confirm against the Pricing URL in `shared/live-sources.md` before computing any ceiling. +Pricing multipliers quoted below (cache write rates, batch discount) are current as of writing - confirm against the Pricing URL in `shared/live-sources.md` before computing any ceiling. The cache-read rate differs by model and is deliberately not quoted here. ### 2.1 Prompt caching - first, and it stays on -Every turn of an agentic task resends the entire growing conversation - system prompt, tool definitions, every prior turn - so a 40-turn task sends its first turn 40 times and task cost grows with roughly the square of turn count. Caching does not stop the resending; it reprices it to 0.1x for everything already cached. +Every turn of an agentic task resends the entire growing conversation - system prompt, tool definitions, every prior turn - so a 40-turn task sends its first turn 40 times and task cost grows with roughly the square of turn count. Caching does not stop the resending; it reprices everything already cached to the model's cache-read rate, a small fraction of base input. For design and placement - the prefix-match invariant, classifying inputs by stability, breakpoint patterns, the anti-pattern table - **read `shared/prompt-caching.md` and follow its workflow**; do not improvise `cache_control` markers. Points that matter specifically for cost: -- **Measured expectation**: the largest single lever on every model and benchmark Anthropic measured - it cut agent-loop cost by a factor of 2.5 to 3.7, at 81% to 90% hit rates; a small issue-triage agent's bill fell 83% from caching alone. +- **Measured expectation**: the largest single lever on every model and benchmark Anthropic measured - it cut agent-loop cost several-fold at high hit rates. - **Explicit breakpoints when many independent conversations share a static prefix** (or prefix layers change at different rates). Automatic caching only amortizes within one conversation; in the cookbook's worked example, one explicit breakpoint on the static system prefix roughly halved cost per task across a queue of independent tasks. The robust shape for agent loops - one explicit breakpoint on the static prefix plus top-level automatic caching for the tail - and the cases where automatic alone is a pure surcharge are in `shared/prompt-caching.md` § Automatic vs explicit breakpoints. -- **Use the 1-hour cache duration when the loop waits on humans between turns.** It writes at 2x instead of 1.25x and pays for itself on the first prevented miss - a miss resends the whole prefix at full price and writes it again. Decide from the start-to-start gap between requests (generation time counts against the TTL) - the table in `shared/prompt-caching.md` § Choosing the TTL. -- **Audit for mid-task cache-breakers**: dynamic content above a breakpoint; changing `thinking` or `effort` between requests (always invalidates the messages cache, and on some models the tools+system cache too - `shared/prompt-caching.md` § Invalidation hierarchy); changing a task budget mid-task; every context-editing pass; switching models mid-conversation (caches are per-model). -- **Verify from usage, not from code review - and re-verify after every prompt-assembly change**: on a warmed-up loop, `cache_read_input_tokens` should dominate regular `input_tokens`, and `cache_creation_input_tokens` should be roughly one turn's worth, not the whole conversation. If it isn't, hunt for a cache-breaker with the healthy-loop signature and payload-diff method in `shared/prompt-caching.md` § Verifying cache hits - unless the workload's input is mostly unique per-request payload (which can never cache), or the misses are concurrent-batch artifacts (§ 2.5); neither is a breaker, and neither has a fix. +- **Use the 1-hour cache duration when the loop waits on humans between turns.** It writes at 2x instead of 1.25x, so it pays only through the misses it prevents, and on gaps longer than an hour it costs more than the default. Decide from the distribution of start-to-start gaps between requests (generation time counts against the TTL), not their average - the table in `shared/prompt-caching.md` § Choosing the TTL, which also covers the `max_tokens: 0` keep-alive that can beat the longer duration for pauses well short of an hour, more so where the model's cache reads are cheapest. +- **Audit for mid-task cache-breakers**: dynamic content above a breakpoint; changing `thinking` or top-level `effort` between requests (always invalidates the messages cache, and on some models the tools+system cache too - `shared/prompt-caching.md` § Invalidation hierarchy); setting or changing a structured-output format; switching `speed`; changing a task budget mid-task; every context-editing pass; a thinking block the API drops (the messages cache changes from that block onward); switching models mid-conversation (caches are per-model). +- **Not breakers, and where to put a change that is one**: setting effort explicitly to the model's default is the same as omitting it; a per-message effort change (beta) keeps the cache on the models that accept it; and most edits to `system` or `tools` have an append-only form that keeps the prefix: a mid-conversation system message (a `role: "system"` message inside `messages`) instead of editing `system`, `tool_addition` / `tool_removal` blocks instead of editing `tools`, and a turn-scoped reminder (`clear_at`) for text that should not persist. The table in `shared/prompt-caching.md` § Invalidation hierarchy lists each with its model availability. When a model or top-level effort change is wanted anyway, make it where the cache is already going to break: on the first request after a compaction (not the one that triggers it), or between conversations. +- **On models that run the preserved-thinking check, an edit to `system`, `tools`, or earlier messages breaks the model's earlier reasoning as well as the cache.** Each thinking block is tied to the conversation that produced it, and the edits that invalidate it are the same edits that restart the cache, so one discipline serves both: keep `system` and `tools` fixed for the session and treat `messages` as append-only. `cache_control` markers, `effort`, `max_tokens`, and `tool_choice` are outside the check. A diff that moves content out of `system` or `tools` is still an edit for conversations in flight when it deploys: have the diff keep sending the stored `system` and `tools` bytes to those conversations and the new bytes to new conversations only. Where the harness cannot do that, say in the report that in-flight conversations take one cold miss and, where the check is enforced, are rejected until their thinking blocks are dropped (next bullet). +- **What an invalid edit costs**: by default the request is rejected where the check is enforced. Opting into dropping the failing blocks instead (the Preserved thinking page has the field, and the beta header that both it and the response's list of dropped blocks need) leaves them unbilled, but the session can still cost more, because the model may think again to rebuild them. Which models and accounts are checked changes with releases - read it from that page (`https://platform.claude.com/docs/en/build-with-claude/preserved-thinking.md`), never from this guide - and hand off to the `preserved-thinking-migration` subcommand (`shared/preserved-thinking-migration.md`) to find and fix a harness's edits. +- **Short prefixes**: the minimum cacheable prefix is per-model, so after any model change re-check whether a short prompt still caches, or now does - table in `shared/prompt-caching.md` § API reference. +- **Verify from usage, not from code review - and re-verify after every prompt-assembly change**: on a warmed-up loop, `cache_read_input_tokens` should dominate regular `input_tokens`, and `cache_creation_input_tokens` should be roughly one turn's worth, not the whole conversation (the Cost Optimization page gives the current bar for how much of its input a healthy loop reads from the cache). If it isn't, hunt for a cache-breaker with the healthy-loop signature in `shared/prompt-caching.md` § Verifying cache hits - unless the workload's input is mostly unique per-request payload (which can never cache), or the misses are concurrent-batch artifacts (§ 2.5); neither is a breaker, and neither has a fix. To localize the breaker, reach for cache diagnostics first where the platform has it (availability in `shared/platform-availability.md`): include a `diagnostics` object on every request (`previous_message_id: null` on the first, the previous response's `id` after that), and the response's `diagnostics` object names where the two requests diverged - no payload logging needed. `shared/prompt-caching.md` § Verifying cache hits also sends a beta header on every request; the Cache diagnostics page (`https://platform.claude.com/docs/en/build-with-claude/cache-diagnostics.md`) says that header is no longer required, that requests that still send it work as before, and that the `diagnostics` object is the opt-in, so a harness that already sends the header can keep it, and every request needs the object either way. That page has the current shape. On other platforms, fall back to the payload-diff method in the same section. - **The cache probe, when there is no usage history to read**: a scratch script for the project's own stack that sends one representative request twice, byte-identical; prints all four usage meters (`input_tokens`, `cache_creation_input_tokens`, `cache_read_input_tokens`, `output_tokens`) for both; and exits non-zero if the second request's `cache_read_input_tokens` is zero. Ship it alongside the caching diff so the user can run the before/after themselves. It spends real tokens and may execute the project's tools - run it only under the standing approval rule, and point it at a scratch environment if the request's tools mutate state. ### 2.2 Input tokens - progressive disclosure @@ -119,23 +126,23 @@ Send the model what the task needs, let it fetch the rest. Each sub-lever has a - **Chained tool calls whose intermediates don't matter** -> programmatic tool calling runs the calls from code so only the filtered result enters context; its documentation reports 24% fewer input tokens on agentic search benchmarks, with a higher score. - **Broad data-dump tools** -> prefer narrow accessors (`get_policy(claim_id)` over `get_all_policies()`), and give list tools `limit`/`fields`/`date_range` parameters. - **Unbounded user-supplied input** -> the token-counting endpoint as an ingestion gate (`shared/token-counting.md`): count first, then truncate, summarize, or route oversize payloads to the Files API. -- **The prompt text itself** -> run the `prompt-audit` subcommand (`shared/prompt-audit.md`) as part of this step; its pattern tables are the reference for dated prompt text (this guide deliberately does not restate them), and its report and proposed diff fold into this workflow's deliverables. Skip when the prompt surface is small and recently audited. Prompts written for an older model make the current one over-work: on a support-desk evaluation, prompts written for Claude Opus 4.8 cost 36% more per ticket on Claude Opus 5 for no change in accuracy; audited, the same prompts were 14% cheaper than unaudited and more accurate (97% of tickets, up from 92%). On the Claude Sonnet 4.6 to Claude Sonnet 5 migration the audit took 14% off at the same accuracy. +- **The prompt text itself** -> run the `prompt-audit` subcommand (`shared/prompt-audit.md`) as part of this step; its pattern tables are the reference for dated prompt text (this guide deliberately does not restate them), and its report and proposed diff fold into this workflow's deliverables. Skip when the prompt surface is small and recently audited. Prompts written for an older model make a newer one over-work: in Anthropic's support-desk evaluation, prompts carried over unaudited cost noticeably more per ticket on the newer model for no change in accuracy, and the audited versions were cheaper and, in one migration, more accurate (current figures: the Cost Optimization URL in `shared/live-sources.md`). -**Caveat for the whole section**: a smaller prefix is not automatically a cheaper task. Deferring context means the model may spend discovery turns fetching what it previously read inline. Validate against the eval - on the cookbook's workload, wrapping the manual in a tool matched the explicit-breakpoint config on cost and gave back accuracy. +**Caveat for the whole section**: a smaller prefix is not automatically a cheaper task. Deferring context means the model may spend discovery turns fetching what it previously read inline. Validate against the eval - on the cookbook's workload, wrapping the manual in a tool matched the explicit-breakpoint config on cost and gave back accuracy. These changes edit `system` or `tools`, so apply them to new conversations only where the harness can, and otherwise say in the report what a conversation already in flight pays when they deploy: a restarted cache and, on models that run the preserved-thinking check, invalid thinking blocks in its history (§ 2.1). Tools declared up front with `defer_loading` are the form that stays valid. ### 2.3 Agent-loop hygiene - keep long loops from compounding Only relevant when the profile shows deep loops with bulky accumulating results; short loops never trigger these and the added machinery is pure overhead. -- **Context editing** (clearing old tool uses or thinking) **is a context-window tool, not a savings lever.** Every clearing pass rewrites the cached conversation, which works against prompt caching - in the run measured for the platform docs, context editing cost more than it saved. Use it to make room in the window; set the trigger high enough that clears stay infrequent, and clear in a few large batches rather than every turn. -- **Compaction** (the server-side summarize-and-continue edit) needs sessions long enough to reach its trigger; where it fired once on a long triage run it cut the bill a further 38%. Steer it with its `instructions` string so task-critical state survives the summary. -- **Client-side pruning at natural boundaries**: collapse bulky tool results to one-line extracts when a work phase completes, keeping the message array byte-identical between prunes so each prune is one cold cache miss rather than a new miss every turn. +- **Context editing** (clearing old tool uses or thinking) **is a context-window tool, not a savings lever.** Every clearing pass rewrites the cached conversation, which works against prompt caching - in the run measured for the platform docs, context editing cost more than it saved. Use it to make room in the window; set the trigger high enough that clears stay infrequent, and clear in a few large batches rather than every turn (`clear_at_least` sets the minimum a clear must remove, so a clear is skipped unless it removes enough to be worth the cache write). On models that run the preserved-thinking check it is also the safe way to clear: the check compares the conversation as sent, so server-side clearing and compaction do not count as edits, where the same trim done client-side does. +- **Compaction** (the server-side summarize-and-continue edit) pays only in sessions long enough to need it; on the long run measured for the platform docs it took roughly a third off the bill, and on the short run it saved nothing. Where the platform offers on-demand compaction the docs prefer it to the token-threshold form (status and platforms: the **Compaction** URL in `shared/live-sources.md`). A custom `instructions` string replaces the default summarization prompt rather than adding to it, so write it to keep the task-critical state the default would have kept; and the summarization call is billed like any other request (it shows under `usage.iterations`). +- **Client-side pruning at natural boundaries - gated on models that run the preserved-thinking check**: collapsing bulky tool results to one-line extracts when a work phase completes rewrites earlier turns. On those models (the Preserved thinking page says which - URL in § 2.1) that invalidates every later thinking block, and the `preserved-thinking-migration` guide lists it as having no workaround (`shared/preserved-thinking-migration.md` § Severity tiers). There, size tool results and images before the first request that carries them and clear later through the server-side edits above; client-side, what stays valid is a compaction that replays nothing earlier (one summary message), or a prune after which the client drops every later thinking block, at the cost of that reasoning. Elsewhere the prune works as before: keep the message array byte-identical between prunes so each prune is one cold cache miss rather than a new miss every turn. - **Subagents for self-contained bulky steps**: a nested loop absorbs its own heavy tool results and hands back one line, optionally on a cheaper model. Skip when the deciding model needs the intermediate context to judge well - and note the subagent starts a fresh prefix with no cache shared with the parent. ### 2.4 Output tokens -- **`max_tokens` is a backstop, not a tuning knob.** The model never sees it; hitting it cuts the response off mid-thought with `stop_reason: "max_tokens"`. In Anthropic's coding runs a 16,384-token cap ended 15% of Claude Opus 5's attempts and a third of Claude Fable 5's, none of them solved - capped runs spent less per attempt and bought proportionally fewer solves, so cost per solved task didn't improve. Set it to 64,000 for agentic work (128,000 at `xhigh` or `max` effort), stream responses that large, and treat `stop_reason: max_tokens` as a failed attempt rather than retrying at the same cap. -- **To shorten visible responses**, specify the exact output shape in the prompt, ideally with an example. To shorten reasoning, that is the effort parameter (§ 2.6) - not `max_tokens`. +- **`max_tokens` is a backstop, not a tuning knob.** The model never sees it; hitting it cuts the response off mid-thought with `stop_reason: "max_tokens"`. In Anthropic's coding runs a tight cap cut off a large share of attempts, few of them solved - capped runs spent less per attempt and bought proportionally fewer solves, so cost per solved task didn't improve. Set it to 64,000 for agentic work (also the published starting point at `xhigh` or `max` effort; up to 128,000 where a single cut-off attempt is costly), stream responses that large, and treat `stop_reason: max_tokens` as a failed attempt rather than retrying at the same cap. +- **To shorten visible responses**, specify the exact output shape in the prompt, ideally with an example. To shorten reasoning, that is the effort parameter (§ 2.6) - not `max_tokens`, and not switching thinking off unchecked: some current models reject a disabled or budgeted `thinking` configuration with a 400, so read SKILL.md -> Thinking & Effort for the target model before proposing any `thinking` change. Where thinking is always on, effort is the main control over thinking spend, and a workload that ran without thinking can bill more output tokens after moving there. - **Stop sequences as content-aware early exits**: register a sentinel the model emits when it cannot proceed (for example ``), so it stops instead of spending tokens explaining. ### 2.5 Batch processing @@ -150,31 +157,34 @@ Only relevant when the profile shows deep loops with bulky accumulating results; From here down, every lever trades capability for cost. Sweep on the eval, one change at a time. -- **Sweep effort before touching the model** (on models that expose an effort parameter - check `shared/models.md` or the **Effort Parameter** URL in `shared/live-sources.md`). Effort scales thinking and tool-call depth without changing the model. Test each level in a separate session - changing effort mid-session invalidates the cache and distorts the comparison. Sweep mechanics that keep the comparison honest: +- **Sweep effort before touching the model** (on models that expose an effort parameter - check `shared/models.md` or the **Effort Parameter** URL in `shared/live-sources.md`). Effort scales thinking and tool-call depth without changing the model. Test each level in a separate session - changing top-level effort mid-session invalidates the cache and distorts the comparison. Sweep mechanics that keep the comparison honest: - Cells are byte-identical except `output_config.effort`; same model throughout. Complete every sample request at one setting before starting the next, in a stable order, so cache reads are comparable across settings - and if the cache meters still differ materially between settings, say so and weight the read toward output-side cost. - Include a hard case the user knows about: curves are flattest on easy tasks, and the hard tail is where higher effort earns its cost. - **Side-effect gate**: if replaying a sample request executes tools that mutate real state, point the replay at a scratch environment or stub those tools first; a sweep is never worth a production mutation. If that isn't possible, sweep only the requests that are safe to replay and say so. - Read the curve as flat (the lower setting does this workload's work), steep (the higher setting is earning its cost - now a measured number rather than a fear), or mixed (name which tasks flipped - those are the candidates for the re-run-failures policy below). Differences of a task or two of pass rate, or cents of mean cost, are within noise on single runs; the remedy is repeat trials at the settings in contention, offered with their cost. - - The curve is per-workload *and* per-model. Keep the sample and the outcome check where the report says they live, and re-sweep after a model migration, a major prompt change, or a workload shift. + - The curve is per-workload *and* per-model. Keep the sample and the outcome check where the report says they live, and re-sweep after a model migration, a major prompt change, or a workload shift. Default effort differs by model, and the token allocation behind each level can change between models, so after a migration run a fresh sweep from the new model's own default (SKILL.md -> Thinking & Effort) rather than carrying a level over. What to expect by workload shape: - - Research and knowledge work: nearly flat curves - in Anthropic's runs (all with Claude Fable 5), `low` gave up 1 to 3 points for a third to a half off cost per task; `medium` matched the default's accuracy at 70% to 85% of its cost; the default bought nothing measurable over `medium` on any of the four benchmarks measured. Lower effort is also faster (4.5 versus 7.9 minutes per problem on one research benchmark). - - Long-horizon coding: a real tradeoff - Claude Opus 5 gave up about 2 points at `medium` for half the cost, and about 8 points at `low` for a quarter of it. - - Reasoning-ceiling work (deep multi-subtopic research): every effort step bought about 2.4 rubric points - no free cut on that curve. -- **Re-run failures at higher effort** - when the workload has a usable failure signal (tests, a checker, a validator). Run everything at `low` and re-run failures at the default: in Anthropic's coding runs, about 93% passed for about $0.70 per task, against 91.7% for $1.39 running everything at the default - the same pass rate for half the cost, counting the failed cheap attempts. Starting at `medium` solved about 94% for about $0.95. Use this for the saving, not the lift, and price in the checker and the doubled wall-clock on failures. -- **Task budgets** (the model sees the budget and paces itself - this is the budget control that saves money): set from the loop's 90th-percentile token usage, then tighten. The budget is advisory - it steers the model rather than stopping it - so verify adherence on the workload. Measured on coding: a generous budget gave up about 2.7 points of pass rate for an 18% saving; the tightest allowed budget gave up 4.4 points for 47%. Budgets below the 20,000-token floor are rejected; very tight budgets can produce refusal-like behavior; set the budget once on the first request - a mid-task change invalidates the cache. Check model availability before wiring it in (beta, and not available on every current model) - parameter shape, the streaming requirement, and supported models are in this skill's SKILL.md -> Task Budgets (Quick Reference) and `shared/model-migration.md` -> Task Budgets. + - Research and knowledge work: nearly flat curves - in Anthropic's runs `low` gave up a few points for a third to a half off cost per task, `medium` matched `high`'s accuracy for noticeably less, and `high` bought nothing measurable over `medium`. Lower effort is also faster. + - Long-horizon coding: a real tradeoff - each step down gave up a few points of pass rate (single digits in the published runs) for a substantially lower cost per task, and the highest settings bought little for a large multiple of the cost. + - Hard work does not automatically need high effort: on one deep-research benchmark the published scores were nearly level across `low`, `medium`, and `high` while cost per task climbed. Only the sweep says whether more effort still buys accuracy on this workload. + - Current curves per model: the Cost Optimization URL in `shared/live-sources.md`. +- **Re-run failures at higher effort** - when the workload has a usable failure signal (tests, a checker, a validator). Run everything at a low setting and re-run only the failures at a higher one: in Anthropic's coding runs this held the pass rate of running everything at the higher setting, or slightly beat it, for a little over half the cost, counting the failed cheap attempts. Use this for the saving, not the lift, and price in the checker and the doubled wall-clock on failures. +- **Tell the model that time matters, and show it the elapsed time**, when wall-clock time matters: the published runs finished sooner and cost less per task for a small score cost. Append the clock as a new message after the newest turn (a mid-conversation system message where the model accepts one), never by rewriting `system` - recipe and figures: the Cost Optimization URL in `shared/live-sources.md`. Validate it like any other tradeoff. +- **Task budgets** (the model sees the budget and paces itself - this is the budget control that saves money): set from the loop's 90th-percentile token usage, then tighten. The budget is advisory - it steers the model rather than stopping it - so verify adherence on the workload. Measured on coding: pass rate fell a few points as the budget tightened while cost per task fell by a much larger share - budgets bought efficiency at a price in pass rate that grows as they tighten. Budgets below the documented floor are rejected; very tight budgets can produce refusal-like behavior; set the budget once on the first request - a mid-task change invalidates the cache. Check model availability before wiring it in (beta, and not available on every current model) - parameter shape, the streaming requirement, and supported models are in this skill's SKILL.md -> Task Budgets (Quick Reference) and `shared/model-migration.md` -> Task Budgets. - **Backstops that don't save per-task money but cap the damage**: a Managed Agents session budget is a hard dollar stop; a workspace spend limit is the final backstop on the whole workspace. ### 2.7 Model selection - last, deliberately -Model choice constrains the intelligence ceiling, which is why it comes after every lever that doesn't. +Model choice constrains the intelligence ceiling, which is why it comes after every lever that doesn't. (The exception is when the project has an eval and you are running the hillclimb loop: there `shared/evals/cost-hillclimb.md` walks model x effort early, because an eval can detect the case where a stronger model at lower effort is the cheaper cell. Without an eval that case is invisible and a model swap stays the riskiest change - keep it last.) -- **Price candidates in cost per completed task on your own traffic**, including the larger model at reduced effort - per-token price lists do not predict the ranking. In Anthropic's runs, Claude Fable 5 at `low` effort beat Claude Sonnet 5 on a deep-research benchmark while costing about 10% less per task; on a coding subset both models largely saturate, Claude Opus 5 matched Claude Fable 5 (91.7% versus 91.3%) at about 60% of its cost. For most agent workloads, start with Claude Opus 5. At the other end, Claude Haiku 4.5 answered knowledge questions at about a tenth of Claude Opus 5's cost per question at 63% accuracy versus 92% - it fits high-volume work with checkable outputs, not long agentic loops. +- **Check for an upgrade before a step-down.** If the workload runs a model a generation or two behind the current one in its tier, the cheapest lever can be the model string: in the published runs the cheapest upgrade was often the new model at a lower effort setting. That direction is not guaranteed - on one research benchmark the same upgrade cost more per task - so measure it on this workload before assuming it saves. It is still a migration - run the `migrate` subcommand for the breaking changes, re-run the prompt audit (§ 2.2), and re-sweep effort from the new model's default. +- **Price candidates in cost per completed task on your own traffic**, including the larger model at reduced effort - per-token price lists do not predict the ranking. In Anthropic's runs the ranking flipped by workload: the frontier model at `low` effort out-solved a smaller model for less per solved task on one benchmark, and on a coding subset that the frontier model and the model one tier below it both largely saturate, the lower of the two at its default matched the frontier model for a fraction of the cost. Which model to start from is not this guide's call: take the current starting-point recommendation and per-task comparisons from the Cost Optimization URL in `shared/live-sources.md`, and current rates from the Pricing URL there. At the other end, the smallest model answered knowledge questions at a fraction of the cost per question of that same model one tier below the frontier, with markedly lower accuracy - it fits high-volume work with checkable outputs, not long agentic loops. - **Price the tail, not the median.** Compare models on the hardest tenth of the workload: on the typical task every model looks similar and the cheapest looks best, but the bill is decided by the tasks the cheap model fails - and the tail is where the money goes even when nothing fails (on one 20-problem research run, two problems carried 43% of the spend). -- **The stepping-down method**: sweep effort on the current model first; if `low` passes the eval, drop one model tier, **confirm which parameters and effort levels the target tier supports** (SKILL.md -> Thinking & Effort), reset effort to that tier's default - not a hardcoded level; the default and the supported range vary by model - and re-sweep down from there (on a tier without `effort` support, evaluate at its single default only). One notch at a time, against the eval - and when there is no cheaper tier, the lever is exhausted; say so rather than inventing a step. Current model lineup and discovery: `shared/models.md`; for model-swap mechanics and per-target breaking changes, the `migrate` subcommand (`shared/model-migration.md`). -- **Two models can beat one, in exactly two measured shapes** - both are architecture changes; validate like one: - - **Advisor** (a cheaper executor runs the loop and consults a frontier model on hard decisions): pays when the capability gap between the two models is wide and the executor actually consults. The consult rate is the fragile variable - lowering effort can drop a pairing from consulting on most tasks to almost none, and then it scores below the executor alone - and gating the consult well requires a cheap signal; asking the executor to recognize the hard cases itself demands the very judgment it's missing. Benchmark first: on Anthropic's coding benchmark the flagship pairing was the most accurate configuration measured but sat within noise of the frontier model alone at `medium` effort, at about the same cost - sweep effort and price the stronger model alone before adding the advisor. - - **Orchestrator** (a frontier model plans and delegates bulk work to cheaper workers): buys something only when there is bulk to hand off - many independent pieces, ideally too many for one context window. On work larger than any context window it cost 55% less than the frontier model solo at every effort setting (3 to 7 points below its best score); on routine search work it paid as tail insurance (about half the average cost, a third at the 90th percentile) but reversed on the harder full set. When the work is one dependent chain, or fits in a single context, the orchestrator pays for a plan, a handoff, and a merge that a single model gets for free - in every such case measured, the coordinator's model alone at lower effort came out ahead. +- **The stepping-down method**: sweep effort on the current model first; if `low` passes the eval, drop one model tier, **confirm which parameters and effort levels the target tier supports** (SKILL.md -> Thinking & Effort) - including the `thinking` configuration the current code sends, which the target may reject - reset effort to that tier's default - not a hardcoded level; the default and the supported range vary by model - and re-sweep down from there (on a tier without `effort` support, evaluate at its single default only). One notch at a time, against the eval - and when there is no cheaper tier, the lever is exhausted; say so rather than inventing a step. Current model lineup and discovery: `shared/models.md`; for model-swap mechanics and per-target breaking changes, the `migrate` subcommand (`shared/model-migration.md`). Switch between conversations, not inside one: a mid-conversation switch cold-starts the cache and, where the new model cannot read the old one's thinking blocks, runs on without that reasoning (`shared/preserved-thinking-migration/causes.md` § Switching models mid-conversation). +- **Two models can beat one, in exactly two measured shapes** - both are architecture changes; validate like one, and any design that routes a single conversation between models inherits the model-switch costs above: + - **Advisor** (a cheaper executor runs the loop and consults a frontier model on hard decisions): pays when the capability gap between the two models is wide and the executor actually consults. The consult rate is the fragile variable - lowering effort can drop a pairing from consulting on most tasks to almost none, and then it scores below the executor alone - and gating the consult well requires a cheap signal; asking the executor to recognize the hard cases itself demands the very judgment it's missing. Benchmark first: on Anthropic's coding benchmark the flagship pairing landed about on the executor model's own effort curve - the advisor bought roughly what more effort did - so sweep effort and price the stronger model alone before adding the advisor. + - **Orchestrator** (a frontier model plans and delegates bulk work to cheaper workers): buys something only when there is bulk to hand off - many independent pieces, ideally too many for one context window. On work larger than any context window it cost about half as much as the frontier model solo, for a lower score; on routine search work it paid as tail insurance (about half the average cost, less still at the expensive tail) but reversed on the harder full set. When the work is one dependent chain, or fits in a single context, the orchestrator pays for a plan, a handoff, and a merge that a single model gets for free - in every such case measured, the coordinator's model alone at lower effort came out ahead. ## Step 3: Apply, measure, keep or revert - one lever at a time @@ -197,15 +207,15 @@ Adapted from the cookbook's takeaways table, for mapping a profile to levers (ro | Where the cost is | Reach for | Skip it or watch out when | |---|---|---| -| Same system prompt and tools re-billed on every call | Prompt caching with auto first, then an explicit breakpoint on the static prefix when many independent conversations share it or prefix layers change at different rates, and 1-hour TTL if calls are more than five minutes apart | Anything dynamic sits above the breakpoint - move that content into the user turn. And a cache that already reads well needs nothing: concurrent-batch misses (§ 2.5) aren't breakers, and a 1-hour TTL doesn't reach calls that are hours apart | +| Same system prompt and tools re-billed on every call | Prompt caching with auto first, then an explicit breakpoint on the static prefix when many independent conversations share it or prefix layers change at different rates, and the 1-hour TTL or a keep-alive where gaps run past five minutes (§ 2.1) | Anything dynamic sits above the breakpoint - move that content into the user turn. And a cache that already reads well needs nothing: concurrent-batch misses (§ 2.5) aren't breakers, and a 1-hour TTL doesn't reach calls that are hours apart | | Large reference document in every prompt | Move it behind a tool or skill | Each call needs most of the document rather than a section, or the eval shows misses on cases that hinge on rules the model has to go looking for | | Many or heavy tool schemas | Tool search with `defer_loading` | Under roughly 10K schema tokens, where the search step is overhead | | Images, PDFs, or large files in context | Downscale images to what the task needs, and use the Files API plus code execution for tables and PDFs | There is nothing to extract or compute so the sandbox only adds tokens | | Unbounded user-supplied input | Token counting as an ingestion gate | | -| Bulky results piling up across a long loop | Context editing or compaction server-side, or a client-side prune at natural boundaries | Loops are short or the cleared content is still needed, and note that every edit breaks the cache from that point | +| Bulky results piling up across a long loop | Context editing or compaction server-side; a client-side prune at natural boundaries, gated on models that run the preserved-thinking check (§ 2.3) | Loops are short or the cleared content is still needed, and note that every edit breaks the cache from that point - and a client-side edit of earlier turns also invalidates later thinking on models that run the preserved-thinking check | | One self-contained step with bulky intermediates | Subagent, optionally on a cheaper model | The deciding model needs that intermediate context to judge well | | Long visible responses | Specify the output shape with an example, with `max_tokens` as a backstop and a stop-sequence sentinel for early exits | | -| Thinking and tool calls dominate, and the eval has headroom | Lower `effort` first, then drop a model tier and re-sweep effort | Always a direct capability trade, so step down one notch at a time against the eval | +| Thinking and tool calls dominate, and the eval has headroom | Lower `effort` first; check for a newer model in the tier (§ 2.7), then drop a model tier and re-sweep effort | Always a direct capability trade, so step down one notch at a time against the eval | | Mostly routine cases with a few hard ones | Advisor tool on a cheaper driver | There is no cheap signal to gate the consult, leaving the driver to spot hard cases itself | | No one is waiting on the response | Batch API, flattening a tool loop into one request by pre-fetching its inputs if you have to | A user is waiting, or when flattening changes how the model reasons | @@ -216,7 +226,7 @@ Adapted from the cookbook's takeaways table, for mapping a profile to levers (ro **Report skeleton** (section order and required columns - keep the rest flexible): -- **Scope / quality bar / baseline / platform** (Step 0 assumptions) +- **Scope / quality bar / baseline / platform / pages fetched, with dates** (Step 0 assumptions) - **Token profile** (Step 1) - **Ranked shortlist** - table columns: `Lever | Type (free win / tradeoff) | Savings ceiling | Data source (measured / usage logs / code estimate)`. Ceiling is in the unit tier the data supports (Step 1 -> Rank the levers). Caption the table "ranked by savings ceiling, not application order." - **Proposed changes** - one diff per lever, numbered in § 2 application order (free wins -> effort/budgets -> model), each tagged *applied and measured* / *proposed* / *needs an eval* @@ -225,9 +235,10 @@ Adapted from the cookbook's takeaways table, for mapping a profile to levers (ro ## Sources and live references -The measured results above come from two published Anthropic sources (and the Admin API facts in Step 1 from a third); fetch them when the user needs the full write-ups, charts, or current numbers: +The measured results above come from two published Anthropic sources (and the Admin API facts in Step 1 from a third); Step 0 requires the first and the Pricing page before anything is sized; the cookbook and Admin API docs are fetched when the user needs the full write-ups or schemas: - The platform guide **Optimizing for cost and intelligence** - WebFetch the Cost Optimization URL in `shared/live-sources.md`. - The cookbook **Cost optimization on the Claude API** (`https://platform.claude.com/cookbook/cost-optimization-cost-optimization`) - a runnable end-to-end worked example of this workflow. - The **Usage and Cost Admin API** docs - the URL in `shared/live-sources.md`; the endpoint reference pages linked from that page carry the full parameter and response schemas. - Per-model prices: always the **Pricing** URL in `shared/live-sources.md`, never remembered rates. +- The **Preserved thinking** page (URL in § 2.1) - required whenever the plan includes a lever that edits history, `system`, or `tools`. diff --git a/skills/claude-api/shared/error-codes.md b/skills/claude-api/shared/error-codes.md index 2b594358f..abb549d25 100644 --- a/skills/claude-api/shared/error-codes.md +++ b/skills/claude-api/shared/error-codes.md @@ -8,8 +8,9 @@ This file documents HTTP error codes returned by the Claude API, their common ca | ---- | ----------------------- | --------- | ------------------------------------ | | 400 | `invalid_request_error` | No | Invalid request format or parameters | | 401 | `authentication_error` | No | Invalid or missing API key | -| 403 | `permission_error` | No | API key lacks permission | -| 404 | `not_found_error` | No | Invalid endpoint or model ID | +| 402 | `billing_error` | No | Billing or payment problem | +| 403 | `permission_error` | No | Not allowed for this credential | +| 404 | `not_found_error` | No | Unknown endpoint, or model not found or not available to your org | | 413 | `request_too_large` | No | Request exceeds size limits | | 429 | `rate_limit_error` | Yes | Too many requests | | 500 | `api_error` | Yes | Anthropic service issue | @@ -26,6 +27,7 @@ This file documents HTTP error codes returned by the Claude API, their common ca - Invalid parameter types (e.g., string where integer expected) - Empty messages array - Messages not alternating user/assistant +- An `anthropic-beta` value that does not exist or is not enabled for your organization. Both cases return the same message: ``Unexpected value(s) `` for the `anthropic-beta` header.`` **Example error:** @@ -66,11 +68,13 @@ This file documents HTTP error codes returned by the Claude API, their common ca **Causes:** -- API key doesn't have access to the requested model -- Organization-level restrictions -- Attempting to access beta features without beta access +- The credential's organization or workspace is not allowed to perform this operation. +- The request was blocked by an access requirement, such as a region restriction or identity verification, for a model your organization can otherwise use. The message says what to do. +- Rarely, the model server denies a request that passed the API's access check. The message is `Access to this model requires an access grant your request does not have.` -**Fix:** Check your API key permissions in the Console. You may need a different API key or to request access to specific features. +A model your organization cannot use is normally a 404, not a 403 (see below). A beta header your organization is not enabled for is a 400. + +**Fix:** Check your organization's access and workspace settings in the Console. --- @@ -80,9 +84,12 @@ This file documents HTTP error codes returned by the Claude API, their common ca - Typo in model ID (e.g., `claude-sonnet-4.6` instead of `claude-sonnet-4-6`) - Using deprecated model ID +- A model ID that exists but is not available to your organization - Invalid API endpoint -**Fix:** Use exact model IDs from the models documentation. You can use aliases (e.g., `claude-opus-5`). +A model that does not exist and a model your organization cannot use return the same response, `not_found_error` with a message that starts with `model: `. The API does not reveal whether a model exists to callers who cannot use it. + +**Fix:** Use exact model IDs from the models documentation. You can use aliases (e.g., `claude-opus-5-5`). To see which models your organization can use, call `GET /v1/models`. --- @@ -107,15 +114,17 @@ Some 400 errors are specifically related to parameter validation: - `budget_tokens` >= `max_tokens` in extended thinking - Invalid tool definition schema -**Model-specific 400s on Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7:** +**Model-specific 400s on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7:** - `temperature`, `top_p`, `top_k` are removed - sending any of them returns 400. Delete the parameter; see `shared/model-migration.md` -> Per-SDK Syntax Reference. - `thinking: {type: "enabled", budget_tokens: N}` is removed - sending it returns 400. Use `thinking: {type: "adaptive"}` instead. - **Claude Opus 5:** `thinking: {type: "disabled"}` returns 400 when `effort` is `xhigh` or `max` - it is accepted at `high` or below. Thinking is on by default, so omitting the param runs adaptive rather than disabling it. - **Fable 5/5.1 only:** an explicit `thinking: {type: "disabled"}` returns 400 at any effort (it is accepted on Opus 4.8/4.7). Omit the `thinking` param entirely instead. - **Fable 5/5.1, Mythos 5/5.1:** if the organization or workspace is set to zero data retention (ZDR) - or any retention below the required 30 days - then **all** requests to these models return `400 invalid_request_error` ("In order to access this model, your organization or workspace must have data retention enabled."), even with a perfectly valid payload; ZDR only if expressly authorized by Anthropic. Check the retention configuration before debugging the request body. -- **Claude Fable 5.1 / Claude Mythos 5.1 (and Mythos Preview):** `tool_choice: {type: "any"}` or `{type: "tool", name: ...}` returns 400 `tool_choice: type "tool" and "any" are not supported for this model.` - also on `count_tokens` and Batches. Use `{type: "auto"}` plus a prompt instruction (`strict: true` for schema-valid arguments), or structured outputs. -- **Claude Fable 5.1 / Claude Mythos 5.1 - preserved thinking / history-editing check (new accounts created on/after 2026-08-31, or any request that sets `prefix_mismatch_behavior`):** ``messages.N.content.M: Invalid `signature` in `thinking` block. The block is bound to a different conversation. Remove the block, or set `thinking.block_binding.prefix_mismatch_behavior` to "drop_block".`` (plus a sentence naming the beta header when it wasn't sent, and optionally one naming the first message that changed) means the system prompt, tool list, or an earlier message changed since that thinking block was produced. Retrying the same body never clears it; `count_tokens` returns the same 400. (In the Message Batches API the *unset* default drops the failing blocks instead of failing the item - a Batches item fails as `errored` only with `prefix_mismatch_behavior: "error"` set.) Strip the named block and every thinking block after it and retry once, or resend with `thinking.block_binding.prefix_mismatch_behavior: "drop_block"` under beta `thinking-binding-controls-2026-08-01` (where the controls beta is offered - Claude API / Claude Platform on AWS at launch, per model on Bedrock and Google Cloud, not on Foundry: `shared/platform-availability.md`; elsewhere use the strip-and-retry path; without the header that field is a 400 ending `block_binding: Extra inputs are not permitted`); then fix the harness so it stops editing history (see `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5). The same leading clause with *no* "bound to a different conversation" sentence is a tampered signature - always a 400, regardless of the setting. +- **Claude Opus 5.5:** `thinking: {type: "disabled"}` or `{type: "enabled", budget_tokens: N}` returns 400 `"thinking.type.disabled" is not supported for this model. Use "thinking.type.adaptive" and "output_config.effort" to control thinking behavior.` (`"thinking.type.enabled"` for the budget form) at every effort level - omit `thinking` and lower `output_config.effort` instead. A `tools` entry of type `computer_20251124` returns 400 `'claude-opus-5-5' does not support tool types: computer_20251124.` followed by `Did you mean one of` and the accepted types - declare `{type: "computer_toolset_20260801"}` instead (no beta header, no `name` / display size). See `shared/model-migration.md` -> Migrating to Claude Opus 5.5. +- **Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5:** `tool_choice: {type: "any"}` or `{type: "tool", name: ...}` returns 400 `tool_choice: type "tool" and "any" are not supported for this model.` - also on `count_tokens` and Batches. Use `{type: "auto"}` plus a prompt instruction (`strict: true` for schema-valid arguments), or structured outputs. +- **Claude Sonnet 5.5:** `thinking: {type: "disabled"}` returns 400 `"thinking.type.disabled" is not supported for this model. Use "thinking.type.between_tools" for the lowest thinking setting, or "thinking.type.adaptive" and "output_config.effort" to control thinking behavior.` - send `{type: "between_tools"}` to turn thinking off, or leave thinking on at a lower effort. `between_tools` has its own 400s: at effort `xhigh` / `max` (`output_config.effort 'xhigh' is not supported when thinking is disabled on this model. Use effort 'high' or below, or enable thinking.`), with `display`, `budget_tokens`, or `block_binding` beside it, on a per-message effort change (`messages.N: output_config.effort 'low' differs from the 'high' in effect before it; ...`), and on any other model (`"thinking.type.between_tools" is not supported for this model.`). On the Claude API and Google Cloud a `computer_20251124` tool returns 400 `'claude-sonnet-5-5' does not support tool types: computer_20251124.` - declare `{type: "computer_toolset_20260801"}` (Amazon Bedrock still accepts the earlier tool). An advisor tool `model` of Claude Opus 4.8, Claude Opus 4.7, Claude Opus 4.6, Claude Sonnet 5, or Sonnet 4.6 returns 400 with a Claude Sonnet 5.5 executor. The history-editing check in the next bullet also applies to Claude Sonnet 5.5 thinking blocks - enforced by default for new accounts on the Claude API and Amazon Bedrock - and `block_binding` is accepted only with thinking on. See `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5. +- **Claude Fable 5.1 / Claude Opus 5.5 - preserved thinking / history-editing check (new accounts created on/after 2026-08-31 on every platform, or any request that sets `prefix_mismatch_behavior`; Claude Mythos 5.1 doesn't run it):** ``messages.N.content.M: Invalid `signature` in `thinking` block. The block is bound to a different conversation. Remove the block, or set `thinking.block_binding.prefix_mismatch_behavior` to "drop_block".`` (plus a sentence naming the beta header when it wasn't sent, and optionally one naming the first message that changed) means the system prompt, tool list, or an earlier message changed since that thinking block was produced. Retrying the same body never clears it; `count_tokens` returns the same 400. (In the Message Batches API the *unset* default drops the failing blocks instead of failing the item - a Batches item fails as `errored` only with `prefix_mismatch_behavior: "error"` set.) Strip the named block and every thinking block after it and retry once, or resend with `thinking.block_binding.prefix_mismatch_behavior: "drop_block"` under beta `thinking-binding-controls-2026-08-01` (the beta is available on the Claude API, Claude Platform on AWS, Bedrock, and Vertex; Foundry unconfirmed - `shared/platform-availability.md`; without the header that field is a 400 ending `block_binding: Extra inputs are not permitted`); then fix the harness so it stops editing history (see `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5). The same leading clause with *no* "bound to a different conversation" sentence is a tampered signature - always a 400, regardless of the setting. **Common mistake with extended thinking on older models (Opus 4.6 and earlier):** @@ -173,15 +182,19 @@ thinking: budget_tokens=10000, max_tokens=16000 | Mistake | Error | Fix | | ------------------------------- | ---------------- | ------------------------------------------------------- | -| `temperature`/`top_p`/`top_k` on Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 | 400 | Remove the parameter (see `shared/model-migration.md`) | -| `budget_tokens` on Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 | 400 | Use `thinking: {type: "adaptive"}` | +| `temperature`/`top_p`/`top_k` on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 | 400 | Remove the parameter (see `shared/model-migration.md`) | +| `budget_tokens` on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 | 400 | Use `thinking: {type: "adaptive"}` | | `thinking: {type: "disabled"}` on Fable 5/5.1 | 400 | Omit the `thinking` param entirely (accepted on Opus 4.8/4.7) | | Org set to ZDR / retention below 30 days (Fable 5/5.1, Mythos 5/5.1) | 400 on every request | Fix the org's data-retention configuration - the payload isn't the problem | -| `tool_choice` `any` / `tool` on Claude Fable 5.1 / Claude Mythos 5.1 / Mythos Preview | 400 | `{type: "auto"}` + name the tool in the prompt (`strict: true` for schema-valid args), or structured outputs | -| Edited history replayed with thinking blocks (Claude Fable 5.1 / Claude Mythos 5.1, preserved thinking) | 400 `Invalid signature in thinking block ... bound to a different conversation` | Stop editing history - keep the transcript append-only, using mid-conversation `role: "system"` / tool-change messages, turn-scoped `clear_at` reminders that are never deleted, server-side context editing, and summary-only compaction instead of edits; recover once by stripping the named block and every thinking block after it (text and tool calls stay), or `prefix_mismatch_behavior: "drop_block"` | +| `thinking: {type: "disabled"}` or `budget_tokens` on Claude Opus 5.5 | 400 `"thinking.type.disabled" is not supported for this model` | Omit `thinking`; control depth with `output_config.effort` (default `medium`) | +| `computer_20251124` tool on Claude Opus 5.5 | 400 `does not support tool types: computer_20251124` | `{type: "computer_toolset_20260801"}` - no beta header, no `name` / display size; update the agent loop for member tool calls | +| `thinking: {type: "disabled"}` on Claude Sonnet 5.5 | 400 `"thinking.type.disabled" is not supported for this model` | `{type: "between_tools"}` at effort `high` or below (no other `thinking` field, no per-message effort change), or thinking on at a lower effort | +| `thinking: {type: "between_tools"}` on any other model, or at `xhigh` / `max` | 400 | Send it only to Claude Sonnet 5.5 at effort `high` or below; otherwise omit `thinking` | +| `tool_choice` `any` / `tool` on Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5 | 400 | `{type: "auto"}` + name the tool in the prompt (`strict: true` for schema-valid args), or structured outputs | +| Edited history replayed with thinking blocks (Claude Fable 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5, preserved thinking; Claude Mythos 5.1 doesn't run this check) | 400 `Invalid signature in thinking block ... bound to a different conversation` | Stop editing history - keep the transcript append-only, using mid-conversation `role: "system"` / tool-change messages, turn-scoped `clear_at` reminders that are never deleted, server-side context editing, and summary-only compaction instead of edits; recover once by stripping the named block and every thinking block after it (text and tool calls stay), or `prefix_mismatch_behavior: "drop_block"` (thinking on only - not with Claude Sonnet 5.5's `between_tools`) | | `thinking.block_binding` without `thinking-binding-controls-2026-08-01` | 400 `block_binding: Extra inputs are not permitted` | Send the beta header where the controls beta is offered (`shared/platform-availability.md`); elsewhere remove `block_binding` and use strip-and-retry | | `budget_tokens` >= `max_tokens` (older models) | 400 | Ensure `budget_tokens` < `max_tokens` | -| Typo in model ID | 404 | Use valid model ID like `claude-opus-5` | +| Typo in model ID | 404 | Use valid model ID like `claude-opus-5-5` | | First message is `assistant` | 400 | First message must be `user` | | Consecutive same-role messages | 400 | Alternate `user` and `assistant` | | API key in code | 401 (leaked key) | Use environment variable | @@ -251,7 +264,7 @@ if err != nil { ### Error `.type` Field -All `APIStatusError` subclasses now expose a `.type` property (Python: `.type`, TypeScript: `.type`, Java: `.errorType()`, Go: `.Type()`, Ruby: `.type`, PHP: `.type`) that returns the API error type string (e.g., `"invalid_request_error"`, `"authentication_error"`, `"rate_limit_error"`, `"overloaded_error"`). Use this for programmatic error classification when you need finer granularity than the HTTP status code - for example, distinguishing `"billing_error"` from `"permission_error"` (both map to 403). +All `APIStatusError` subclasses now expose a `.type` property (Python: `.type`, TypeScript: `.type`, Java: `.errorType()`, Go: `.Type()`, Ruby: `.type`, PHP: `.type`) that returns the API error type string (e.g., `"invalid_request_error"`, `"authentication_error"`, `"rate_limit_error"`, `"overloaded_error"`). Use this to classify errors by type name instead of by status code. `"billing_error"` is a 402 and `"permission_error"` is a 403. ```python except anthropic.APIStatusError as e: diff --git a/skills/claude-api/shared/evals/build-eval.md b/skills/claude-api/shared/evals/build-eval.md new file mode 100644 index 000000000..3704145ec --- /dev/null +++ b/skills/claude-api/shared/evals/build-eval.md @@ -0,0 +1,233 @@ +# Building an Eval for a Claude-Powered Application + +> **If you arrived via `/claude-api build-eval`:** this is the right file. If the user passed an argument, treat it as their answer to the first question below - what they want to measure. Run the interview - don't summarize it back to the user, ask the questions and work through the sign-offs. The goal is a runnable eval the user trusts, not a document about evals. + +This guide is for when a user wants to measure whether their Claude app is working - typically because they're about to change something (migrate to a new model, rewrite a prompt, add a tool) and need to know whether the change helped. Your job is to build an eval that could be used to make deploy decisions. + +An eval, for this purpose, is three things: **a set of input examples**, **a way to run the app against each input**, and **a way to grade each output**. The runner is usually a plain Python script; it could be a CLI, a pytest suite, or whatever fits their stack. The exact shape matters much less than whether the user looks at the inputs and says "yes, those are the cases I care about" and looks at the grades and says "yes, that's measuring the right thing." Do not impose a framework. Read how their codebase is already structured and fit the eval into it. + +Stay recommendation-forward throughout: every decision goes through `AskUserQuestion` with your pick listed first and labelled "(Recommended)", so a user who trusts your defaults clicks through in seconds and one who doesn't can override at the exact point they care about. It is much easier to react to "here's what I'd do - OK?" than to answer an open question from scratch. + +> **Talking to the user.** These steps are your execution plan, not a script to narrate. Keep user-facing messages short and outcome-focused: what you built, the number it produced, what you need them to look at, a path or link to open. Don't walk the user through which step you're on, which files you're writing, or internal bookkeeping unless they ask. One concise update per step is enough; instead of listing individual cases, prompts, or per-case scores in the chat, prefer to give the `report.html` path and a one-line headline - call out one or two specific cases in chat only when there's a reason the user should look at those first. When you need a decision - grader type, where inputs come from, what "good" means, which guardrails matter - use the `AskUserQuestion` tool rather than free-text prose: batch up to four related questions into one call, give each two to four concrete options with your recommendation listed first and labelled "(Recommended)", and don't add your own "Other" option - the tool appends a free-text one automatically. If `AskUserQuestion` isn't available (headless runs), fall back to one short question at a time. + +There are two sign-offs you always need - the inputs and the grading method. Each is a literal pause: state what you're proposing, ask for approval, and **wait for a clear yes** - not silence, and not your own judgment that it's fine. If getting to a yes took several rounds of back-and-forth, restate the final version in one message and confirm it once more before you build on it; it's easy for both sides to lose track of what was actually agreed after five refinements. They're the only places you wait for prose, not a click. Everything else is guidance; adapt freely to the user's situation. + +**Read `shared/evals/eval-audit.md` now, before Step 0, and keep it in view throughout.** It is the health checklist every eval must satisfy - task design, harness design, metrics hygiene, grader design, and whether the eval can detect the change the user is after. While you build, treat each item as a construction requirement the runner, grader, and case set meet by default; when the user brings an existing eval, it is the verification you run on it; and before the first full paid pass you run it once more against what you built and report per its §6. + +--- + +## Step 0: Understand what's being evaluated + +Start by asking what the user actually wants to measure: + +> What exactly are you trying to evaluate - which use-case or feature? If this app does several things, which one do you need a number for first? + +One app can easily have ten things worth evaluating - a classifier here, a summarizer there, an agent loop elsewhere - and they need different inputs and different grading. Pin down one. One flow per eval; don't try to build a grand unified benchmark. If the user invoked `/claude-api build-eval` with an argument, take that as their answer and confirm it rather than asking from scratch. + +Then make sure you and the user agree on what "the app" is for that flow. Find the entry point: the function, endpoint, or script that takes a user input and produces the output that matters. Read enough of it to know the model, **which provider it's calling** (first-party Anthropic API, Claude Platform on AWS, Amazon Bedrock, Vertex AI, Foundry), the system prompt, the tools, and what the output looks like (text, JSON, a tool trajectory, a file). If the entry point is a streaming proxy or wrapper that doesn't surface `model`, `usage`, or `stop_reason`, propose a small additive change to its final event so the runner can record them per case - without those the report can't derive cost or flag truncation. Any code the runner writes - judge calls included - must use the same provider's client class and model-ID format; see `SKILL.md` and its referenced `shared/` docs for the per-provider details. + +If the flow depends on live external state - a database, a search index, a customer's private documents - note that now. You'll need fixtures or a test instance to make the eval reproducible, and whether those exist will shape everything downstream. Prefer measuring real *outcomes* through the real entry point whenever possible. Only when that can't be run safely or reproducibly - because tools have real-world side effects (send emails, write to databases, delete files) or depend on live external state that's since changed - stub those tools (optionally replaying canned tool results) and grade the model's tool calls and response text instead of the downstream effect. + +Also ask what the system needs per example besides the user message: + +> What does one request into this flow carry besides the text - attached files or images? User metadata or profile? Summarized memory of prior conversations? A container image or workspace for an agent to run in? + +The answer shapes what an eval "input" is. Often it's just a prompt string; sometimes it's a prompt plus a PDF, a user profile, a conversation prefix, or a path to a docker image for an agentic environment. Don't force a schema - just find out what the app actually consumes so each eval case carries everything the entry point needs. If the input is a multi-turn conversation, also pin down what gets graded: the final response only, each assistant turn independently, or the trajectory as a whole. A turn can look fine on its own but be downstream of an earlier wrong turn - grading per-turn will call that "good" when the conversation isn't. Default to grading the conversation outcome unless the user explicitly wants per-turn. + +--- + +## Step 1: Find or build the input set + +Ask the user: + +> Do you already have any of the pieces - a set of test cases (even an informal spreadsheet), a grader or scoring function, or a harness/script that runs the app over inputs? + +**Whatever exists, use it; build only what's missing.** An existing grader gets wrapped, not rewritten; an existing harness gets a thin adapter that emits `results.jsonl`/`traces/` in the Step 3 shape (that shape is the only contract the report needs - `report/SCHEMA.md`), not replaced by the scaffold. Say which pieces you're reusing and which you're adding before you write anything. **If there are cases:** read them, then run `eval-audit.md` against them - cases, runner, and grader - and report what you find per its §6 before deciding how much to reuse. Two questions to ask the user directly rather than infer: whether the inputs are still representative of real traffic, and **where the expected outputs came from** - human-written, human-verified, or a model's outputs (which model). Gold derived from a model under comparison - the incumbent in a migration, especially - makes reference-match scoring reward imitation of that model; say so and prefer a rubric or pairwise judge, or human-verify a sample first. If the audit and the user both trust it, use it as the starting point and reuse the grading. If only partly ("the inputs are fine but the grading is vibes"), keep the inputs and rebuild the grading. If not, treat it as one source among several. + +**Either way,** ask where realistic inputs could come from. Work down this list and use the first source that's available and that the user is comfortable using: + +1. **Production transcripts or logs.** The highest-fidelity source. Ask where they live (Datadog, a database, S3, a logging endpoint) and whether you can pull a sample. Before you pull anything, confirm the source is **usable in practice**, not just available right now: *Is there a retention policy that will force you to delete this data? Does it contain PII that can't sit in a repo?* An eval built on data the user can't keep is an eval they can't re-run next quarter - that's worse than a synthetic one they can. If either answer is yes, three options: store only the **identifiers** in the repo and have the runner fetch the real inputs at eval time (nothing sensitive ever lands on disk); have the user pull and anonymize a sample themselves; or rewrite each real input into a synthetic one that preserves the shape and difficulty but replaces the identifying content (show the user the rewrites before using them). +2. **Bug reports, support tickets, or "this went wrong" examples.** Often the most valuable inputs are the ones someone complained about. Ask if there's a channel or tracker where these collect. +3. **Hand-written by the user.** Ask them for five to ten examples off the top of their head. These are usually skewed toward what's salient to them rather than what's frequent, so treat them as a seed, not the whole set. +4. **Synthesized by you from the codebase.** Read the system prompt, the tool descriptions, and any docs or tests, and generate candidate inputs that exercise the flow. This is the lowest-fidelity option - make that clear to the user, and don't do it cold: first get three to five real examples from them (source 3) plus a sentence on what makes a case *hard* in this domain, then synthesize variations of those rather than inventing from the prompt alone. Evals synthesized with nothing real to anchor on come out simplistic, and steering them afterwards costs the user more than writing cases would have. + +Aim for somewhere between fifteen and a hundred inputs for a first eval. Fewer than fifteen and a single flaky case swings the score; well past a hundred and the user won't actually review them all, which defeats the point of the sign-off - for a big set, have them read a stratified sample and lean on `eval-audit.md` §1's programmatic checks for the rest. You can always grow the set later. One caveat: if the user already knows they'll want to **hill-climb** on this eval afterwards, size the set against the change they hope to detect, not just against reviewability - `eval-audit.md` §5 has the arithmetic (noise floor ~ `1/sqrt(n·reps)` for a pass-rate; 25 cases × 2 reps is about ±14 points). Show them that number next to the improvement they'd act on, and budget cases and reps together now: fifty-plus inputs with a random held-out slice, or fewer inputs with more reps, are two routes to the same resolution. Finding out after several paid rounds that the eval couldn't have seen the win is the expensive way. + +### Get the inputs approved + +Show the user the actual inputs - all of them, not a summary. Any observation you offer about the set should be quantitative - counts, named cases, measured scores - not "looks reasonable." Default to a markdown file - a table (`id`, `tags`, `expected`, path of any attached file) followed by one section per case with the input text in a fenced block whose fence is longer than any run of backticks in that text (so a line of backticks in a case cannot close it) - and point the user at it; or, if the cases are already in the Step 3 row shape, run the report builder on them and hand over `report.html`. **Prefer whatever the user already uses to look at prompts and transcripts** - if they have an existing viewer, a notebook they like, or a markdown convention, put the inputs there instead. Match their workflow; the point is that they actually read them. Don't author an ad-hoc HTML page for this: the inputs are sourced from transcripts, tickets and logs, so their text is untrusted, and interpolating it into HTML you wrote yourself is how a `\n'; +} + +const CSS = [ + ':root{color-scheme:light dark;--fg:#1a1a1a;--bg:#fff;--muted:#666;--line:#ddd;--pill:#eee;--warn:#b45309;--best:#ecfdf5}', + '@media(prefers-color-scheme:dark){:root{--fg:#e6e6e6;--bg:#121212;--muted:#9a9a9a;--line:#333;--pill:#2a2a2a;--warn:#f59e0b;--best:#0f2a1f}}', + 'body{font:14px/1.45 system-ui,-apple-system,Segoe UI,Roboto,sans-serif;color:var(--fg);background:var(--bg);margin:0;padding:24px;max-width:1400px}', + 'h1{font-size:22px;margin:0 0 4px}h2{font-size:16px;margin:28px 0 8px}.meta,.hint{color:var(--muted);margin:0 0 8px}', + 'table{border-collapse:collapse;width:100%}th,td{border-bottom:1px solid var(--line);padding:6px 8px;text-align:left;vertical-align:top}', + 'th{cursor:pointer;user-select:none;white-space:nowrap}th.asc:after{content:" \\25B4"}th.desc:after{content:" \\25BE"}', + 'td.num{text-align:right;font-variant-numeric:tabular-nums;white-space:nowrap}td.id{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;white-space:nowrap}', + '.pill{display:inline-block;background:var(--pill);border-radius:10px;padding:0 8px;font-size:12px}.pill.warn{color:var(--warn)}', + 'tr.best td{background:var(--best)}.links{font-size:12px}.links a{margin-right:6px}', + 'td.prompt{max-width:520px}details summary{cursor:pointer;color:var(--muted)}pre{white-space:pre-wrap;font-size:12px;margin:6px 0 0}', + '.warnings{color:var(--warn)}code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace}', +].join(''); + +const SORT_JS = [ + 'document.querySelectorAll("th[data-k]").forEach(function(th){th.addEventListener("click",function(){', + 'var t=th.closest("table"),tb=t.tBodies[0],i=Array.prototype.indexOf.call(th.parentNode.children,th);', + 'var asc=!th.classList.contains("asc");t.querySelectorAll("th").forEach(function(h){h.classList.remove("asc","desc")});', + 'th.classList.add(asc?"asc":"desc");var num=th.dataset.k==="n";', + 'var rows=Array.prototype.slice.call(tb.rows);rows.sort(function(a,b){var x=a.cells[i],y=b.cells[i];', + 'var u=num?parseFloat("v" in x.dataset?x.dataset.v:x.textContent):x.textContent.trim().toLowerCase();', + 'var w=num?parseFloat("v" in y.dataset?y.dataset.v:y.textContent):y.textContent.trim().toLowerCase();', + 'if(num){if(isNaN(u))u=-Infinity;if(isNaN(w))w=-Infinity}return (uw?1:0)*(asc?1:-1)});', + 'rows.forEach(function(r){tb.appendChild(r)})})});', +].join(''); + +const arg = process.argv[2]; +if (!arg || arg === '-h' || arg === '--help') { + eprint('usage: node build-report-lite.mjs (e.g. .claude/hillclimb//)'); + process.exit(arg ? 0 : 2); +} +try { + const { variants, cases, warnings } = build(arg); + for (const w of warnings) eprint('warning: ' + w); + eprint('wrote ' + join(resolve(arg), 'report.html') + ' (' + variants + ' variants, ' + cases + ' cases) and trajectory/scores.tsv'); +} catch (e) { + eprint('build-report-lite: ' + (e?.message || e)); + process.exit(1); +} diff --git a/skills/claude-api/shared/evals/report/runner-scaffold.mjs b/skills/claude-api/shared/evals/report/runner-scaffold.mjs new file mode 100644 index 000000000..2f0220b41 --- /dev/null +++ b/skills/claude-api/shared/evals/report/runner-scaffold.mjs @@ -0,0 +1,602 @@ +#!/usr/bin/env node +// Runner scaffold for the build-eval / hillclimb loop. Copy this into the +// user's repo and fill in loadCases / runCase / gradeCase below - the I/O +// shape, file naming, resume, and CLI surface are already hillclimb-ready +// so adding v2, v3, ... is `--variant v3`, not a refactor. +// +// node run-eval.mjs --flow .claude/hillclimb/ --variant baseline --reps 2 +// +// Structural properties this encodes (so you don't have to remember them): +// - parameterized by --variant / --model / --reps (no hardcoded A/B pair) +// - rep-aware filenames + resume (traces/_rep.json) +// - reads _state.json, never writes it (loop state belongs to the orchestrator) - +// the ONE exception is --approve-harness recording `harness_sha` (see below) +// - refuses to run when the harness (this file + _state.json.harness_paths) has +// changed since the sha a human last approved with --approve-harness, so a +// round that edits the runner cannot execute unreviewed under a standing +// session allowlist +// - pairwise graders judge against frozen baseline/ref/.* on disk +// - writes rows as cases complete (crash-safe) +// - jittered exponential backoff on transient 429/overloaded/5xx errors +// - hard per-case wall-clock ceiling (--timeout-s; stream keepalives don't reset it) +// - served-model assertion (response model must match --model; documented alias->snapshot +// shapes tolerated: 'foo-latest'/'foo-0'/'foo' -> 'foo-20250101' / 'foo@20250101' / 'foo-2025-01-01') +// - failed attempts land in errors.jsonl with a failure class and, when the call +// completed, the billed model/usage (never in results.jsonl) +// - row ids, trace filenames, and frozen refs share one path-safe id +// (original id kept in meta.original_id when sanitization changed it) + +import { createHash } from 'node:crypto'; +import { closeSync, constants as FS, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, writeFileSync, writeSync } from 'node:fs'; +import { dirname, isAbsolute, join, relative, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +// Every output write refuses symlinks: the flow dir is model-influenced, and a +// prompt-injected round can plant `results.jsonl -> ~/.bashrc` where the next +// unattended run would append. POSIX opens O_NOFOLLOW (a symlink fails with +// ELOOP); Windows - where Node leaves O_NOFOLLOW undefined and Bun defines a +// meaningless value - lstat-refuses first. Symlinked parent dirs are +// refused the same way. Same discipline as the report builders' reads. +const WIN = process.platform === 'win32'; +const NOFOLLOW = WIN ? 0 : FS.O_NOFOLLOW; +// A guard that cannot tell must refuse: only "no such entry" reads as absent; +// any other lstat failure (EACCES, ENAMETOOLONG, ...) is rethrown, never "no". +const lstatOrNull = p => { try { return lstatSync(p); } catch (e) { if (e?.code === 'ENOENT') return null; throw e; } }; +const isSymlink = p => lstatOrNull(p)?.isSymbolicLink() === true; +// Stderr lines interpolate model-influenced bytes (case ids, error text that +// can echo model output, JSON.parse messages). Strip escape sequences and +// control characters, as build-report-lite.mjs's eprint does, so a planted +// OSC/CSI can't retitle the terminal or forge output lines. +const ESC_SEQ = /\x1b\[[0-?]*[ -\/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b[@-_]/g; +const CONTROL = /[\x00-\x1f\x7f-\x9f]/g; +const termSafe = s => String(s).replace(ESC_SEQ, '').replace(CONTROL, ''); +const eprint = (...a) => console.error(...a.map(termSafe)); +// The leaf checks above can't see a symlink on an INTERMEDIATE component +// (lstat and open both resolve those silently), so every open is also bound +// to the flow root: main() captures realpathSync(flow) once, and any path +// whose resolved parent leaves it - e.g. `vdir` or the flow dir itself +// replaced by a directory symlink - is refused when the check sees it. +// Residual, all platforms: the check and the open are separate path lookups +// (Node's sync fs has no openat-style call), so a directory swapped for a +// symlink in between is still followed. This stops a planted link, not a +// writer racing the run. +let flowRealRoot = null; +function assertInFlow(dir, what) { + if (flowRealRoot == null) throw new Error(`refusing to ${what}: flow root not resolved yet`); + const dirReal = realpathSync(dir); + if (dirReal !== flowRealRoot && !dirReal.startsWith(flowRealRoot + (WIN ? '\\' : '/'))) + throw new Error(`refusing to ${what}: ${dir} resolves outside the flow directory`); +} +function openNoFollow(p, flags) { + if (isSymlink(dirname(p))) throw new Error(`refusing to open through symlinked directory: ${dirname(p)}`); + assertInFlow(dirname(p), 'open'); + if (WIN && isSymlink(p)) throw new Error(`refusing to open through symlink: ${p}`); + const fd = openSync(p, flags | NOFOLLOW, 0o644); + try { + const st = fstatSync(fd); + if (!st.isFile()) throw new Error(`refusing to use non-regular file: ${p}`); + // O_NOFOLLOW and lstat cannot see a hard link: a second name for a file + // outside the flow dir opens as an ordinary regular file. Nothing the + // runner creates has more than one link, so refuse any that does. + if (st.nlink > 1) throw new Error(`refusing to use ${p}: it has a second hard link (another name for the same file); replace it with a plain copy if it is yours`); + } catch (e) { closeSync(fd); throw e; } + return fd; +} +// writeFileSync on the fd loops until every byte lands (a bare writeSync is +// one write(2) that may return short on ENOSPC and silently truncate a +// results row or trace). +// Opened without O_TRUNC and truncated only after openNoFollow's checks, so a +// refused file keeps its bytes. +function writeFileNoFollow(p, data) { + const fd = openNoFollow(p, FS.O_WRONLY | FS.O_CREAT); + try { ftruncateSync(fd, 0); writeFileSync(fd, data); } finally { closeSync(fd); } +} +// POSIX appends atomically under O_APPEND with no position. On Windows, Bun +// writes an O_APPEND handle at offset 0 unless given a position, so there the +// write starts at the current size and re-issues any short write. +function appendFileNoFollow(p, data) { + const fd = openNoFollow(p, FS.O_WRONLY | FS.O_CREAT | FS.O_APPEND); + try { + if (!WIN) { writeFileSync(fd, data); return; } + const buf = Buffer.from(data); + const start = fstatSync(fd).size; + for (let off = 0; off < buf.length;) { + const n = writeSync(fd, buf, off, buf.length - off, start + off); + if (n <= 0) throw new Error(`append to ${p} made no progress`); + off += n; + } + } finally { closeSync(fd); } +} +// Reads of the frozen pairwise refs get the same discipline as writes (same +// open guard): the flow dir is model-influenced, so `baseline/ref/ -> +// ~/.ssh/id_rsa` planted after the startup preflight must not be read into +// the judge prompt. lexists probes with lstat so a planted symlink still +// counts as "present" at the freeze guard (never overwritten - or followed). +const lexists = p => lstatOrNull(p) != null; +function readFileNoFollow(p) { + const fd = openNoFollow(p, FS.O_RDONLY); + try { return readFileSync(fd, 'utf8'); } finally { closeSync(fd); } +} +// null when the file is absent; any other failure (a planted link included) throws. +function readIfPresent(p) { + try { return readFileNoFollow(p); } catch (e) { if (e?.code === 'ENOENT') return null; throw e; } +} +function mkdirNoFollow(dir) { + if (isSymlink(dir)) throw new Error(`refusing to use symlinked directory: ${dir}`); + mkdirSync(dir, { recursive: true }); + // Check after creating: mkdirSync(recursive) follows symlinked ancestors, + // so a dir minted through one resolves outside the flow root and is refused + // here before any file lands in it. + assertInFlow(dir, 'create directory'); +} +// Frozen pairwise refs may carry an extension; reader and freeze-guard probe +// the same list so a suffixed ref never gets an extensionless shadow. +const REF_EXTS = ['', '.html', '.txt', '.json']; + +// --- fill these in ---------------------------------------------------------- + +/** Return the list of input cases. Each must have a stable `id`. */ +async function loadCases() { + // e.g. return JSON.parse(readFileSync('eval/cases.json', 'utf8')); + throw new Error('TODO: loadCases'); +} + +/** + * Run the app on one input. Return everything the grader and the report need. + * `ctx.model` and `ctx.variant` are the CLI args; use them to pick the model + * and (for hillclimb) the harness/prompt under test. + */ +async function runCase(input, ctx) { + // const res = await client.messages.create({ model: ctx.model, ... }); + // return { + // output: res.content.at(-1).text, // or a file you wrote, an HTML page, ... + // transcript: [...], // Turn[] - see SCHEMA.md + // model: res.model, usage: res.usage, stop_reason: res.stop_reason, + // // optional, for model-graded cost: judge_model, judge_usage + // // optional, per-turn artifacts: attachments: [{kind, ref, alt}] + // }; + throw new Error('TODO: runCase'); +} + +/** + * Grade one output. For pairwise, `ref` is the FROZEN baseline output read + * from disk (baseline/ref/.*) - never a freshly co-generated one. + * Return { grade: {metric_id: number, ...}, explanation?: {metric_id: string}, + * judge_model?, judge_usage? }. + * If the judge call succeeded but grading still fails (parse error, bad + * schema), attach judge_model/judge_usage to the thrown error - the errors + * sidecar reads them so billed judge spend on failed attempts stays counted. + */ +async function gradeCase(input, run, ref, ctx) { + // return { grade: { pass: run.output.includes(input.expected) ? 1 : 0 } }; + throw new Error('TODO: gradeCase'); +} + +/** Side-channel perf fields beyond the built-ins (latency_s etc.). */ +function perfFrom(run) { return {}; } + +// --- harness (you usually won't need to touch below this line) -------------- + +function parseArgs(argv) { + const a = { flow: '.claude/hillclimb/flow', variant: 'baseline', + model: undefined, reps: 1, concurrency: 4, timeoutS: 1800, + approveHarness: false }; + // A flag at the end of argv would otherwise consume undefined - which for + // --model equals the default and silently disables the served-model check. + const val = (i) => { if (argv[i] === undefined) { eprint(`missing value for ${argv[i - 1]}`); usage(); process.exit(2); } return argv[i]; }; + for (let i = 0; i < argv.length; i++) { + const k = argv[i]; + if (k === '--flow') a.flow = val(++i); + else if (k === '--variant') a.variant = val(++i); + else if (k === '--model') a.model = val(++i); + else if (k === '--reps') a.reps = +val(++i); + else if (k === '--concurrency') a.concurrency = +val(++i); + else if (k === '--timeout-s') a.timeoutS = +val(++i); + else if (k === '--approve-harness') a.approveHarness = true; + else if (k === '-h' || k === '--help') { usage(); process.exit(0); } + else { eprint(`unknown argument: ${k}`); usage(); process.exit(2); } + } + if (!/^(baseline|v[1-9]\d*)$/.test(a.variant)) { + // The report only reads directories named 'baseline' or 'v' - any other + // name runs to completion but spends the pass into a directory the Summary, + // trajectory, and budget arithmetic never see. + eprint(`--variant must be 'baseline' or 'v', got '${a.variant}'`); + usage(); process.exit(2); + } + if (!Number.isFinite(a.timeoutS) || a.timeoutS < 0 + || a.timeoutS * 1000 > 2147483647 // setTimeout clamps >2^31-1 ms to 1 ms - the ceiling would fire instantly + || !Number.isInteger(a.reps) || a.reps < 1 + || !Number.isInteger(a.concurrency) || a.concurrency < 1) { usage(); process.exit(2); } + return a; +} +function usage() { + eprint('usage: node run-eval.mjs --flow DIR --variant ID [--model ID] [--reps N] [--concurrency N] [--timeout-s N (0 = no ceiling)] [--approve-harness]'); +} + +// Harness integrity gate. The hillclimb loop gets this runner command +// allowlisted for the session and then runs rounds unattended, while the +// per-round change (proposed by an analyzer fed untrusted transcripts) may +// legitimately edit harness code. Without this gate a round that rewrites the +// runner would execute attacker-chosen code on the next unattended run under +// the user's one-time approval. So: sha256 over this file, any lockfile +// beside it or in the directory the runner is invoked from (dependency drift +// is harness drift too; with neither, a note says so), plus +// every path in `_state.json.harness_paths` (relative to the directory the +// runner is invoked from, i.e. the repo root); compare to +// `_state.json.harness_sha`; refuse on absent/mismatch unless a human passes +// --approve-harness, which records the new sha. That write is the one +// sanctioned exception to "never write _state.json". +// +// Scope, stated plainly: this is a change detector, not a security boundary. +// The sha, the path list and this file all live where the loop agent can +// write, so an agent that is already prompt-injected can forge the record or +// pass --approve-harness itself. What it catches is the common case - a +// round's harness edit reaching an unattended run unreviewed - and what +// actually bounds an unattended run is the permission allowlist the user +// grants the runner command (scope it to this exact command line, not a +// prefix). Installed dependencies beyond the lockfile are outside the digest. +function checkHarness(statePath, st, approve) { + const self = fileURLToPath(import.meta.url); + const listed = Array.isArray(st.harness_paths) ? st.harness_paths.map(String) : []; + const lockfiles = [...new Set([dirname(self), process.cwd()].flatMap(d => + ['package-lock.json', 'bun.lock', 'bun.lockb', 'yarn.lock', 'pnpm-lock.yaml'].map(f => join(d, f))))] + .filter(f => existsSync(f)); + const paths = [...new Set([self, ...lockfiles, ...listed.map(p => resolve(p))])].sort(); + const h = createHash('sha256'); + const hashed = []; + for (const p of paths) { + let buf; + try { buf = readFileSync(p); } + catch (e) { + if (p === self) throw e; + eprint(`warning: harness path '${relative(process.cwd(), p)}' not readable (${e?.code || 'error'}) - skipped`); + continue; + } + h.update(relative(process.cwd(), p)).update('\0').update(buf).update('\0'); + hashed.push(relative(process.cwd(), p)); + } + const sha = h.digest('hex'); + if (st.harness_sha === sha) return; + // Said only here, where a person is about to approve or is being refused. + if (!lockfiles.length) eprint('note: no lockfile beside the runner or in the current directory - dependency changes are outside the harness sha'); + if (approve) { + st.harness_sha = sha; + writeFileNoFollow(statePath, JSON.stringify(st, null, 2) + '\n'); + eprint(`harness approved: sha256 ${sha.slice(0, 12)} over ${hashed.length} file(s) recorded in ${statePath}`); + return; + } + if (st.harness_sha == null) { + eprint(`no approved harness sha in ${statePath} (computed ${sha.slice(0, 12)} over: ${hashed.join(', ')}).`); + eprint('Review the harness, then run once with --approve-harness to record it.'); + } else { + eprint(`harness changed since last approved run (files: ${hashed.join(', ')}); ` + + `approved ${String(st.harness_sha).slice(0, 12)}, now ${sha.slice(0, 12)}.`); + eprint('Re-run with --approve-harness after reviewing the diff.'); + } + process.exit(2); +} + +// Transient provider errors (429 / overloaded / 5xx) retry with jittered +// exponential backoff - a zero-delay retry loop multiplies cost invisibly +// under rate limits and can turn one transient 429 into a torn-down batch. +// The attempt count lands in the row's meta (or the errors sidecar) so retry +// churn is visible in the data, not just the bill. +async function withBackoff(fn, retry, deadline = Infinity, tries = 5) { + for (let attempt = 0; ; attempt++) { + // Checked before every attempt, not just before sleeps: once the case's + // ceiling has passed, an abandoned chain must not issue another call + // (e.g. a judge call after the app call consumed the whole ceiling). + if (Date.now() >= deadline) { + const e = new Error('wall-clock ceiling exceeded before attempt'); + e.failure_class = 'timeout'; + throw e; + } + try { return await fn(); } catch (e) { + const status = e?.status ?? e?.response?.status; + const transient = status === 429 || status === 529 || (status >= 500 && status < 600) + || /overloaded|rate.?limit/i.test(String(e?.message ?? '')); + if (!transient || attempt >= tries - 1) throw e; + const delay = Math.min(60_000, 1000 * 2 ** attempt) * (0.5 + Math.random()); + // Never start a retry that would outlive the case's wall-clock ceiling - + // otherwise an abandoned chain keeps issuing API calls after the case failed. + if (Date.now() + delay >= deadline) throw e; + retry.count++; + await new Promise(r => setTimeout(r, delay)); + } + } +} + +// Hard per-case wall-clock ceiling, independent of stream liveness - a hung +// SSE stream can emit keepalives forever, defeating inactivity-based timers. +// The underlying call may keep running; the case fails and the slot is freed. +function withTimeout(promise, seconds, label) { + if (!(seconds > 0)) return promise; + let timer; + const ceiling = new Promise((_, reject) => { + timer = setTimeout(() => { + const e = new Error(`${label}: exceeded ${seconds}s wall-clock ceiling`); + e.failure_class = 'timeout'; + reject(e); + }, seconds * 1000); + }); + return Promise.race([promise, ceiling]).finally(() => clearTimeout(timer)); +} + +// Case ids appear in file paths AND as the row/file join key the report uses, +// so rows, trace filenames, and frozen refs all carry the same path-safe id. +// When sanitization changes the id, a short content hash keeps distinct ids +// distinct ('case/1' vs 'case_1'); the original rides in meta.original_id. +function pathSafeId(id) { + const raw = String(id); + const cleaned = raw.replace(/[^\w.-]/g, '_'); + // Idempotent by construction: anything already path-safe and within the + // length bound - including this function's own truncated+suffixed output - + // passes through unchanged. Long ids (URLs, prompt text as id) truncate to + // 120 chars plus an 8-hex hash of the full original, so they fail here, not + // at the trace write after the spend, and distinct ids stay distinct. + if (cleaned === raw && raw.length <= 129) return raw; + return `${cleaned.slice(0, 120)}-${createHash('sha256').update(raw).digest('hex').slice(0, 8)}`; +} + +async function main() { + const args = parseArgs(process.argv.slice(2)); + // lstat("link/") follows the final symlink, so a trailing separator on + // --flow would blind every leaf isSymlink check below - strip it first. + // Only Windows treats `\` as a separator; on POSIX it is a filename byte, so + // splitting on it would walk prefixes that are not real path components. + args.flow = args.flow.replace(WIN ? /(.)[\\/]+$/ : /(.)\/+$/, '$1'); + const flowSegments = args.flow.split(WIN ? /[\\/]/ : '/'); + // A `.`/`..` segment (e.g. a trailing `/.`) makes isSymlink(args.flow) below + // resolve a different final component than the named dir - following a + // planted link at the flow root - while join() collapses it and the absolute + // branch skips the ancestor walk. Refuse dot segments outright + // (absolute --flow stays supported). + if (flowSegments.some(seg => seg === '.' || seg === '..')) { + eprint(`refusing to run: --flow must not contain '.' or '..' segments, got '${args.flow}'`); + process.exit(2); + } + const vdir = join(args.flow, args.variant); + // Preflight every output path before the first model call: a planted + // symlink would otherwise fail each case after its (billed) run. + for (const p of [args.flow, join(args.flow, 'baseline'), vdir, join(vdir, 'traces'), + join(vdir, 'results.jsonl'), join(vdir, 'errors.jsonl'), + join(vdir, 'progress.txt'), join(args.flow, 'baseline', 'ref'), join(args.flow, '_state.json')]) + if (isSymlink(p)) { eprint(`refusing to run: ${p} is a symlink (the flow dir must hold regular files)`); process.exit(2); } + // A relative --flow (the documented `.claude/hillclimb/` layout) is + // also lstat-walked component by component from the cwd: a pre-planted + // link at an ancestor (`.claude/hillclimb -> elsewhere`) would otherwise + // relocate the root capture below - the containment anchor itself - to the + // attacker's target. An absolute --flow is the caller's own trust decision + // and is not walked (an absolute ancestor link can be legitimate: /tmp on + // macOS). + if (!isAbsolute(args.flow)) { + let walk = ''; + for (const part of flowSegments.filter(Boolean).slice(0, -1)) { + walk = walk ? join(walk, part) : part; + if (isSymlink(walk)) { eprint(`refusing to run: ${walk} is a symlink (ancestor of --flow)`); process.exit(2); } + } + } + // Every later open/mkdir is bound to this resolved root (see assertInFlow): + // create the flow dir when fresh (the preflight above refused a link at it + // and, for a relative path, at every ancestor), then capture where it + // really resolves. + mkdirSync(args.flow, { recursive: true }); + flowRealRoot = realpathSync(args.flow); + mkdirNoFollow(join(vdir, 'traces')); + // _state.json is READ-ONLY here. The orchestrator owns it. Absent is fine + // (a baseline-only run has no loop state yet), but present-and-unparsable + // must not let the id-space gate below pass vacuously over a corrupt file. + const statePath = join(args.flow, '_state.json'); + let st = {}; + // Read through the no-follow opener like every other flow-dir file; the + // parse message is not echoed (it can quote the file's first bytes). + const stateText = readIfPresent(statePath); + if (stateText != null) { + try { st = JSON.parse(stateText) || {}; } + catch { eprint(`${statePath} exists but is not valid JSON - fix it before spending a pass`); process.exit(2); } + } + checkHarness(statePath, st, args.approveHarness); + const ctx = { ...args, state: st }; + + // Resume: which (id, rep) pairs already have a row? + const resultsPath = join(vdir, 'results.jsonl'); + const done = new Set(); + for (const ln of (readIfPresent(resultsPath) ?? '').split('\n')) { + if (!ln.trim()) continue; + try { const r = JSON.parse(ln); done.add(`${r.prompt_id}\0${r.rep}`); } catch {} + } + // Rows key on the path-safe id (see pathSafeId), so resume must too. + + const cases = await loadCases(); + // Validate the id space before spending anything: duplicate path-safe ids - + // including case-insensitive twins, which macOS/Windows filesystems collapse - + // would silently overwrite traces and frozen refs; and a _state.json split id + // that matches no case would silently shrink the scored denominator. + const seen = new Map(); + for (const c of cases) { + const k = pathSafeId(c.id).toLowerCase(); + if (seen.has(k)) { + eprint(`duplicate case id after sanitization: '${c.id}' collides with '${seen.get(k)}'`); + process.exit(2); + } + seen.set(k, c.id); + } + const safeIds = new Set(cases.map(c => pathSafeId(c.id))); + for (const k of ['train_ids', 'val_ids', 'test_ids']) + if (st[k] != null && !Array.isArray(st[k])) { eprint(`_state.json ${k} must be a list of ids`); process.exit(2); } + for (const sid of [...(st.train_ids ?? []), ...(st.val_ids ?? []), ...(st.test_ids ?? [])]) { + const s = String(sid); // the adapter joins with String() on both sides - numeric ids are fine + if (safeIds.has(s)) continue; // matches a loaded case - definitionally valid + if (s !== pathSafeId(s)) { + // Can never match a row: rows key on path-safe ids. This is the silent + // shrunken-denominator bug - fail before anything is spent. + eprint(`_state.json split id '${s}' is not a path-safe id - record split ids exactly as they appear in results.jsonl's prompt_id`); + process.exit(2); + } + // Well-formed but absent is legitimate (a trimmed top-K subset run) - note it, don't fail. + eprint(`note: split id '${s}' matches no loaded case (expected for a trimmed subset run)`); + } + const refDir = join(args.flow, 'baseline', 'ref'); + const tasks = []; + for (const c of cases) for (let rep = 0; rep < args.reps; rep++) { + if (done.has(`${pathSafeId(c.id)}\0${rep}`)) continue; + tasks.push({ c, rep }); + } + eprint(`[${args.variant}] ${tasks.length} of ${cases.length * args.reps} (id,rep) to run`); + + let i = 0, ok = 0, fail = 0; + const errorsPath = join(vdir, 'errors.jsonl'); + // A hard crash (power loss, ENOSPC) can leave a torn final line with no + // trailing newline; the next append would merge two rows into one permanently + // unparseable line. Isolate any fragment before appending anything. + for (const p of [resultsPath, errorsPath]) { + const tail = readIfPresent(p); + if (tail && !tail.endsWith('\n')) appendFileNoFollow(p, '\n'); + } + async function worker() { + while (i < tasks.length) { + const { c, rep } = tasks[i++]; + const safeId = pathSafeId(c.id); + const t0 = Date.now(); + let lastRun = null; // survives into the catch - billed spend on a failed attempt + let rowWritten = false; // set once the results row lands - the attempt is scored + const deadline = args.timeoutS > 0 ? t0 + args.timeoutS * 1000 : Infinity; + const appRetry = { count: 0 }, judgeRetry = { count: 0 }; + try { + // One ceiling over the whole case - app call, identity check, and grading - + // so a hung judge stream can't hold the slot either. + const { run, g, latency_s } = await withTimeout((async () => { + let tAttempt = t0; + const run = await withBackoff(() => { tAttempt = Date.now(); return runCase(c, ctx); }, + appRetry, deadline); + lastRun = run; + // latency_s = the final app attempt only; backoff sleeps, failed + // attempts, and judge time are excluded (retry counts are in meta). + const latency_s = (Date.now() - tAttempt) / 1000; + // Serving identity: fail loudly when the response was served by a model + // other than the one requested. Accept exact match or a documented + // alias->snapshot resolution - 'foo-latest'/'foo-0'/'foo' served as + // 'foo-20250101', 'foo@20250101', or 'foo-2025-01-01'. Anything else - + // another snapshot of the requested pin, a sibling model, or the bare + // base id ('foo-latest' served as 'foo', an unversioned echo that can + // hide snapshot drift across rounds) - fails the attempt. Non-Anthropic + // id schemes (e.g. Bedrock's 'anthropic.claude-...-v1:0') need their own + // rule here. + if (ctx.model && run.model && run.model !== ctx.model) { + const base = ctx.model.replace(/-latest$|-0$/, ''); + const rest = String(run.model).startsWith(base) + ? String(run.model).slice(base.length) : null; + if (!(rest != null && /^[-@](\d{8}|\d{4}-\d{2}-\d{2})$/.test(rest))) { + const e = new Error(`served model ${run.model} != requested ${ctx.model}`); + e.failure_class = 'serving_substitution'; + throw e; + } + } + // Frozen pairwise reference (never regenerated): baseline/ref/.* + let ref = null; + if (args.variant !== 'baseline') { + const p = join(refDir, safeId); + // A planted symlink throws (ELOOP) rather than feeding the judge + // its target; the case then fails loudly instead of leaking. + for (const ext of REF_EXTS) { + try { ref = readFileNoFollow(p + ext); break; } + catch (e) { if (e?.code !== 'ENOENT') throw e; } + } + } + const g = await withBackoff(() => gradeCase(c, run, ref, ctx), judgeRetry, deadline); + return { run, g, latency_s }; + })(), args.timeoutS, `${c.id} rep${rep}`); + const row = { + prompt_id: safeId, rep, prompt: c.prompt ?? c.input ?? c.id, + tags: c.tags, attachments: c.attachments, + meta: safeId !== String(c.id) || appRetry.count || judgeRetry.count + ? { ...(c.meta ?? {}), + ...(safeId !== String(c.id) ? { original_id: String(c.id) } : {}), + ...(appRetry.count ? { retries: appRetry.count } : {}), + ...(judgeRetry.count ? { judge_retries: judgeRetry.count } : {}) } + : c.meta, + model: run.model, usage: run.usage, stop_reason: run.stop_reason, + // The report keys on `status`, not stop_reason: a clipped answer is + // counted and shown but kept out of the means. runCase may set + // run.status to override the max_tokens rule. + status: run.status ?? (run.stop_reason === 'max_tokens' ? 'truncated' : 'ok'), + judge_model: g.judge_model ?? run.judge_model, + judge_usage: g.judge_usage ?? run.judge_usage, + latency_s, ...perfFrom(run), + grade: g.grade, explanation: g.explanation, + }; + appendFileNoFollow(resultsPath, JSON.stringify(row) + '\n'); + rowWritten = true; // past this point the attempt is scored - a later throw (trace write, ref freeze) must not also append an error row + if (run.transcript) + writeFileNoFollow(join(vdir, 'traces', `${safeId}_rep${rep}.json`), + JSON.stringify(run.transcript, null, 2)); + // For pairwise: on the baseline run, freeze the reference output once. + if (args.variant === 'baseline' && run.output != null + && !REF_EXTS.some(ext => lexists(join(refDir, safeId) + ext))) { + mkdirNoFollow(refDir); + writeFileNoFollow(join(refDir, safeId), + typeof run.output === 'string' ? run.output : JSON.stringify(run.output)); + } + ok++; + } catch (e) { + fail++; + if (rowWritten) { + // The attempt scored; only a post-row write (trace, ref) failed. An error + // row here would double-count the billed usage under the budget rule. + eprint(` [${args.variant}] ${c.id} rep${rep} scored, but a post-row write failed: ${e?.message || e}`); + continue; + } + // Failed attempts are data too - but they must not occupy the (case, rep) + // slot in results.jsonl, or resume would never re-run them. + appendFileNoFollow(errorsPath, JSON.stringify({ + prompt_id: safeId, rep, + ...(safeId !== String(c.id) ? { original_id: String(c.id) } : {}), + failure_class: e?.failure_class ?? 'error', + error: String(e?.message || e), + retries: appRetry.count, judge_retries: judgeRetry.count, + // Billed-but-failed spend stays countable: when the app call completed + // before the failure (e.g. a served-model mismatch, a judge-stage + // ceiling), carry its identity and usage on the error row. + model: lastRun?.model, usage: lastRun?.usage, + judge_model: e?.judge_model ?? lastRun?.judge_model, + judge_usage: e?.judge_usage ?? lastRun?.judge_usage, + latency_s: (Date.now() - t0) / 1000, + }) + '\n'); + eprint(` [${args.variant}] ${c.id} rep${rep} FAILED: ${e?.message || e}`); + } + } + } + // One progress line every 30s (and to /progress.txt) so "how far along + // is it?" is answerable from the background shell's output or one file read, + // without the orchestrator parsing results.jsonl mid-write. ETA is a plain + // rate extrapolation from this pass. + const t0 = Date.now(); + const progress = () => { + const done = ok + fail, total = tasks.length; + const el = (Date.now() - t0) / 1000; + const eta = done ? Math.round((el / done) * (total - done)) : null; + const line = `[${args.variant}] ${done}/${total} done (${ok} ok, ${fail} failed), ` + + `${Math.round(el)}s elapsed` + (eta != null ? `, ~${eta}s left` : ''); + eprint(line); + try { writeFileNoFollow(join(vdir, 'progress.txt'), line + '\n'); } catch {} + }; + const tick = setInterval(progress, 30_000); + workersStarted = true; + await Promise.all(Array.from({ length: Math.max(1, args.concurrency) }, worker)); + clearInterval(tick); progress(); + eprint(`[${args.variant}] done - ${ok} ok, ${fail} failed -> ${resultsPath}`); + process.exit(fail ? 1 : 0); +} + +// Anything main() throws prints as one sanitized line, not a raw stack. Before +// the workers start it is a refusal (a planted link at _state.json or +// results.jsonl, an lstat that fails, an error from loadCases) and exits 2 like +// the preflight refusals. After they start, only a failed errors.jsonl append +// gets here; rows may already be on disk, so say that and exit 1. +let workersStarted = false; +main().catch(e => { + const m = String(e?.message || e); + if (workersStarted) { eprint('stopped mid-run (rows already written are kept; re-run to resume): ' + m); process.exit(1); } + eprint(m.startsWith('refusing to ') ? m : 'refusing to run: ' + m); + process.exit(2); +}); diff --git a/skills/claude-api/shared/live-sources.md b/skills/claude-api/shared/live-sources.md index 92434f657..f2bba9290 100644 --- a/skills/claude-api/shared/live-sources.md +++ b/skills/claude-api/shared/live-sources.md @@ -18,7 +18,7 @@ This file contains WebFetch URLs for fetching current information from platform. | Models Overview | `https://platform.claude.com/docs/en/about-claude/models/overview.md` | "Extract current model IDs, context windows, and pricing for all Claude models" | | Migration Guide | `https://platform.claude.com/docs/en/about-claude/models/migration-guide.md` | "Extract breaking changes, deprecated parameters, and per-model migration steps when moving to a newer Claude model" | | Introducing Claude Fable 5 | `https://platform.claude.com/docs/en/about-claude/models/introducing-claude-fable-5.md` | "Extract capabilities, API changes, and availability stages for Claude Fable 5 and Claude Mythos 5" | -| Pricing | `https://platform.claude.com/docs/en/pricing.md` | "Extract current pricing per million tokens for input and output" | +| Pricing | `https://platform.claude.com/docs/en/about-claude/pricing.md` | "Extract current pricing per million tokens for input and output" | | Cost Optimization | `https://platform.claude.com/docs/en/about-claude/models/optimizing-for-cost-and-intelligence.md` | "Extract measured cost levers, cache and batch savings, effort and model cost-per-task comparisons, budget controls, and multi-model guidance" | ### Core Features @@ -26,7 +26,7 @@ This file contains WebFetch URLs for fetching current information from platform. | Topic | URL | Extraction Prompt | | ----------------- | ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | | Extended Thinking | `https://platform.claude.com/docs/en/build-with-claude/extended-thinking.md` | "Extract extended thinking parameters, budget_tokens requirements, and usage examples" | -| Adaptive Thinking | `https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking.md` | "Extract adaptive thinking setup, effort levels, and Claude Opus 5 usage examples" | +| Adaptive Thinking | `https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking.md` | "Extract adaptive thinking setup, effort levels, and Claude Opus 5.5 usage examples" | | Effort Parameter | `https://platform.claude.com/docs/en/build-with-claude/effort.md` | "Extract effort levels, cost-quality tradeoffs, and interaction with thinking" | | Tool Use | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview.md` | "Extract tool definition schema, tool_choice options, and handling tool results" | | Streaming | `https://platform.claude.com/docs/en/build-with-claude/streaming.md` | "Extract streaming event types, SDK examples, and best practices" | @@ -69,13 +69,13 @@ This file contains WebFetch URLs for fetching current information from platform. | Topic | URL | Extraction Prompt | | -------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | | Code Execution | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool.md` | "Extract code execution tool setup, file upload, container reuse, and response handling" | -| Computer Use | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use.md` | "Extract computer use tool setup, capabilities, and implementation examples" | +| Computer Use | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool.md` | "Extract the computer_toolset_20260801 setup (configs, member tools, batch actions, toolset_name on results), the Compatibility matrix, and the migration steps from computer_20251124" | | Bash Tool | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/bash-tool.md` | "Extract bash tool schema, reference implementation, and security considerations" | | Text Editor | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/text-editor-tool.md` | "Extract text editor tool commands, schema, and reference implementation" | | Memory Tool | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool.md` | "Extract memory tool commands, directory structure, and implementation patterns" | | Tool Search | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool.md` | "Extract tool search setup, when to use, and cache interaction" | | Programmatic Tool Calling | `https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling.md` | "Extract PTC setup, script execution model, and tool invocation from code" | -| Skills | `https://platform.claude.com/docs/en/agents-and-tools/skills.md` | "Extract skill folder structure, SKILL.md format, and loading behavior" | +| Skills | `https://platform.claude.com/docs/en/agents-and-tools/agent-skills/overview.md` | "Extract skill folder structure, SKILL.md format, and loading behavior" | | Skills Guide | `https://platform.claude.com/docs/en/build-with-claude/skills-guide.md` | "Extract the Skills API (/v1/skills) usage and the migration steps from skills-2025-10-02" | ### Advanced Features @@ -120,11 +120,12 @@ Use these when a managed-agents binding, behavior, or wire-level detail isn't co ### Anthropic CLI -The `ant` CLI provides terminal access to the Claude API. Every API resource is exposed as a subcommand. It is the recommended way to create agents and environments from version-controlled YAML (`ant beta:agents create < agent.yaml` - see `shared/anthropic-cli.md`), and also exposes sessions and every other API resource for scripting and interactive inspection. +The `ant` CLI provides terminal access to the Claude API. Every API resource is exposed as a subcommand. It is the recommended way to keep agents, environments, skills, memory stores and deployments as version-controlled files (`ant apply` - see `shared/anthropic-cli.md`), and also exposes sessions and every other API resource for scripting and interactive inspection. | Topic | URL | Extraction Prompt | | ------------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -| Anthropic CLI | `https://platform.claude.com/docs/en/api/sdks/cli.md` | "Extract CLI install, authentication, command structure, and the beta:agents/environments/sessions commands" | +| Anthropic CLI | `https://platform.claude.com/docs/en/cli-sdks-libraries/cli/quickstart.md` | "Extract CLI install, authentication, command structure, and sending a first request" | +| `ant apply` | `https://platform.claude.com/docs/en/cli-sdks-libraries/cli/apply.md` | "Extract the file layout per resource kind, how a file's kind is inferred, path references between files, `claude-lock.json`, the flags (`--dry-run`, `--yes`, `--force`, `--prune`, `--upgrade`, `--lock-file`), and the CI setup" | | `ant beta:sessions connect` | `https://platform.claude.com/docs/en/cli-sdks-libraries/cli/sessions-connect.md` | "Extract the interactive session viewer: keybindings, tool-call allow/deny prompt, `--web` local viewer and its URL/lifetime rules" | | Authentication overview | `https://platform.claude.com/docs/en/manage-claude/authentication.md` | "Extract the credential options (API keys, interactive OAuth login, Workload Identity Federation) and when to use each" | | WIF reference | `https://platform.claude.com/docs/en/manage-claude/wif-reference.md` | "Extract credential precedence order, the profile configuration file schema, and the configuration directory layout" | diff --git a/skills/claude-api/shared/managed-agents-api-reference.md b/skills/claude-api/shared/managed-agents-api-reference.md index 6f7a5e95b..cbea5292a 100644 --- a/skills/claude-api/shared/managed-agents-api-reference.md +++ b/skills/claude-api/shared/managed-agents-api-reference.md @@ -2,7 +2,7 @@ All endpoints require `x-api-key` and `anthropic-version: 2023-06-01` headers. Managed Agents endpoints additionally require the `anthropic-beta` header. -> Most users should define agents and environments as version-controlled YAML applied with the `ant` CLI - see `shared/anthropic-cli.md`. The endpoints below are the underlying API that the CLI and SDKs drive. +> Most users should define agents and environments as version-controlled files synced with `ant apply` - see `shared/anthropic-cli.md`. The endpoints below are the underlying API that the CLI and SDKs drive. ## Beta Headers @@ -10,7 +10,7 @@ All endpoints require `x-api-key` and `anthropic-version: 2023-06-01` headers. M anthropic-beta: managed-agents-2026-04-01 ``` -The SDK adds this header automatically for all `client.beta.{agents,environments,sessions,vaults,memory_stores,deployments,deployment_runs}.*` calls. Skills endpoints use `skills-2025-10-02`; Files endpoints use `files-api-2025-04-14`. +The SDK adds this header automatically for all `client.beta.{agents,environments,sessions,vaults,deployments,deployment_runs}.*` calls. Memory store endpoints (`client.beta.memory_stores.*`) use `agent-memory-2026-07-22` instead, which the SDK also sets; sending both headers on a memory store request returns a 400. The Files and Skills APIs are out of beta and need no beta header. --- @@ -44,7 +44,7 @@ All resources are under the `beta` namespace. Python and TypeScript share identi **Agent shorthand:** `agent` on session create accepts three forms - a bare string (`agent="agent_abc123"`, latest version), a pinned reference `{type: "agent", id, version}`, or `{type: "agent_with_overrides", id, version?, model?, system?, tools?, mcp_servers?, skills?}` to override those fields for this session only (see `shared/managed-agents-core.md` -> Override agent configuration for a session). -**Model shorthand:** `model` on agent create accepts either a bare string (`model="claude-opus-5"` - uses `standard` speed) or the full config object, which takes `speed`, `effort`, and `inference_geo` alongside `id`: `{id: "claude-opus-5", speed: "fast"}`, `{id: "claude-opus-5", effort: "high"}`, `{id: "claude-opus-5", inference_geo: "us"}`. `effort` accepts a level string (`low`/`medium`/`high`/`xhigh`/`max`) or `{type: ""}`, and is **agent-configuration only** - an `effort` inside a per-session `model` override is ignored. `inference_geo` (`"us"` | `"global"`) pins the geography serving the agent's model requests, and unlike `effort` **is** applied in a per-session `model` override. See `shared/managed-agents-core.md` -> Effort on the agent model / Pinning inference geography. Note: `speed: "fast"` is supported on Claude Opus 5 and Opus 4.8 - on the Claude API only, which includes Managed Agents but not Amazon Bedrock, Google Cloud, or Microsoft Foundry. Opus 4.7 fast mode has been removed; `speed: "fast"` on Opus 4.7 returns an error. +**Model shorthand:** `model` on agent create accepts either a bare string (`model="claude-opus-5-5"` - uses `standard` speed) or the full config object, which takes `speed`, `effort`, and `inference_geo` alongside `id`: `{id: "claude-opus-5-5", speed: "fast"}`, `{id: "claude-opus-5-5", effort: "high"}`, `{id: "claude-opus-5-5", inference_geo: "us"}`. `effort` accepts a level string (`low`/`medium`/`high`/`xhigh`/`max`) or `{type: ""}`, and in a per-session `model` override it sets the session's effort level (the agent's own `effort` isn't carried over, and a `model` override without `effort` runs at that model's default effort level). `inference_geo` (`"us"` | `"global"`) pins the geography serving the agent's model requests, and is also applied in a per-session `model` override. See `shared/managed-agents-core.md` -> Effort on the agent model / Pinning inference geography. Note: `speed: "fast"` is supported on Claude Opus 5.5, Claude Opus 5, and Opus 4.8 - on the Claude API only, which includes Managed Agents but not Amazon Bedrock, Google Cloud, or Microsoft Foundry. Opus 4.7 fast mode has been removed; `speed: "fast"` on Opus 4.7 returns an error. --- @@ -235,7 +235,7 @@ Immutable per-mutation snapshots (`memver_...`) - the audit and rollback surface ```json { "name": "string (required, 1-256 chars)", - "model": "claude-opus-5 (required - bare string, or {id, speed?, effort?, inference_geo?} object)", + "model": "claude-opus-5-5 (required - bare string, or {id, speed?, effort?, inference_geo?} object)", "description": "string (optional, up to 2048 chars)", "system": "string (optional, up to 100,000 chars)", "tools": [ @@ -298,7 +298,7 @@ Immutable per-mutation snapshots (`memver_...`) - the audit and rollback surface } ``` -> The `agent` field accepts a string ID, `{type: "agent", id, version}`, or `{type: "agent_with_overrides", id, version?, ...}` for session-local overrides of `model`/`system`/`tools`/`mcp_servers`/`skills`. Outside the overrides form, those fields live on the agent, not here. An `effort` inside a `model` override is ignored - set it on the agent. An `inference_geo` inside a `model` override **is** applied (omitting it clears the agent's pin for this session). +> The `agent` field accepts a string ID, `{type: "agent", id, version}`, or `{type: "agent_with_overrides", id, version?, ...}` for session-local overrides of `model`/`system`/`tools`/`mcp_servers`/`skills`. Outside the overrides form, those fields live on the agent, not here. An `effort` inside a `model` override is applied (the agent's own `effort` isn't carried over, and a `model` override without `effort` runs at that model's default effort level). An `inference_geo` inside a `model` override **is** applied (omitting it clears the agent's pin for this session). > > **`budget`** (optional, create-only) is a hard dollar cap on the session's list-priced spend; `amount` is an integer string in minor units (cents - `"2500"` = $25.00), `USD` only. It can be changed or removed later via session update, never added. See `shared/managed-agents-core.md` -> Session budgets. > @@ -361,7 +361,7 @@ Immutable per-mutation snapshots (`memver_...`) - the audit and rollback surface } ``` -> `system.message` events (append system-level context for this turn and later ones) use the same envelope with `type: "system.message"` - supported on Claude Opus 5, Claude Opus 4.8, Claude Sonnet 5, Claude Fable 5.1, and Claude Mythos 5.1, checked against the agent's *primary* model only; see `shared/managed-agents-events.md` § Adding system context mid-session. +> `system.message` events (append system-level context for this turn and later ones) use the same envelope with `type: "system.message"` - supported on Claude Fable 5.1, Claude Mythos 5.1, Claude Fable 5, Claude Mythos 5, Claude Opus 5.5, Claude Opus 5, Claude Opus 4.8, and Claude Sonnet 5.5 (not Claude Sonnet 5), checked against the agent's *primary* model only; see `shared/managed-agents-events.md` § Adding system context mid-session. ### Define Outcome Event diff --git a/skills/claude-api/shared/managed-agents-core.md b/skills/claude-api/shared/managed-agents-core.md index d352371f8..376b83c7c 100644 --- a/skills/claude-api/shared/managed-agents-core.md +++ b/skills/claude-api/shared/managed-agents-core.md @@ -110,7 +110,7 @@ Key fields returned by the API: const agent = await client.beta.agents.create( { name: "Coding Assistant", - model: "claude-opus-5", + model: "claude-opus-5-5", system: "You are a helpful coding agent.", tools: [{ type: "agent_toolset_20260401"}], }, @@ -241,25 +241,25 @@ The agent is a **persistent resource**, not a per-run parameter. The intended pa **Anti-pattern:** calling `agents.create()` at the top of every script run. This accumulates orphaned agent objects, pays create latency on every invocation, and defeats the versioning model. If you see `agents.create()` in a function that's called per-request or per-cron-tick, that's wrong - hoist it to one-time setup and persist the ID. -> **Recommended - define agents and environments as YAML + apply via the `ant` CLI.** The split is **CLI for the control plane, SDK for the data plane**: agents and environments are relatively static resources you manage with `ant` (version-controlled YAML, applied from CI); sessions are dynamic and driven by your application through the SDK. See `shared/anthropic-cli.md` -> *Version-controlled Managed Agents resources* for the `ant beta:agents create < agent.yaml` / `update --version N` flow. The SDK `agents.create()` call shown elsewhere in this doc is the in-code equivalent - use it when you need to provision programmatically, but prefer the YAML flow for anything a human maintains. +> **Recommended - define agents and environments as files and sync them with `ant apply`.** The split is **CLI for the control plane, SDK for the data plane**: agents and environments are relatively static resources you manage with `ant` (version-controlled files, synced by hand or from CI); sessions are dynamic and driven by your application through the SDK. See `shared/anthropic-cli.md` -> *Version-controlled Managed Agents resources* for the file layout, `claude-lock.json`, and the CI flow. The SDK `agents.create()` call shown elsewhere in this doc is the in-code equivalent - use it when you need to provision programmatically, but prefer files + `ant apply` for anything a human maintains. ### Effort on the agent model -Pass `model` as an object to set the effort level: `{"id": "claude-opus-5", "effort": "high"}`. `effort` accepts a level string (`low`, `medium`, `high`, `xhigh`, `max`) or an object such as `{"type": "high"}`. The create/update response echoes it in object form and fills in omitted `model` fields with their defaults. +Pass `model` as an object to set the effort level: `{"id": "claude-opus-5-5", "effort": "high"}`. `effort` accepts a level string (`low`, `medium`, `high`, `xhigh`, `max`) or an object such as `{"type": "high"}`. The create/update response echoes it in object form and fills in omitted `model` fields with their defaults. -> Warning: **Effort is agent configuration only.** An `effort` set inside a per-session `model` override is **not applied** - the session runs at the agent's effort. To change effort you must update the agent (or point the session at a different agent). This is the one field where the override form silently does nothing rather than erroring. +> Warning: **A per-session `model` override replaces the agent's `model` object in full, so the agent's own `effort` isn't carried over.** To run the session at a specific effort level, set `effort` inside the override's `model` object. A level the model doesn't support returns a 400 error, and a `model` override without `effort` runs at that model's default effort level. -The same object form carries `speed` for fast mode: `{"id": "claude-opus-5", "speed": "fast"}`. +The same object form carries `speed` for fast mode: `{"id": "claude-opus-5-5", "speed": "fast"}`. ### Pinning inference geography (`inference_geo`) -The `model` object also takes `inference_geo` to pin the geography that serves the agent's model requests: `{"id": "claude-opus-5", "inference_geo": "us"}`. Accepts `"us"` or `"global"` - and unlike the Messages API, where `inference_geo` is a top-level request parameter, here it is always nested inside `model`, never top-level. When unset, each model request follows the workspace's default inference geo at the time it's served. +The `model` object also takes `inference_geo` to pin the geography that serves the agent's model requests: `{"id": "claude-opus-5-5", "inference_geo": "us"}`. Accepts `"us"` or `"global"` - and unlike the Messages API, where `inference_geo` is a top-level request parameter, here it is always nested inside `model`, never top-level. When unset, each model request follows the workspace's default inference geo at the time it's served. - **Validated at every stage:** the pin is checked against the workspace's `allowed_inference_geos` when the agent is saved, when a session is created from it, and on every turn the session serves. If the workspace allowlist later narrows so the pin is no longer allowed, new sessions can't be created from the agent and **running sessions refuse further turns** - pins are never grandfathered (workspaces rely on them for compliance). - Setting `inference_geo` on a model that doesn't support geographic inference pinning returns a 400. - **Fixed for a session's lifetime** - the pin can't change mid-session. Set it on the agent, or set/clear it for one session with a `model` override at session create (see § Override agent configuration for a session). - **Multiagent rosters must be geo-uniform:** the coordinator's pin and every roster member's must all be the same value or all be unset - see `shared/managed-agents-multiagent.md`. -- Unlike `effort`, an `inference_geo` inside a per-session `model` override **is applied** - and because overrides replace the `model` object in full, an override that *omits* `inference_geo` clears the agent's pin for that session. +- Like `effort`, an `inference_geo` inside a per-session `model` override **is applied** - and because overrides replace the `model` object in full, an override that *omits* `inference_geo` clears the agent's pin for that session. ### Versioning @@ -270,7 +270,7 @@ Each `POST /v1/agents/{id}` (update) creates a new immutable version - a sequent | `version` | Behavior | Fits | |---|---|---| | Supplied (must be >= 1) | 409 if it doesn't match the agent's current version - **even when the fields you send already equal the stored values**. Re-read and retry. | Interactive callers; the recommended default | -| Omitted | Applies unconditionally. The most recent update silently replaces any concurrent one, with no error to either caller. | Declarative apply loops - e.g. a CI job syncing checked-in agent definitions, where the loop owns the agent | +| Omitted | Applies unconditionally. The most recent update silently replaces any concurrent one, with no error to either caller. | Hand-rolled sync loops - e.g. a CI script pushing checked-in agent definitions with `agents.update()`, where the loop owns the agent | **Update semantics.** Omitted fields are preserved. Scalar fields (`model`, `system`, `name`, `description`) are replaced; `system` and `description` can be cleared with `null`, while `model` and `name` cannot. Array fields (`tools`, `mcp_servers`, `skills`) are replaced wholesale - `null` or `[]` clears them. **`effort` is the sole exception inside a `model` object you supply:** if the model `id` is unchanged, omitting `effort` leaves the stored level alone; if you change the `id`, an omitted `effort` resets to the new model's default. Other `model` fields are replaced along with the object - **supplying `model` without `inference_geo` clears the agent's inference geo pin.** @@ -324,7 +324,7 @@ session = client.beta.sessions.create( agent={ "type": "agent_with_overrides", "id": agent.id, - "model": "claude-opus-5", # replace the agent's model for this session + "model": "claude-opus-5-5", # replace the agent's model for this session "system": None, # clear the system prompt for this session }, environment_id=environment_id, @@ -334,7 +334,7 @@ session = client.beta.sessions.create( Each overridable field follows tri-state rules: - **Omit** -> the session inherits the value from the referenced agent version. - **`null` (or `[]` for list fields)** -> the session runs with that field cleared. Applies in full to `system` and `skills`. Three exceptions: `model` is never clearable (`model: null` -> 400 `agent_model_required`); clearing `tools` returns 400 when the session's effective `skills` is non-empty (skills require the `read` tool); and clearing `mcp_servers` returns 400 when the effective `tools` still contains an `mcp_toolset` referencing one of the agent's servers - override `tools` in the same request to drop those entries, then clear `mcp_servers`. -- **A value** -> replaces the agent's value **in full**. Overrides never merge - a `tools` override must list every tool the session should have. One exception: an `effort` level inside a `model` override is **not applied** (set it on the agent instead - see § Effort on the agent model). An `inference_geo` inside a `model` override **is** applied - and because the object is replaced in full, an override that omits it clears the agent's pin, so the session follows the workspace's default inference geo. The overridden value is validated against the workspace's `allowed_inference_geos` at session create. +- **A value** -> replaces the agent's value **in full**. Overrides never merge - a `tools` override must list every tool the session should have. A `model` override also replaces the agent's `model` object in full: the agent's own `effort` isn't carried over, so set `effort` inside the override's `model` object to run the session at a specific effort level (a level the model doesn't support returns a 400 error, and a `model` override without `effort` runs at that model's default effort level). An `inference_geo` inside a `model` override **is** applied - and because the object is replaced in full, an override that omits it clears the agent's pin, so the session follows the workspace's default inference geo. The overridden value is validated against the workspace's `allowed_inference_geos` at session create. Overrides are session-local: they do **not** modify the agent resource or create a new agent version. The response's `agent` object reflects the post-override configuration, while its `id` and `version` still identify the base agent - so you can trace a session back to its base. In multiagent sessions, overrides apply to the coordinator and its `{type: "self"}` copies; roster agents referenced by ID always use their own as-created configuration (see `shared/managed-agents-multiagent.md`). diff --git a/skills/claude-api/shared/managed-agents-environments.md b/skills/claude-api/shared/managed-agents-environments.md index 1c86e8e95..9cd185b40 100644 --- a/skills/claude-api/shared/managed-agents-environments.md +++ b/skills/claude-api/shared/managed-agents-environments.md @@ -109,7 +109,7 @@ for await (const f of client.beta.files.list({ **Requirements:** - The `write` tool (or `bash`) must be enabled for the agent to create output files. - Session-scoped `files.list` / `files.download` captures outputs written to `/mnt/session/outputs/`. -- The filter parameter is **`scope_id`** (REST query param `?scope_id=`). The SDK's files resource auto-adds only the `files-api-2025-04-14` header, so pass `betas: ["managed-agents-2026-04-01"]` explicitly (or both headers on raw HTTP) - without it the API may reject `scope_id` as an unknown field. Requires `@anthropic-ai/sdk` >= 0.88.0 / `anthropic` (Python) >= 0.92.0 - older versions don't type `scope_id`. The `ant` CLI does **not** expose this flag yet; use the SDK or curl. +- The filter parameter is **`scope_id`** (REST query param `?scope_id=`). Filtering by `scope_id` requires the `managed-agents-2026-04-01` header, which `client.beta.files` does not add, so pass `betas: ["managed-agents-2026-04-01"]` explicitly (on raw HTTP, send `anthropic-beta: managed-agents-2026-04-01`); the list call uses the `beta` files namespace only to pass that header, and upload and download also work on `client.files`. Requires `@anthropic-ai/sdk` >= 0.88.0 / `anthropic` (Python) >= 0.92.0 - older versions don't type `scope_id`. In the `ant` CLI, use `ant beta:files list --scope-id --beta managed-agents-2026-04-01`. - Pass the session ID returned by `sessions.create()` verbatim (e.g. `sesn_011CZx...`) - the API validates the prefix. - There's a brief indexing lag (~1-3s) between `session.status_idle` and output files appearing in `files.list`. Retry once or twice if empty. @@ -150,7 +150,7 @@ Repositories are attached for the lifetime of the session - to change which repo const agent = await client.beta.agents.create( { name: 'GitHub Agent', - model: 'claude-opus-5', + model: 'claude-opus-5-5', mcp_servers: [ { type: 'url', name: 'github', url: 'https://api.githubcopilot.com/mcp/' }, ], @@ -184,7 +184,7 @@ import os agent = client.beta.agents.create( name="GitHub Agent", - model="claude-opus-5", + model="claude-opus-5-5", mcp_servers=[{ "type": "url", "name": "github", diff --git a/skills/claude-api/shared/managed-agents-events.md b/skills/claude-api/shared/managed-agents-events.md index 657a69fd0..df85e96b0 100644 --- a/skills/claude-api/shared/managed-agents-events.md +++ b/skills/claude-api/shared/managed-agents-events.md @@ -35,7 +35,7 @@ client.beta.sessions.events.send( Constraints: -- **Model-gated: Claude Opus 5, Claude Opus 4.8, Claude Sonnet 5, Claude Fable 5.1, and Claude Mythos 5.1.** Only the agent's **primary** model is checked - `system.message` lands on the primary thread only, so subagent models are not considered. On an unsupported primary model the event is rejected with a `model_does_not_support_mid_conversation_system` validation error. +- **Model-gated: Claude Fable 5.1, Claude Mythos 5.1, Claude Fable 5, Claude Mythos 5, Claude Opus 5.5, Claude Opus 5, Claude Opus 4.8, and Claude Sonnet 5.5 (not Claude Sonnet 5).** Only the agent's **primary** model is checked - `system.message` lands on the primary thread only, so subagent models are not considered. On an unsupported primary model the event is rejected with a `model_does_not_support_mid_conversation_system` validation error. - **While the session is idle with `stop_reason: requires_action`** (blocked on `user.custom_tool_result` / `user.tool_confirmation`), a `system.message` is accepted **only when it trails a tool result event in the same request**. Sent on its own - or alongside a `user.message` - it is rejected until the pending tool events are resolved. - `content` accepts 1-1000 text items. diff --git a/skills/claude-api/shared/managed-agents-memory.md b/skills/claude-api/shared/managed-agents-memory.md index 28e119b6f..3c89bd781 100644 --- a/skills/claude-api/shared/managed-agents-memory.md +++ b/skills/claude-api/shared/managed-agents-memory.md @@ -1,6 +1,6 @@ # Managed Agents - Memory Stores -> **Public beta.** Memory stores ship under the `managed-agents-2026-04-01` beta header; the SDK sets it automatically on all `client.beta.memory_stores.*` calls. If `client.beta.memory_stores` is missing, upgrade to the latest SDK release. +> **Public beta.** Memory stores ship under the `agent-memory-2026-07-22` beta header; the SDK sets it automatically on all `client.beta.memory_stores.*` calls. Don't add `managed-agents-2026-04-01` to these calls - sending both headers on a memory store request returns a 400. Attaching a store to a session is a session call and still uses `managed-agents-2026-04-01`. If `client.beta.memory_stores` is missing, upgrade to the latest SDK release. Sessions are ephemeral by default - when one ends, anything the agent learned is gone. A **memory store** is a workspace-scoped collection of small text documents that persists across sessions. When a store is attached to a session (via `resources[]`), it is mounted into the container as a filesystem directory; the agent reads and writes it with the ordinary file tools, and a system-prompt note tells it the mount is there. diff --git a/skills/claude-api/shared/managed-agents-multiagent.md b/skills/claude-api/shared/managed-agents-multiagent.md index d33052e10..ababa24c5 100644 --- a/skills/claude-api/shared/managed-agents-multiagent.md +++ b/skills/claude-api/shared/managed-agents-multiagent.md @@ -16,7 +16,7 @@ The SDK sets the `managed-agents-2026-04-01` beta header automatically on all `c agent = client.beta.agents.create( name="Research assistant", description="Researches a question end to end. A copy can be spawned to own one well-scoped sub-question.", - model="claude-opus-5", + model="claude-opus-5-5", system="You are a research assistant. When a request splits into independent sub-questions, delegate each to a copy of yourself, one self-contained task per copy, then verify and combine their reports.", tools=[{"type": "agent_toolset_20260401"}], multiagent={"type": "coordinator", "agents": [{"type": "self"}]}, # the only change vs. a single agent @@ -25,7 +25,7 @@ agent = client.beta.agents.create( session = client.beta.sessions.create(agent=agent.id, environment_id=env.id) # unchanged ``` -**Step 2 - move the reading-heavy work to a cheaper model.** Delegated research work is mostly searching, reading, and extracting: many input tokens, little hard reasoning. Create a second agent on a smaller current-generation model (Claude Haiku 4.5, or Claude Sonnet 5 when the worker needs more judgment) with a narrow `system` prompt and only the tools it needs, and list it next to `self`. A roster entry is only a reference: the worker runs on its own `model`, `system`, and `tools`, and its tokens are billed at its own model's rates. The large model spends its tokens on planning, checking, and synthesis; the small model does the bulk reading. +**Step 2 - move the reading-heavy work to a cheaper model.** Delegated research work is mostly searching, reading, and extracting: many input tokens, little hard reasoning. Create a second agent on a smaller current-generation model (Claude Haiku 4.5, or Claude Sonnet 5.5 when the worker needs more judgment) with a narrow `system` prompt and only the tools it needs, and list it next to `self`. A roster entry is only a reference: the worker runs on its own `model`, `system`, and `tools`, and its tokens are billed at its own model's rates. The large model spends its tokens on planning, checking, and synthesis; the small model does the bulk reading. ```python worker = client.beta.agents.create( @@ -43,7 +43,7 @@ worker = client.beta.agents.create( lead = client.beta.agents.create( name="Research lead", description="Plans and synthesizes research. A copy can be spawned to own one large sub-analysis.", - model="claude-opus-5", + model="claude-opus-5-5", system="Plan the work. Delegate each independent, reading-heavy question to Web researcher, one self-contained task per spawn, several in parallel. Keep verification and the final synthesis for yourself; spawn a copy of yourself only for a sub-analysis that needs your full capability.", tools=[{"type": "agent_toolset_20260401"}], multiagent={"type": "coordinator", "agents": [worker.id, {"type": "self"}]}, @@ -56,7 +56,7 @@ lead = client.beta.agents.create( reviewer = client.beta.agents.create( name="Concurrency reviewer", description="Read-only reviewer for race conditions, deadlocks, lost updates, and retry/idempotency bugs. Give it the changed file paths and the invariants that must hold; it reports findings with file:line evidence. Spawn several on the same change for independent reviews.", - model="claude-sonnet-5", + model="claude-sonnet-5-5", system="Review only the files you are pointed at. Look for concurrency bugs: unsynchronized shared state, lock ordering, non-atomic read-modify-write, retries without idempotency. Report each finding as file:line, the interleaving that triggers it, and a suggested fix; say plainly if you found none.", tools=[{"type": "agent_toolset_20260401", "default_config": {"enabled": False}, "configs": [{"name": n, "enabled": True} for n in ("read", "glob", "grep")]}], @@ -64,7 +64,7 @@ reviewer = client.beta.agents.create( test_writer = client.beta.agents.create( name="Test writer", description="Writes and runs tests. Give it the module path, the behavior to pin down, and the test command; it adds test files, runs them, and reports results with output.", - model="claude-sonnet-5", + model="claude-sonnet-5-5", system="Write focused tests for the behavior you are given, run them with the command you are given, and report pass/fail, the relevant output, and the paths of files you added. Do not edit non-test code; if the code under test looks wrong, report that instead.", tools=[{"type": "agent_toolset_20260401", "default_config": {"enabled": True}, "configs": [{"name": n, "enabled": False} for n in ("web_fetch", "web_search")]}], @@ -72,7 +72,7 @@ test_writer = client.beta.agents.create( lead = client.beta.agents.create( name="Engineering lead", description="Plans and makes code changes and integrates specialist reports. A copy can be spawned to own one independent change.", - model="claude-opus-5", + model="claude-opus-5-5", system="Make the change yourself. Then, in parallel, send the changed paths and invariants to three Concurrency reviewers and the module path and test command to Test writer. Merge and de-duplicate the reviewers' findings, check each against the code before acting on it, fix, and have Test writer re-run. Keep design decisions and the final summary for yourself.", tools=[{"type": "agent_toolset_20260401"}], multiagent={"type": "coordinator", "agents": [reviewer.id, test_writer.id, {"type": "self"}]}, @@ -98,7 +98,7 @@ The sections below are the reference for rosters, threads, events, and client-si ```python orchestrator = client.beta.agents.create( name="Engineering lead", - model="claude-opus-5", + model="claude-opus-5-5", system="You coordinate engineering work. Delegate code review to the reviewer and test writing to the test agent.", tools=[{"type": "agent_toolset_20260401"}], multiagent={ @@ -184,16 +184,16 @@ An `{"type": "advisor", "model": ""}` roster entry gives the session's ```python agent = client.beta.agents.create( name="Backend engineer", - model="claude-sonnet-5", + model="claude-sonnet-5-5", system="You implement backend features end to end.", multiagent={ "type": "coordinator", - "agents": [{"type": "advisor", "model": "claude-opus-5"}], + "agents": [{"type": "advisor", "model": "claude-opus-5-5"}], }, ) ``` -(Claude Opus 5 is the default advisor choice. It is a redacted advisor - the agent reads its advice server-side, but the client sees `[{"type": "redacted"}]`; see *Plaintext vs redacted delivery* below. For client-readable advice, a plaintext advisor such as `claude-opus-4-8` is valid only when the agent's own model is `claude-opus-4-8` or below - agents on Claude Opus 5, Claude Fable 5.1, or Claude Mythos 5.1 can only pair with redacted advisors, so client-readable advice is not available for them (pairing table: `shared/tool-use-concepts.md`).) +(Claude Opus 5.5 is the default advisor choice. It is a redacted advisor - the agent reads its advice server-side, but the client sees `[{"type": "redacted"}]`; see *Plaintext vs redacted delivery* below. For client-readable advice, a plaintext advisor such as `claude-opus-4-8` is valid only when the agent's own model is `claude-opus-4-8` or below - agents on Claude Opus 5.5, Claude Opus 5, Claude Sonnet 5.5, Claude Fable 5.1, or Claude Mythos 5.1 can only pair with redacted advisors, so client-readable advice is not available for them (pairing table: `shared/tool-use-concepts.md`).) **Rules:** - **At most one advisor entry per roster.** The entry occupies the reserved roster name `anthropic.advisor` - a roster that also lists a member literally named `anthropic.advisor` is a 400. In responses, the advisor entry is echoed **last** in the roster regardless of submitted position. diff --git a/skills/claude-api/shared/managed-agents-onboarding.md b/skills/claude-api/shared/managed-agents-onboarding.md index 9b5f99464..bb9cdf465 100644 --- a/skills/claude-api/shared/managed-agents-onboarding.md +++ b/skills/claude-api/shared/managed-agents-onboarding.md @@ -22,9 +22,9 @@ Their description does the interview's work. Draft the agent config from it and - **Tools** - enable the full prebuilt toolset by default (`agent_toolset_20260401`: `bash`, `read`, `write`, `edit`, `glob`, `grep`, `web_fetch`, `web_search`). **Suggest MCP servers** for any third-party service the job names (GitHub, Linear, Slack, ...) - and flag the credential each one implies as you suggest it ("Linear MCP -> you'll need a Linear API token at kickoff"), so §4's auth step is a formality, not a surprise. Collection itself waits for §4. Custom tools only if the user's own app must answer calls (name, description, input schema - their handler code is theirs; don't generate it). - **Skills** - **suggest** prebuilt `xlsx`/`docx`/`pptx`/`pdf` when the job produces those artifacts; custom by `skill_id` (max 20 total per agent, prebuilt + custom combined). -- **Outcome** - if the description implies checkable "done" criteria (or you can elicit them in the follow-up: not "a good report" but "a CSV with a numeric `price` column per SKU"), **suggest an Outcome kickoff** - the harness grades and iterates against a rubric (`shared/managed-agents-outcomes.md`). +- **Outcome - the default kickoff for any job with a deliverable.** If the job produces something checkable (an artifact, a report, a PR, a dataset), draft a starter rubric from the description - explicit, independently gradeable criteria: not "a good report" but "a CSV with a numeric `price` column per SKU" - and propose it inline with the config; the harness grades and iterates against it (`shared/managed-agents-outcomes.md`). The user not having a rubric is not a reason to skip this - drafting one is your job; mark it as a starter to tune. Fall back to a conversational kickoff only when the job is genuinely interactive (a chat surface, human-in-the-loop steering). - **On-hand resources** - repos on disk (`github_repository`: URL, optional `mount_path`/`checkout`; token comes in §4), files to seed (Files API upload -> `{type: "file", file_id, mount_path}`; read-only), if the job references them. -- **Model** - default `claude-opus-5`; `claude-fable-5-1` for the hardest long-horizon work (`shared/model-migration.md` -> Migrating to Claude Fable 5.1). +- **Model** - default `claude-opus-5-5`; `claude-fable-5-1` for the hardest long-horizon work (`shared/model-migration.md` -> Migrating to Claude Fable 5.1). > Important: **PR creation needs the GitHub MCP server too** - a `github_repository` mount is filesystem-only. Edit in the mount -> push branch via `bash` -> open the PR via the MCP `create_pull_request` tool. @@ -44,9 +44,9 @@ Usually zero or one question: **Silent viability gate - run this yourself before emitting anything; surface only the gaps.** Walk the job clause by clause: every verb maps to an enabled tool or MCP server ("open a PR" -> GitHub MCP, not just the mount); every MCP server and repo mount has its credential from the auth step; every external host is reachable under the networking choice; every file/repo/dataset the job references is mounted; "done" is checkable. If something's missing, say so and resolve it - don't emit a config you already know is under-resourced. -**Kickoff - pick one, never both:** -- `user.message` - conversational. -- `user.define_outcome` + rubric - when §2 settled on an Outcome; the harness iterates and grades until the rubric passes. +**Kickoff - pick one, never both. Outcome is the default:** +- `user.define_outcome` + rubric - the default whenever the job has a deliverable (§2 drafts the rubric); the harness iterates and grades until the rubric passes. +- `user.message` - only for genuinely conversational sessions. - **Scheduled shape?** Skip per-session kickoff entirely - create a **deployment** (`deployments.create()` with `schedule` + `initial_events`); each firing creates the session autonomously. See `shared/managed-agents-scheduled-deployments.md`. Mechanics to bake into the runtime code: session creation resolves resources (a bad mount surfaces there, before tokens) but does not itself provision the sandbox; open the event stream *before* sending the kickoff; break on `session.status_terminated`, or `session.status_idle` with any non-`requires_action` `stop_reason` - terminal, or `budget_reached`, which is not terminal (only a budget change/removal resumes it) (`shared/managed-agents-client-patterns.md` Pattern 5); usage lands on `span.model_request_end`; artifacts land in `/mnt/session/outputs/` (`files.list({scope_id: session.id, ...})`). @@ -55,24 +55,27 @@ Mechanics to bake into the runtime code: session creation resolves resources (a Go straight from the last answer to the code - no preamble, no lecture about setup-vs-runtime; the two-block structure shows it. Generate **two clearly-separated blocks**: -**Block 1 - Setup (run once, store the IDs).** Prefer **YAML files + `ant` CLI** - agents and environments are version-controlled definitions users should check in and apply from CI: +**Block 1 - Setup (files + `ant apply`; the IDs land in `claude-lock.json`).** Agents and environments are version-controlled definitions - write them as files and sync them with `ant apply` (`shared/anthropic-cli.md` -> Version-controlled Managed Agents resources): -1. `.agent.yaml` (flat: `name`, `model`, `system`, `tools`, `mcp_servers`, `skills`) and `.environment.yaml` +1. `agents/.md` - YAML frontmatter (`name`, `model`, `tools`, `mcp_servers`, `skills`) with the system prompt as the Markdown body - and `environments/.yaml`. Reusing an existing environment (§3)? Write no environment file (it would create a second one), leave it out of the commands below, and use the existing `env_...` ID wherever an environment is named (Block 2, a deployment file's `environment_id`). 2. ```sh - AGENT_ID=$(ant beta:agents create < .agent.yaml --transform id -r) - ENV_ID=$(ant beta:environments create < .environment.yaml --transform id -r) - # CI sync: ant beta:agents update --agent-id "$AGENT_ID" --version N < .agent.yaml + ant apply --dry-run -v agents/.md environments/.yaml # prints the full plan, every field; changes nothing + ant apply agents/.md environments/.yaml # asks, then creates; run it again after any edit to update ``` + Name the files you just wrote - never `.` or a directory, which is walked and also creates whatever else in the repo looks like a resource (a Claude Code plugin's `agents/*.md` and `skills/*/SKILL.md`, files the user never read). Without a terminal (a coding agent's shell) the second command prints the plan and exits; it applies only with `--yes`, which is the user's approval, not yours: show them the dry-run plan and add it only once they say go ahead. If the plan would create or change anything you did not write, or a file you did not write sits at a path you need, stop and ask; never add `--force` or `--prune` on your own. +3. Keep `claude-lock.json` beside the files (commit both if this is a repo) - it holds the IDs, and without it the next `ant apply` creates duplicates. Copy the IDs Block 2 needs (agent, environment; scheduled shape: the deployment) into the app's own config or env vars once - `resources["./agents/.md"].id` and so on, keyed by the path the plan printed - so the running app does not depend on the lockfile. + +If `ant` is missing or older than 1.30.0 (`ant --version`) - and the user is not on Claude Platform on AWS (below) - say so and offer to install or upgrade it (`shared/anthropic-cli.md` -> Install and auth). Ask before running an installer, and do not silently fall back to the SDK; use the SDK fallback below only if the user declines or it cannot be installed. SDK fallback if the user asks - and **required on Claude Platform on AWS**, where auth is SigV4 and the `ant` CLI has no SigV4 mode (use the platform client from `shared/claude-platform-on-aws.md`): label it `# ONE-TIME SETUP - run once, save the IDs` and call `environments.create()` -> `agents.create()`. > Warning: **Deployments are newer than the rest of the MA surface.** Before emitting `ant beta:deployments ...` or `client.beta.deployments` / `client.beta.deployment_runs` calls, verify the user's installed CLI/SDK exposes them (`ant beta:deployments --help`; `hasattr(client.beta, "deployments")`). If not, emit raw HTTP against `POST /v1/deployments` with the `managed-agents-2026-04-01` beta header (plus `oauth-2025-04-20` when authenticating with a Bearer token from `ant auth print-credentials`), and leave an upgrade note marking what simplifies to SDK calls. -**Scheduled shape? The deployment is setup, not runtime.** Create it in Block 1, after the agent/environment IDs exist (`deployments.create()` with `schedule` + `initial_events`). Block 2 is then **not** a session loop - there is no per-run kickoff to send. Emit instead: a manual-run trigger (`POST /v1/deployments/{id}/run`) so the user can test now rather than wait for the first firing - the manual run doubles as the smoke test - plus a fetch helper (latest `deployment_runs` entry -> `session_id` -> Console URL + `files.list(scope_id=session_id)` for the artifacts). +**Scheduled shape? The deployment is setup, not runtime.** Create it in Block 1. With `ant apply`: write `deployments/.md` and add it to the same `ant apply` command. It names the agent and environment by path (a reused environment by its `env_...` ID); the frontmatter is `schedule` plus the rest of the create body, and the Markdown body becomes a `user.message` kickoff (for an Outcome kickoff put `initial_events` in the frontmatter and leave the body empty, not both). With the SDK: `deployments.create()` with `schedule` + `initial_events` after the agent/environment IDs exist. Block 2 is then **not** a session loop - there is no per-run kickoff to send. Emit instead: a manual-run trigger (`POST /v1/deployments/{id}/run`) so the user can test now rather than wait for the first firing - the manual run doubles as the smoke test - plus a fetch helper (latest `deployment_runs` entry -> `session_id` -> Console URL + `files.list(scope_id=session_id)` for the artifacts). **Block 2 - Runtime (every invocation; conversational and Outcome shapes).** SDK code in the detected language (Python/TS/cURL - SKILL.md -> Language Detection); don't emit shell loops here: -1. Load `agent_id` + `env_id` from config/env +1. Load `agent_id` + `env_id` from config/env (where Block 1 put them) 2. `sessions.create(agent=AGENT_ID, environment_id=ENV_ID, resources=[...], vault_ids=[...])`, then print the Console URL so the user can watch live: `https://platform.claude.com/workspaces/default/sessions/{session.id}` (swap `default` for their workspace slug) 3. **Smoke-test when the job depends on MCP servers, credentials, or locked-down hosts** - those failures don't surface at `sessions.create()`, only on first use. One cheap probe turn ("Confirm you can reach and list 1-2 items; don't start the task"), verify, then send the real kickoff. Skip when there are no external dependencies. 4. Open stream -> send the §4 kickoff -> loop with the terminal gate from §4. diff --git a/skills/claude-api/shared/managed-agents-outcomes.md b/skills/claude-api/shared/managed-agents-outcomes.md index e06b4fd43..c19e05d97 100644 --- a/skills/claude-api/shared/managed-agents-outcomes.md +++ b/skills/claude-api/shared/managed-agents-outcomes.md @@ -4,6 +4,8 @@ An **outcome** elevates a session from *conversation* to *work*: you state what The SDK sets the `managed-agents-2026-04-01` beta header automatically on all `client.beta.sessions.*` calls; no additional header is required for outcomes. +**Codegen default:** when generating Managed Agents client code for a task with a deliverable (an artifact, a report, a PR, a dataset), kick the session off with `user.define_outcome` rather than `user.message` - draft a starter rubric from the task description and mark it for the user to tune. Reserve plain `user.message` kickoffs for genuinely conversational sessions. + --- ## The `user.define_outcome` event @@ -37,12 +39,12 @@ client.beta.sessions.events.send( |---|---|---| | `type` | `"user.define_outcome"` | | | `description` | string | The task. This is what the agent works toward - no separate `user.message` needed. | -| `rubric` | `{type: "text", content}` \| `{type: "file", file_id}` | **Required.** Markdown with explicit, independently gradeable criteria. Upload once via `client.beta.files.upload(...)` (beta `files-api-2025-04-14`) to reuse across sessions. | +| `rubric` | `{type: "text", content}` \| `{type: "file", file_id}` | **Required.** Markdown with explicit, independently gradeable criteria. Upload once via `client.files.upload(...)` to reuse across sessions. | | `max_iterations` | int | Optional. Default **3**, max **20**. | The event is echoed back on the stream with a server-assigned `outcome_id` and `processed_at`. -> **Writing rubrics.** Use explicit, gradeable criteria ("CSV has a numeric `price` column"), not vibes ("data looks good") - the grader scores each criterion independently, so vague criteria produce noisy loops. If you don't have a rubric, have Claude analyze a known-good artifact and turn that analysis into one. +> **Writing rubrics.** Use explicit, gradeable criteria ("CSV has a numeric `price` column"), not vibes ("data looks good") - the grader scores each criterion independently, so vague criteria produce noisy loops. If you don't have a rubric, have Claude analyze a known-good artifact and turn that analysis into one. When generating code for a user who supplied no rubric, draft one yourself from their task description - 5-10 concrete criteria covering the artifact's format, required content, and quality floor - and comment it as a starter rubric to tune; never omit the outcome because the rubric wasn't handed to you. --- @@ -92,7 +94,7 @@ for ev in session.outcome_evaluations: print(f"{ev.outcome_id}: {ev.result}") # outc_01a...: satisfied ``` -**Deliverables** - the agent writes to `/mnt/session/outputs/`. Once idle, fetch via the Files API with `scope_id=session.id`. This is the same session-outputs mechanism documented in `shared/managed-agents-environments.md` -> Session outputs (including the dual-beta-header requirement on `files.list`). +**Deliverables** - the agent writes to `/mnt/session/outputs/`. Once idle, fetch via the Files API with `scope_id=session.id`. This is the same session-outputs mechanism documented in `shared/managed-agents-environments.md` -> Session outputs (including the `managed-agents-2026-04-01` header that `files.list` needs for `scope_id`). --- diff --git a/skills/claude-api/shared/managed-agents-overview.md b/skills/claude-api/shared/managed-agents-overview.md index 7df784412..43aef6ead 100644 --- a/skills/claude-api/shared/managed-agents-overview.md +++ b/skills/claude-api/shared/managed-agents-overview.md @@ -15,9 +15,9 @@ Every session references a pre-created `/v1/agents` object. Create the agent onc If you're about to write `sessions.create()` with `model`, `system`, or `tools` on the session body - **stop**. Those fields live on `agents.create()`. The session takes a *pointer* only. -**When generating code, separate setup from runtime.** `agents.create()` belongs in a setup script (or a guarded `if agent_id is None:` block), not at the top of the hot path. If the user's code calls `agents.create()` on every invocation, they're accumulating orphaned agents and paying the create latency for nothing. The correct shape is: define the agent as a version-controlled YAML manifest, apply it once with `ant beta:agents create < agent.yaml` (or a guarded setup script - see `shared/anthropic-cli.md`), persist the returned ID (config file, env var, secrets manager), and have every run load the ID and call `sessions.create()`. +**When generating code, separate setup from runtime.** `agents.create()` belongs in a setup script (or a guarded `if agent_id is None:` block), not at the top of the hot path. If the user's code calls `agents.create()` on every invocation, they're accumulating orphaned agents and paying the create latency for nothing. The correct shape is: define the agent as a version-controlled file and sync it with `ant apply`, which records the ID in `claude-lock.json` (see `shared/anthropic-cli.md`) - or use a guarded setup script that persists the returned ID (config file, env var, secrets manager) - and have every run load the ID and call `sessions.create()`. -**To change the agent's behavior, use `POST /v1/agents/{id}` - don't create a new one.** Each update bumps the version; running sessions keep their pinned version, new sessions get the latest (or pin explicitly via `{type: "agent", id, version}`). See `shared/managed-agents-core.md` -> Agents -> Versioning. To change `tools`/`mcp_servers` on **one running session** without touching the agent object, use `sessions.update()` (`vault_ids` attaches at session create only) - see `shared/managed-agents-core.md` -> Updating the agent configuration mid-session. +**To change the agent's behavior, use `POST /v1/agents/{id}` - don't create a new one.** (For an agent managed with `ant apply`, edit its file and re-run instead - an update made outside the files makes the next `ant apply` refuse to run.) Each update bumps the version; running sessions keep their pinned version, new sessions get the latest (or pin explicitly via `{type: "agent", id, version}`). See `shared/managed-agents-core.md` -> Agents -> Versioning. To change `tools`/`mcp_servers` on **one running session** without touching the agent object, use `sessions.update()` (`vault_ids` attaches at session create only) - see `shared/managed-agents-core.md` -> Updating the agent configuration mid-session. ## Beta Headers @@ -25,11 +25,10 @@ Managed Agents is in beta. The SDK sets required beta headers automatically: | Beta Header | What it enables | | ------------------------------ | ---------------------------------------------------- | -| `managed-agents-2026-04-01` | Agents, Environments, Sessions, Events, Session Resources, Session Threads, Outcomes, Multiagent, Vaults, Credentials, Memory Stores, Deployments | -| `skills-2025-10-02` | Skills API (for managing custom skill definitions) | -| `files-api-2025-04-14` | Files API for file uploads | +| `managed-agents-2026-04-01` | Agents, Environments, Sessions, Events, Session Resources, Session Threads, Outcomes, Multiagent, Vaults, Credentials, Deployments | +| `agent-memory-2026-07-22` | Memory Stores (replaces `managed-agents-2026-04-01` on memory store endpoints) | -**Which beta header goes where:** The SDK sets `managed-agents-2026-04-01` automatically on `client.beta.{agents,environments,sessions,vaults,memory_stores,deployments,deployment_runs}.*` calls, and `files-api-2025-04-14` / `skills-2025-10-02` automatically on `client.beta.files.*` / `client.beta.skills.*` calls. You do NOT need to add the Skills or Files beta header when calling Managed Agents endpoints. On raw HTTP the Managed Agents header **grants Files API access on its own**, so uploading a file for use as a session resource does not need `files-api-2025-04-14` alongside it. (Direct Skills API calls over cURL do still need `skills-2025-10-02`; the `ant` CLI and the SDKs send it for you.) **Exception - session-scoped file listing:** `client.beta.files.list({scope_id: session.id})` is a Files endpoint that takes a Managed Agents parameter, so it needs **both** headers. Pass `betas: ["managed-agents-2026-04-01"]` explicitly on that call (the SDK adds the Files header; you add the Managed Agents one). See `shared/managed-agents-environments.md` -> Session outputs. +**Which beta header goes where:** The SDK sets `managed-agents-2026-04-01` automatically on `client.beta.{agents,environments,sessions,vaults,deployments,deployment_runs}.*` calls and `agent-memory-2026-07-22` on `client.beta.memory_stores.*` calls. Don't add `managed-agents-2026-04-01` to a memory store call: sending both headers on a memory store request returns a 400 (attaching a memory store to a session is a session call and still uses `managed-agents-2026-04-01`). The Files and Skills APIs are out of beta and need no beta header; requests that still send `files-api-2025-04-14` or `skills-2025-10-02` keep working but get the old beta response shapes. **Exception - session-scoped file listing:** filtering `files.list` by `scope_id` requires `managed-agents-2026-04-01`, which `client.beta.files` does not add, so pass `betas: ["managed-agents-2026-04-01"]` explicitly on `client.beta.files.list({scope_id: session.id})` (on raw HTTP, send `anthropic-beta: managed-agents-2026-04-01`; in the `ant` CLI, add `--beta managed-agents-2026-04-01` to `ant beta:files list --scope-id`). See `shared/managed-agents-environments.md` -> Session outputs. ## Reading Guide @@ -54,7 +53,7 @@ Managed Agents is in beta. The SDK sets required beta headers automatically: | Upload files / attach repos | `shared/managed-agents-environments.md` (Resources) | | Give agents persistent memory across sessions | `shared/managed-agents-memory.md` - memory stores, `memory_store` session resource, preconditions, versions/redact. On self-hosted sandboxes: `shared/managed-agents-self-hosted-sandboxes.md` § Memory stores (SDK worker syncs a local copy) | | Inspect a session without code (transcript, per-tool stats, cost, threads) | `shared/managed-agents-events.md` - Console session viewer note; deep link `?event={event_id}` | -| Define agents/environments as version-controlled YAML; drive the API from the shell | `shared/anthropic-cli.md` - `ant beta:agents create < agent.yaml`, `--transform`, `@file` inlining | +| Keep agents/environments/skills as version-controlled files (`ant apply`); drive the API from the shell | `shared/anthropic-cli.md` - `ant apply`, `claude-lock.json`, `--transform`, `@file` inlining | | Store credentials (MCP auth, API keys for CLIs/SDKs) | `shared/managed-agents-tools.md` (Vaults section) - `mcp_oauth` / `static_bearer` / `environment_variable` | | Call a non-MCP API / CLI that needs a secret | `shared/managed-agents-tools.md` (Vaults section) - `environment_variable` credential, substituted at egress. If that doesn't fit (e.g. self-hosted sandboxes), `shared/managed-agents-client-patterns.md` Pattern 9 keeps the secret host-side via a custom tool | | Run an agent on a recurring cron schedule | `shared/managed-agents-scheduled-deployments.md` - deployments, deployment runs, pause/auto-pause | diff --git a/skills/claude-api/shared/managed-agents-scheduled-deployments.md b/skills/claude-api/shared/managed-agents-scheduled-deployments.md index 9b42736fe..471057afc 100644 --- a/skills/claude-api/shared/managed-agents-scheduled-deployments.md +++ b/skills/claude-api/shared/managed-agents-scheduled-deployments.md @@ -9,7 +9,7 @@ Requires the `managed-agents-2026-04-01` beta header (the SDK sets it automatica A deployment bundles everything a session needs (agent, environment, optional files / GitHub / memory stores / vaults) plus a `schedule` and the `initial_events` that kick off each run: - `agent` and `environment_id` are required - same shapes as `sessions.create` (see `shared/managed-agents-core.md`). A deployment targeting a **self-hosted** environment can attach `memory_store` resources (SDK worker required - `shared/managed-agents-self-hosted-sandboxes.md` § Memory stores); `file` and `github_repository` resources need a cloud environment. The Console deployment form doesn't offer memory stores for self-hosted environments - attach them via the API/SDK. -- `initial_events` must contain at least one starting event - a `user.message` **or** a `user.define_outcome`. (A deployment's `initial_events` also accepts `system.message`, which a session's does not.) +- `initial_events` must contain at least one starting event - a `user.message` **or** a `user.define_outcome`. Same default as sessions: a scheduled run that produces a deliverable (the weekly report, the compliance scan's findings file, a dataset) starts with `user.define_outcome` plus a drafted starter rubric (`shared/managed-agents-outcomes.md`); use `user.message` only when the run is genuinely conversational or has no checkable output. (A deployment's `initial_events` also accepts `system.message`, which a session's does not.) - `schedule` takes a cron `expression` and an IANA `timezone`. Minute-level granularity is the maximum. ```bash diff --git a/skills/claude-api/shared/managed-agents-tools.md b/skills/claude-api/shared/managed-agents-tools.md index d418e04d3..58c6da360 100644 --- a/skills/claude-api/shared/managed-agents-tools.md +++ b/skills/claude-api/shared/managed-agents-tools.md @@ -110,7 +110,7 @@ Set `{"type": "auto"}` anywhere a `permission_policy` is accepted: a toolset's ` ```json { "name": "Ops Agent", - "model": "claude-opus-5", + "model": "claude-opus-5-5", "mcp_servers": [{ "type": "url", "name": "github", "url": "https://mcp.example.com/github" }], "tools": [ { @@ -432,7 +432,7 @@ Skills reach the agent two ways: **attached** through the agent's `skills` array | **Pre-built Anthropic skills** | Common document tasks (PowerPoint, Excel, Word, PDF). Reference by name (e.g. `xlsx`). | | **Custom skills** | Skills you've created in your organization via the Skills API. Reference by `skill_id` + optional `version`. | -**Max 20 skills per agent.** Agent creation uses `managed-agents-2026-04-01`; the separate Skills API (for managing custom skill definitions) uses `skills-2025-10-02`. +**Max 20 skills per agent.** Agent creation uses `managed-agents-2026-04-01`; the separate Skills API (for managing custom skill definitions) is out of beta and needs no beta header. ### Enabling skills on a session @@ -442,7 +442,7 @@ Skills are attached to the **agent** definition via `agents.create()`: const agent = await client.beta.agents.create( { name: "Financial Agent", - model: "claude-opus-5", + model: "claude-opus-5-5", system: "You are a financial analysis agent.", skills: [ { type: "anthropic", skill_id: "xlsx" }, @@ -457,7 +457,7 @@ Python: ```python agent = client.beta.agents.create( name="Financial Agent", - model="claude-opus-5", + model="claude-opus-5-5", system="You are a financial analysis agent.", skills=[ {"type": "anthropic", "skill_id": "xlsx"}, diff --git a/skills/claude-api/shared/model-migration.md b/skills/claude-api/shared/model-migration.md index 47c859fda..372f60a74 100644 --- a/skills/claude-api/shared/model-migration.md +++ b/skills/claude-api/shared/model-migration.md @@ -25,9 +25,14 @@ For the latest, authoritative version (with code samples in every supported lang | Claude Sonnet 5 Migration Checklist | The required vs optional items, tagged `[BLOCKS]` / `[TUNE]` | | Migrating to Claude Fable 5.1 | Migrating to Claude Fable 5.1 or Claude Mythos 5.1 (always-on thinking, raw chain of thought never returned, refusal handling, data retention, behavioral shifts + prompting guidance) | | Claude Fable 5.1 Migration Checklist | The required vs optional items for Claude Fable 5.1, tagged `[BLOCKS]` / `[TUNE]` | -| Migrating to Claude Fable 5.1 from Claude Fable 5 | Migrating Claude Fable 5 / Claude Opus 5 / Claude Mythos 5 -> Claude Fable 5.1 or Claude Mythos 5.1 (forced `tool_choice` 400s; "preserved thinking" - model-bound blocks and the history-editing check; per-message effort; append-only per-turn reminders; `display: "updates"` progress updates; cheaper cache reads; behavioral re-tuning) | +| Migrating to Claude Fable 5.1 from Claude Fable 5 | Migrating Claude Fable 5 / Claude Opus 5 / Claude Mythos 5 -> Claude Fable 5.1 or Claude Mythos 5.1 (forced `tool_choice` 400s; "preserved thinking" - model-bound blocks and, on Claude Fable 5.1, the history-editing check; per-message effort; append-only per-turn reminders; `display: "updates"` progress updates; cheaper cache reads; behavioral re-tuning) | | Claude Fable 5.1 from Claude Fable 5 Migration Checklist | The required vs optional items for the Claude Fable 5 -> Claude Fable 5.1 move, tagged `[BLOCKS]` / `[TUNE]` | +| Migrating to Claude Opus 5.5 | Migrating Claude Opus 5 -> Claude Opus 5.5 (thinking can't be disabled; forced `tool_choice` 400s; preserved thinking; computer use via the toolset on the Claude API and Google Cloud; progress updates as thinking blocks; default effort `medium`; broader classifiers; effort tuning + prompting guidance) | +| Claude Opus 5.5 Migration Checklist | The required vs optional items for Claude Opus 5.5, tagged `[BLOCKS]` / `[TUNE]` | +| Migrating to Claude Sonnet 5.5 | Migrating Claude Sonnet 5 -> Claude Sonnet 5.5 (`disabled` thinking 400s - `between_tools` instead; forced `tool_choice` 400s; preserved thinking; computer use via the toolset on the Claude API and Google Cloud; fewer advisor pairings; progress updates as thinking blocks; recalibrated effort; five refusal categories; prompting guidance) | +| Claude Sonnet 5.5 Migration Checklist | The required vs optional items for Claude Sonnet 5.5, tagged `[BLOCKS]` / `[TUNE]` | | Verify the Migration | After edits - runtime spot-check | +| Ground the migration with an eval | User reports a behavioral regression on the new model | **TL;DR:** Change the model ID string. If you were using `budget_tokens`, switch to `thinking: {type: "adaptive"}`. If you were using assistant prefills, they 400 on both Opus 4.6 and Sonnet 4.6 - switch to one of the prefill replacements (most often `output_config.format`; see the table in Breaking Changes by Source Model). If you're moving from Sonnet 4.5 to Sonnet 4.6, set `effort` explicitly - 4.6 defaults to `high`. Remove the `effort-2025-11-24` and `fine-grained-tool-streaming-2025-05-14` beta headers (GA on 4.6); remove `interleaved-thinking-2025-05-14` once you're on adaptive thinking (keep it only while using the transitional `budget_tokens` escape hatch). Then drop back from `client.beta.messages.create` to `client.messages.create`. Dial back any aggressive "CRITICAL: YOU MUST" tool instructions; 4.6 follows the system prompt much more closely. @@ -75,7 +80,7 @@ Not every file that contains the old model ID is a **caller** of the API. Before | 1 | **Calls the API/SDK** | `client.messages.create(model=...)`, `anthropic.Anthropic()`, request payloads | Swap the model ID **and** apply the breaking-change checklist for the target version (below). | | 2 | **Defines or serves the model** | Model registries, OpenAPI specs, routing/queue configs, model-policy enums, generated catalogs | The old entry **stays** (the model is still served). Ask whether to (a) add the new model alongside, (b) leave alone, or (c) retire the old model - never blind-replace. **If you can't ask, default to (a): add the new model alongside and flag it** - replacing would de-register a model that's still in production. | | 3 | **References the ID as an opaque string** | UI fallback constants, capability-gate substring checks, generic test fixtures, label parsers, env defaults | Usually swap the string and verify any parser/regex/substring match handles the new ID - but check the sub-cases below first. | -| 4 | **Suffixed variant ID** | `claude--` like `-fast`, `-1024k`, `-200k`, `[1m]`, dated snapshots | These are deployment/routing identifiers, not the public model ID. **Do not assume a new-model equivalent exists.** Verify in the registry first; if absent, leave the string alone and flag it. **Exception: `-fast` strings (e.g. `claude-opus-4-6-fast`) are handled by the Fast Mode section below**, which rewrites them to Opus 4.8 plus `speed="fast"` and the `fast-mode-2026-02-01` beta rather than leaving them in place. | +| 4 | **Suffixed variant ID** | `claude--` like `-fast`, `-1024k`, `-200k`, `[1m]`, dated snapshots | These are deployment/routing identifiers, not the public model ID. **Do not assume a new-model equivalent exists.** Verify in the registry first; if absent, leave the string alone and flag it. **Exception: `-fast` strings (e.g. `claude-opus-4-6-fast`) are handled by the Fast Mode section below**, which rewrites them to Claude Opus 5.5 plus `speed="fast"` and the `fast-mode-2026-02-01` beta rather than leaving them in place. | **Bucket 3 sub-cases - before swapping a string reference, check:** @@ -169,14 +174,14 @@ If you're applying several prompt-tuning edits at once, offer them as a short li 1. **Confirm the target model ID.** Use only the exact strings from `shared/models.md` - do not append date suffixes to aliases (`claude-opus-4-6`, not `claude-opus-4-6-20251101`). Guessing an ID will 404. 2. **Check which features your code uses** with this checklist: - - `thinking: {type: "enabled", budget_tokens: N}` -> migrate to adaptive thinking on Opus 4.6 / Sonnet 4.6 (still functional but deprecated) - - Assistant-turn prefills (`messages` ending with `role: "assistant"`) -> must change on Opus 4.6 / Sonnet 4.6 (returns 400) - - `output_format` parameter on `messages.create()` -> must change on all models (deprecated API-wide) - - `max_tokens > ~16000` -> must stream on any model (above ~16K risks SDK HTTP timeouts). When streaming, every current model reaches 128K except Haiku 4.5, which caps at 64K - - Beta headers `effort-2025-11-24`, `fine-grained-tool-streaming-2025-05-14`, `interleaved-thinking-2025-05-14` -> GA on 4.6, remove them and switch from `client.beta.messages.create` to `client.messages.create` - - Moving Sonnet 4.5 -> Sonnet 4.6 with no `effort` set -> 4.6 defaults to `high`, which may change your latency/cost profile - - System prompts with `CRITICAL`, `MUST`, `If in doubt, use X` language -> likely to overtrigger on 4.6 (see Prompt-Behavior Changes) - - Coming from 3.x / 4.0 / 4.1: also check sampling params (`temperature` + `top_p`), tool versions (`text_editor_20250728`), `refusal` + `model_context_window_exceeded` stop reasons, trailing-newline tool-param handling + - `thinking: {type: "enabled", budget_tokens: N}` -> migrate to adaptive thinking on Opus 4.6 / Sonnet 4.6 (still functional but deprecated) + - Assistant-turn prefills (`messages` ending with `role: "assistant"`) -> must change on Opus 4.6 / Sonnet 4.6 (returns 400) + - `output_format` parameter on `messages.create()` -> must change on all models (deprecated API-wide) + - `max_tokens > ~16000` -> must stream on any model (above ~16K risks SDK HTTP timeouts). When streaming, every current model reaches 128K except Haiku 4.5, which caps at 64K + - Beta headers `effort-2025-11-24`, `fine-grained-tool-streaming-2025-05-14`, `interleaved-thinking-2025-05-14` -> GA on 4.6, remove them and switch from `client.beta.messages.create` to `client.messages.create` + - Moving Sonnet 4.5 -> Sonnet 4.6 with no `effort` set -> 4.6 defaults to `high`, which may change your latency/cost profile + - System prompts with `CRITICAL`, `MUST`, `If in doubt, use X` language -> likely to overtrigger on 4.6 (see Prompt-Behavior Changes) + - Coming from 3.x / 4.0 / 4.1: also check sampling params (`temperature` + `top_p`), tool versions (`text_editor_20250728`), `refusal` + `model_context_window_exceeded` stop reasons, trailing-newline tool-param handling 3. **Test on a single request first.** Run one call against the new model, inspect the response, then roll out. --- @@ -188,15 +193,17 @@ If you're applying several prompt-tuning edits at once, offer them as a short li | Claude Mythos Preview (`claude-mythos-preview`) | `claude-mythos-5-1` (Project Glasswing successor) or `claude-fable-5-1` (GA) | Same tokenizer family - mostly a model-ID swap; remove `thinking` config and prefill; see Migrating to Claude Fable 5.1 | | Claude Fable 5 (`claude-fable-5`) | `claude-fable-5-1` | Same tier, same per-token price, same tokenizer; three breaking changes (forced `tool_choice` 400s, "preserved thinking") - see Migrating to Claude Fable 5.1 from Claude Fable 5 | | Claude Mythos 5 (`claude-mythos-5`) | `claude-mythos-5-1` | Same path as claude-fable-5 -> claude-fable-5-1; see § Claude Mythos 5.1 under Migrating to Claude Fable 5.1 from Claude Fable 5 | -| Opus 4.8 | `claude-opus-5` | The current Opus. Two breaking changes (thinking on by default; disabling thinking capped at `high` effort) plus prompt re-tuning - see Migrating to Claude Opus 5 | -| Opus 4.7 | `claude-opus-5` | Apply the Opus 4.8 section (prompt re-tuning, no new breaking changes), then the Claude Opus 5 section | -| Opus 4.6 | `claude-opus-5` | Apply the Opus 4.7 breaking changes, then 4.8 re-tuning, then the Claude Opus 5 section | -| Opus 4.0 / 4.1 / 4.5 / Opus 3 | `claude-opus-5` | Apply 4.6 -> 4.7 -> 4.8 -> Claude Opus 5 in order (adaptive thinking, drop sampling params, then re-tune) | -| Sonnet 4.6 | `claude-sonnet-5` | Near-Opus quality on agentic and coding work at Sonnet cost; adaptive thinking on by default; see Migrating to Claude Sonnet 5 | -| Sonnet 4.0 / 4.5 / 3.7 / 3.5 | `claude-sonnet-5` | Apply the Sonnet 4.6 changes first, then the Claude Sonnet 5 section | +| Claude Opus 5 (`claude-opus-5`) | `claude-opus-5-5` | The current Opus. Lower price ($4 / $20 vs $5 / $25), same context window and tokenizer; four breaking changes (thinking can't be disabled, forced `tool_choice` 400s, preserved thinking, computer use via the toolset on the Claude API and Google Cloud) - see Migrating to Claude Opus 5.5 | +| Opus 4.8 | `claude-opus-5-5` | Apply the Claude Opus 5 section (thinking on by default, prompt re-tuning), then the Claude Opus 5.5 section, which replaces Claude Opus 5's thinking-disabled route and `computer_20251124` | +| Opus 4.7 | `claude-opus-5-5` | Apply the Opus 4.8 section (prompt re-tuning, no new breaking changes), then the Claude Opus 5 and Claude Opus 5.5 sections | +| Opus 4.6 | `claude-opus-5-5` | Apply the Opus 4.7 breaking changes, then 4.8 re-tuning, then the Claude Opus 5 and Claude Opus 5.5 sections | +| Opus 4.0 / 4.1 / 4.5 / Opus 3 | `claude-opus-5-5` | Apply 4.6 -> 4.7 -> 4.8 -> Claude Opus 5 -> Claude Opus 5.5 in order (adaptive thinking, drop sampling params, then re-tune) | +| Claude Sonnet 5 (`claude-sonnet-5`) | `claude-sonnet-5-5` | The current Sonnet. Same prices and tokenizer; five breaking changes (`disabled` thinking 400s - use `between_tools`, forced `tool_choice` 400s, preserved thinking, computer use via the toolset on the Claude API and Google Cloud, fewer advisor pairings) - see Migrating to Claude Sonnet 5.5 | +| Sonnet 4.6 | `claude-sonnet-5-5` | Apply the Claude Sonnet 5 section (adaptive thinking on by default, new tokenizer), then the Claude Sonnet 5.5 section, which replaces its thinking-disabled route, forced `tool_choice` on Bedrock, `computer_20251124`, and effort advice | +| Sonnet 4.0 / 4.5 / 3.7 / 3.5 | `claude-sonnet-5-5` | Apply the Sonnet 4.6 changes first, then the Claude Sonnet 5 and Claude Sonnet 5.5 sections | | Haiku 3 / 3.5 | `claude-haiku-4-5` | Fastest and most cost-effective | -Default to the latest Opus for the caller's tier unless they explicitly chose otherwise. The Opus migrations layer: if you're on Opus 4.6 or older, apply each version's section in order up to your target (e.g. 4.5 -> 4.8 means the 4.6, 4.7, and 4.8 sections in sequence). A 4.7 -> 4.8 move has no new breaking changes - see Migrating to Opus 4.8 below. +Default to the latest Opus (Claude Opus 5.5) for the caller's tier unless they explicitly chose otherwise. The Sonnet target is Claude Sonnet 5.5 (`claude-sonnet-5-5`). The Opus migrations layer: if you're on an older Opus, apply each version's section in order up to your target (e.g. 4.5 -> 4.8 means the 4.6, 4.7, and 4.8 sections in sequence). A 4.7 -> 4.8 move has no new breaking changes - see Migrating to Opus 4.8 below. --- @@ -206,13 +213,13 @@ These models return 404 - update immediately: | Retired model | Retired | Drop-in replacement | | ----------------------------- | ------------- | -------------------- | -| `claude-3-7-sonnet-20250219` | Feb 19, 2026 | `claude-sonnet-5` | +| `claude-3-7-sonnet-20250219` | Feb 19, 2026 | `claude-sonnet-5-5` | | `claude-3-5-haiku-20241022` | Feb 19, 2026 | `claude-haiku-4-5` | | `claude-3-opus-20240229` | Jan 5, 2026 | `claude-opus-4-8` | -| `claude-3-5-sonnet-20241022` | Oct 28, 2025 | `claude-sonnet-5` | -| `claude-3-5-sonnet-20240620` | Oct 28, 2025 | `claude-sonnet-5` | -| `claude-3-sonnet-20240229` | Jul 21, 2025 | `claude-sonnet-5` | -| `claude-2.1`, `claude-2.0` | Jul 21, 2025 | `claude-sonnet-5` | +| `claude-3-5-sonnet-20241022` | Oct 28, 2025 | `claude-sonnet-5-5` | +| `claude-3-5-sonnet-20240620` | Oct 28, 2025 | `claude-sonnet-5-5` | +| `claude-3-sonnet-20240229` | Jul 21, 2025 | `claude-sonnet-5-5` | +| `claude-2.1`, `claude-2.0` | Jul 21, 2025 | `claude-sonnet-5-5` | ## Deprecated Models (retiring soon) @@ -220,7 +227,7 @@ These models return 404 - update immediately: | ----------------------------- | ------------- | -------------------- | | `claude-3-haiku-20240307` | Apr 19, 2026 | `claude-haiku-4-5` | | `claude-opus-4-20250514` | June 15, 2026 | `claude-opus-4-8` | -| `claude-sonnet-4-20250514` | June 15, 2026 | `claude-sonnet-5` | +| `claude-sonnet-4-20250514` | June 15, 2026 | `claude-sonnet-5-5` | --- @@ -298,7 +305,7 @@ If the user asks for a "thinking budget" on 4.6, the preferred answer is `effort **2. Effort parameter (Opus 4.5, Opus 4.6, Sonnet 4.6 only).** -Controls thinking depth and overall token spend. Goes inside `output_config`, not top-level. Default is `high`. `max` is supported on Fable 5, Opus 4.6 and later, Sonnet 5, and Sonnet 4.6 - it errors on Sonnet 4.5 and Haiku 4.5. +Controls thinking depth and overall token spend. Goes inside `output_config`, not top-level. Default is `high`. `max` is supported on Fable 5, Opus 4.6 and later, Sonnet 5.5, Sonnet 5, and Sonnet 4.6 - it errors on Sonnet 4.5 and Haiku 4.5. ```python output_config={"effort": "medium"} # often the best cost / quality balance @@ -488,18 +495,20 @@ If the model is now overtriggering a tool or skill, the fix is almost always to | Old string (migration source) | New string | | ------------------------------ | ------------------ | -| `claude-opus-4-8` | `claude-opus-5` | -| `claude-opus-4-7` | `claude-opus-5` | -| `claude-opus-4-6` | `claude-opus-5` | -| `claude-opus-4-5` | `claude-opus-5` | -| `claude-opus-4-1` | `claude-opus-5` | -| `claude-opus-4-0` | `claude-opus-5` | +| `claude-opus-5` | `claude-opus-5-5` | +| `claude-opus-4-8` | `claude-opus-5-5` | +| `claude-opus-4-7` | `claude-opus-5-5` | +| `claude-opus-4-6` | `claude-opus-5-5` | +| `claude-opus-4-5` | `claude-opus-5-5` | +| `claude-opus-4-1` | `claude-opus-5-5` | +| `claude-opus-4-0` | `claude-opus-5-5` | | `claude-mythos-preview` | `claude-mythos-5-1` (Project Glasswing) or `claude-fable-5-1` | | `claude-fable-5` | `claude-fable-5-1` | | `claude-mythos-5` | `claude-mythos-5-1` | -| `claude-sonnet-4-6` | `claude-sonnet-5`| -| `claude-sonnet-4-5` | `claude-sonnet-5`| -| `claude-sonnet-4-0` | `claude-sonnet-5`| +| `claude-sonnet-4-6` | `claude-sonnet-5-5` | +| `claude-sonnet-4-5` | `claude-sonnet-5-5` | +| `claude-sonnet-4-0` | `claude-sonnet-5-5` | +| `claude-sonnet-5` | `claude-sonnet-5-5` | Older aliases (`claude-opus-4-7`, `claude-opus-4-6`, `claude-opus-4-5`, `claude-sonnet-4-6`, `claude-sonnet-4-5`, etc.) are still active and can be pinned if you need time before upgrading - see `shared/models.md` for the full legacy list. @@ -511,16 +520,18 @@ If the code uses the `AnthropicBedrockMantle` client (Python `anthropic[bedrock] |---|---| | `claude-opus-4-8` | `anthropic.claude-opus-4-8` | | `claude-opus-5` | `anthropic.claude-opus-5` | +| `claude-opus-5-5` | `anthropic.claude-opus-5-5` | | `claude-fable-5-1` | `anthropic.claude-fable-5-1` | | `claude-fable-5` | `anthropic.claude-fable-5` | | `claude-mythos-5-1` | `anthropic.claude-mythos-5-1` (us-east-1 only, not publicly listed) | | `claude-opus-4-7` | `anthropic.claude-opus-4-7` | | `claude-sonnet-5` | `anthropic.claude-sonnet-5` | +| `claude-sonnet-5-5` | `anthropic.claude-sonnet-5-5` | | `claude-haiku-4-5` | `anthropic.claude-haiku-4-5` | When migrating a Bedrock file, apply the same rename-table row as first-party, then keep/add the `anthropic.` prefix. Do **not** generate a first-party `claude-*` ID for a Bedrock client - it will 400. -**Skip for Bedrock:** the `code_execution_*` tool-version checklist item and the **Task Budgets** section - neither is available on Bedrock (see `shared/platform-availability.md` for the per-feature table). Everything else in this guide - `effort`, adaptive/extended thinking, `output_config.format`, `thinking.display`, fine-grained tool streaming, token counting - is available on Bedrock. +**Skip for Bedrock:** the `code_execution_*` tool-version checklist item and the **Task Budgets** section - neither is available on Bedrock (see `shared/platform-availability.md` for the per-feature table). Everything else in this guide - `effort`, adaptive/extended thinking, `output_config.format`, `thinking.display`, token counting - is available on Bedrock; fine-grained tool streaming (`eager_input_streaming`) is available on Bedrock's newer serving stack only (see the per-model note in `shared/platform-availability.md`). > **Out of scope:** the legacy Amazon Bedrock integration (`InvokeModel` / `Converse` APIs with ARN-versioned IDs like `anthropic.claude-3-5-sonnet-20241022-v2:0`) uses a different request shape and model-ID format. This guide does not cover it; WebFetch the Bedrock page in `shared/live-sources.md` if the user is migrating between the two Bedrock integrations. @@ -699,22 +710,22 @@ Beyond resolution, Opus 4.7 also improves on low-level perception (pointing, mea Requests that involve prohibited or high-risk topics may lead to refusals. -### Fast Mode: Claude Opus 5 / Opus 4.8 only +### Fast Mode: Claude Opus 5.5 / Claude Opus 5 / Opus 4.8 only -Fast mode is available on Claude Opus 5 and Opus 4.8. Only surface this if the caller's code actually uses fast mode (e.g. `model="claude-opus-4-6-fast"`, or `speed="fast"` on an unsupported model); if the word "fast" does not appear in the code, say nothing about Fast Mode. +Fast mode is available on Claude Opus 5.5, Claude Opus 5, and Opus 4.8. Only surface this if the caller's code actually uses fast mode (e.g. `model="claude-opus-4-6-fast"`, or `speed="fast"` on an unsupported model); if the word "fast" does not appear in the code, say nothing about Fast Mode. -When you see `model="claude-opus-4-6-fast"` (or any retired `-fast` model string), **the migration edit is** to move the fast-mode traffic onto Claude Opus 5, the current fast-capable default (Opus 4.8 also works if the caller is staying on that tier): +When you see `model="claude-opus-4-6-fast"` (or any retired `-fast` model string), **the migration edit is** to move the fast-mode traffic onto Claude Opus 5.5, the current fast-capable default, at $8 / $40 per MTok (Claude Opus 5 and Opus 4.8 also work if the caller is staying on that tier): ```python -# Request fast mode on Claude Opus 5. +# Request fast mode on Claude Opus 5.5. client.beta.messages.create( - model="claude-opus-5", max_tokens=4096, + model="claude-opus-5-5", max_tokens=4096, speed="fast", betas=["fast-mode-2026-02-01"], messages=[...], ) ``` -That is: switch the model to Claude Opus 5 (or Opus 4.8) and request fast mode the supported way, using the beta `client.beta.messages....` endpoint, the `fast-mode-2026-02-01` beta flag, and `speed="fast"` as a top-level request parameter (per-language form in SKILL.md § Fast Mode). Opus 4.7 fast mode has also been removed, so do not land on Opus 4.7 either. Do **not** leave the code on a retired `-fast` model string - the failure mode differs by version: `claude-opus-4-6-fast` is retired and the API **silently falls back** to standard Opus 4.6 (no error - the caller loses fast-mode speed without noticing); `claude-opus-4-7-fast` and `speed="fast"` on Opus 4.7 instead return an **API error** (hard failure - requests break outright rather than degrading). Either way, migrate to Opus 4.8 fast mode now. +That is: switch the model to Claude Opus 5.5 (or Claude Opus 5 or Opus 4.8) and request fast mode the supported way, using the beta `client.beta.messages....` endpoint, the `fast-mode-2026-02-01` beta flag, and `speed="fast"` as a top-level request parameter (per-language form in SKILL.md § Fast Mode). Opus 4.7 fast mode has also been removed, so do not land on Opus 4.7 either. Do **not** leave the code on a retired `-fast` model string - the failure mode differs by version: `claude-opus-4-6-fast` is retired and the API **silently falls back** to standard Opus 4.6 (no error - the caller loses fast-mode speed without noticing); `claude-opus-4-7-fast` and `speed="fast"` on Opus 4.7 instead return an **API error** (hard failure - requests break outright rather than degrading). Either way, migrate to a supported fast-mode model (Claude Opus 5.5 by default) now. ### Behavioral shifts (prompt-tunable) @@ -1305,9 +1316,9 @@ Every item is tagged: **`[BLOCKS]`** items cause a 400 error or truncated output > **Model IDs `claude-fable-5-1` and `claude-mythos-5-1` are authoritative as written here.** When the user asks to migrate to Claude Fable 5.1, write `model="claude-fable-5-1"` exactly; a Mythos Preview migrator in Project Glasswing writes `model="claude-mythos-5-1"` (everyone else: `claude-fable-5-1`). Do **not** WebFetch to verify - this guide is the source of truth for migration target IDs. The corresponding entries exist in `shared/models.md`. -Claude Fable 5.1 is Anthropic's most capable widely released model - for the most demanding reasoning and long-horizon agentic work. **Claude Mythos 5.1** (`claude-mythos-5-1`) offers the same capabilities, pricing, and API behavior through Project Glasswing (participation is the only way to access it), and succeeds the invitation-only **Claude Mythos Preview** (`claude-mythos-preview`). Everything in this section applies to both models - only the ID differs. Mythos Preview migrators in Project Glasswing target `claude-mythos-5-1`; everyone else targets `claude-fable-5-1`. 1M token context window by default (the maximum is also the default), up to 128K output tokens per request. +Claude Fable 5.1 is Anthropic's most capable widely released model - for the most demanding reasoning and long-horizon agentic work. **Claude Mythos 5.1** (`claude-mythos-5-1`) offers the same capabilities and pricing through Project Glasswing (participation is the only way to access it), and succeeds the invitation-only **Claude Mythos Preview** (`claude-mythos-preview`). Everything in this section applies to both models except where § Claude Mythos 5.1 below says otherwise (the history-editing check, platform availability, and safeguards that depend on the access program). Mythos Preview migrators in Project Glasswing target `claude-mythos-5-1`; everyone else targets `claude-fable-5-1`. 1M token context window by default (the maximum is also the default), up to 128K output tokens per request. -**Migrate to Claude Fable 5.1 only when the user explicitly chose it.** It is not the default Opus upgrade path - pricing is above Opus-tier. For "upgrade to the latest model" requests, the target remains `claude-opus-5`. +**Migrate to Claude Fable 5.1 only when the user explicitly chose it.** It is not the default Opus upgrade path - pricing is above Opus-tier. For "upgrade to the latest model" requests, the target is `claude-opus-5-5`. ### Breaking changes (vs Opus-tier and Mythos Preview) @@ -1343,7 +1354,7 @@ On Claude Fable 5.1 and Claude Mythos 5.1, the raw chain of thought is never ret When continuing on the same model, pass each thinking block back **exactly as received - including blocks whose `thinking` text is empty**. The API rejects blocks whose content has been *modified*, not blocks you have read; displaying the summary is fine, editing or reconstructing blocks is not. -Regular thinking blocks aren't origin-locked - they replay across models fine (the server renders them into the target model's prompt). Fable-tier thinking is the exception: a Claude Fable 5.1 / Claude Mythos 5.1 block is read only by that pair (apart from Claude Mythos 5.1, no other model can read a Claude Fable 5.1 block - see Migrating to Claude Fable 5.1 from Claude Fable 5), and a thinking block from Claude Fable 5/Claude Mythos 5 replayed to a different model is **dropped from the prompt** rather than rendered (except by Claude Fable 5.1 / Claude Mythos 5.1, which read these blocks) - typically silently (early-access builds hard-rejected with `invalid_request_error`; that broke workflows and was reverted before launch, but the new behavior is still rolling out, so don't build logic that depends on either outcome). The drop happens before the prompt is priced, so a dropped block **lowers `usage.input_tokens`** - you aren't billed for it, and there's nothing to strip for cost. Don't strip *regular* thinking blocks either: removing them can trigger ordering/signature 400s. Two rules for replay bodies stand regardless: fallback-credit retries must echo the refused body **unchanged**, and `fallback` blocks from a mid-output fallback stay where they appeared. +Regular thinking blocks aren't origin-locked - they replay across models fine (the server renders them into the target model's prompt). Fable-tier thinking is the exception: a Claude Fable 5.1 / Claude Mythos 5.1 block is read only by that pair (apart from Claude Mythos 5.1, no other model can read a Claude Fable 5.1 block - see Migrating to Claude Fable 5.1 from Claude Fable 5), and a thinking block from Claude Fable 5/Claude Mythos 5 replayed to a different model is **dropped from the prompt** rather than rendered (except by Claude Fable 5.1 / Claude Mythos 5.1, which read these blocks) - typically silently (this behavior is still rolling out, so don't build logic that depends on the drop being silent). The drop happens before the prompt is priced, so a dropped block **lowers `usage.input_tokens`** - you aren't billed for it, and there's nothing to strip for cost. Don't strip *regular* thinking blocks either: removing them can trigger ordering/signature 400s. Two rules for replay bodies stand regardless: fallback-credit retries must echo the refused body **unchanged**, and `fallback` blocks from a mid-output fallback stay where they appeared. Related: a request that tries to elicit the model's internal reasoning *in the response text* can be refused with `stop_details.category: "reasoning_extraction"` - applications needing reasoning visibility should read the summarized `thinking` blocks instead of prompting for reasoning. @@ -1429,7 +1440,7 @@ For languages not listed (Java, Ruby, PHP) - or for a full runnable program in a **3. Hand-rolled retry + fallback credit (raw HTTP, or SDKs without the middleware).** Detect the refusal via `stop_reason` and re-send the conversation as-is on a model with broader availability such as `claude-opus-4-8` (no stripping required either way: Claude Fable 5's thinking blocks are silently ignored by models other than Claude Fable 5.1 / Claude Mythos 5.1, which read them, and Claude Fable 5.1's own blocks are dropped by the API for any other model - breaking change 2 in § Migrating to Claude Fable 5.1 from Claude Fable 5); keep using the fallback model for subsequent turns. **Fallback credit** (beta: Claude API, Claude Platform on AWS, Amazon Bedrock, Vertex AI, and Microsoft Foundry) makes those retries cheaper. Prompt caches are per-model, so a plain retry pays cold cache-writes on the new model. With the `fallback-credit-2026-07-01` beta header (send it on both the original request and the retry; `-2026-06-01` is still accepted, and `server-side-fallback-2026-07-01` grants the same fields), a refusal's `stop_details` carries `fallback_credit_token` (opaque; `null` when unavailable) and `fallback_has_prefill_claim`. Echo the token as the top-level `fallback_credit_token` request parameter on the retry (typed in the GA SDKs; on a pre-GA SDK pass it via `extra_body`) and the previously-cached span bills at cache-read rates - the retry costs what it would have if the conversation had been on that model all along. Rules: the retry body must match the refused request **exactly** in every prompt-shaping field (`system`, `messages`, `tools`, `tool_choice`, `thinking` - do **not** strip thinking blocks when redeeming a credit - the server handles them); the retry model must be in the refused model's `allowed_fallback_models`; the token expires in 5 minutes; Batches results carry no tokens. If `fallback_has_prefill_claim` is `true`, append one assistant message echoing the refused response's `content` - the retry model continues from where the refused model stopped (and completed server-tool work isn't re-run). When echoing, strip trailing whitespace from a final `text` block (the prefill validator rejects it; the credit match tolerates that edit), after omitting any unpaired `tool_use` blocks. On a 400, fall back to the unchanged body with the token; on a 400 naming `fallback_credit_token`, retry without it (credit forfeited). -**Migrating code built on the v1 preview.** If the code you're editing carries any of these markers, it targets the discontinued early-access surface - migrate it to the v2 shapes above, and ship the header and parameter changes together (the v1 parameter shape under the v2 header is a 400): +**Migrating code built on the v1 preview.** If the code you're editing carries any of these markers, it targets the discontinued preview surface - migrate it to the v2 shapes above, and ship the header and parameter changes together (the v1 parameter shape under the v2 header is a 400): | v1 marker (replace) | v2 | |---|---| @@ -1501,7 +1512,7 @@ None of these are API-breaking, but they're where migrated workloads feel differ - **Make self-verification explicit.** For long-running builds, instruct it to establish and run its own checking harness on a cadence ("Establish a method for checking your own work as you build; run it every [interval], verifying against the specification with sub-agents"). Separate fresh-context verifier sub-agents tend to outperform self-critique. - **De-prescribe migrated prompts and skills.** Prompts and skills written for prior models are often too prescriptive for Claude Fable 5.1 and *reduce* output quality. After migrating, A/B the workload with older step-by-step scaffolding removed - prefer stating the goal and constraints over enumerating the steps. Claude Fable 5.1 is also good at updating skills on the fly from what it learns mid-task - let it. -- **Start at the top of your difficulty range.** The teams with the best early-access outcomes gave it their hardest unsolved problems first - have it scope the problem, ask questions, then execute. +- **Start at the top of your difficulty range.** The teams with the best outcomes gave it their hardest unsolved problems first - have it scope the problem, ask questions, then execute. - **Add a `send_to_user` tool for verbatim mid-task delivery.** When an asynchronous agent must deliver something the user sees *exactly as written* mid-run (a deliverable, a progress update with specific numbers, a direct answer), give it a client-side tool whose input you render directly in the UI - tool inputs are never summarized, so content arrives intact. Return a simple acknowledgement as the tool result: ```json @@ -1542,15 +1553,15 @@ For agents that only narrate routine progress, the model's default progress narr > **Model IDs `claude-fable-5-1` and `claude-mythos-5-1` are authoritative as written here.** When the user asks to migrate to Claude Fable 5.1, write `model="claude-fable-5-1"` exactly; a Project Glasswing participant migrating from Claude Mythos 5 writes `model="claude-mythos-5-1"`. Do **not** WebFetch to verify - this guide is the source of truth for migration target IDs. The corresponding entries exist in `shared/models.md`. -Claude Fable 5.1 succeeds Claude Fable 5 in the same tier at the same per-token price, with stronger long-running agentic coding, multistep research, and document / spreadsheet / slide work. **Claude Mythos 5.1** (`claude-mythos-5-1`) is the same model for Project Glasswing participants (see § Claude Mythos 5.1 below for the two ways it differs). Same 1M token context window (default and maximum), same 128K max output, same tokenizer as Claude Fable 5 (token counts unchanged; coming from a pre-Opus-4.7 model, expect roughly 30% more tokens - follow the tokenizer guidance in § Migrating to Claude Fable 5.1 above). Available on the Claude API, Amazon Bedrock (`anthropic.claude-fable-5-1`), Claude Platform on AWS, Google Cloud, and Microsoft Foundry (Anthropic-hosted). Existing Claude Fable 5 prompts should perform well out of the box. +Claude Fable 5.1 succeeds Claude Fable 5 in the same tier at the same per-token price, with stronger long-running agentic coding, multistep research, and document / spreadsheet / slide work. **Claude Mythos 5.1** (`claude-mythos-5-1`) is the same model for Project Glasswing participants (see § Claude Mythos 5.1 below for how it differs). Same 1M token context window (default and maximum), same 128K max output, same tokenizer as Claude Fable 5 (token counts unchanged; coming from a pre-Opus-4.7 model, expect roughly 30% more tokens - follow the tokenizer guidance in § Migrating to Claude Fable 5.1 above). Available on the Claude API, Amazon Bedrock (`anthropic.claude-fable-5-1`), Claude Platform on AWS, Google Cloud, and Microsoft Foundry (Anthropic-hosted). Existing Claude Fable 5 prompts should perform well out of the box. -**Migrate to Claude Fable 5.1 only when the user explicitly chose it** - same rule as Claude Fable 5: it is not the default Opus upgrade path. For "upgrade to the latest model" requests, the target remains `claude-opus-5`; the docs' own positioning is "start with Claude Opus 5; use Claude Fable 5.1 for demanding reasoning and long-horizon agentic work, or when evals on Claude Opus 5 at higher effort still fall short". +**Migrate to Claude Fable 5.1 only when the user explicitly chose it** - same rule as Claude Fable 5: it is not the default Opus upgrade path. For "upgrade to the latest model" requests, the target is `claude-opus-5-5`: the docs position Claude Opus 5.5 as the default for most work, including complex agentic coding, and Claude Fable 5.1 as the step up for the hardest long-running agentic and research tasks, or where evals on Claude Opus 5.5 at higher effort still fall short. **What changes, in one line:** three breaking changes (forced tool choice 400s; thinking blocks are preserved only for the model that produced them or a newer one; thinking blocks are preserved only in the conversation that produced them - the docs group the last two as "preserved thinking"), five additions (per-message effort, turn-scoped system messages, progress updates between tool calls, a lower cache-read price, content provenance), and agent-loop behavior that differs in three prompt-tunable ways. Read the path that matches the source model: from Claude Fable 5, everything below applies directly; from Claude Opus 5, also read § Coming from Claude Opus 5; from Opus 4.8 or earlier, apply § Migrating to Claude Fable 5.1 above first (Opus 4.7 or earlier: the Claude Opus 5 section before that), then this one. ### Breaking change 1: forced tool use is rejected -`tool_choice: {"type": "any"}` and `tool_choice: {"type": "tool", "name": "..."}` return a 400 `invalid_request_error` on Claude Fable 5.1 and Claude Mythos 5.1 (as they already do on Mythos Preview) - on the Messages API, the Message Batches API, and the token-counting endpoint: +`tool_choice: {"type": "any"}` and `tool_choice: {"type": "tool", "name": "..."}` return a 400 `invalid_request_error` on Claude Fable 5.1 and Claude Mythos 5.1 - on the Messages API, the Message Batches API, and the token-counting endpoint: ```text tool_choice: type "tool" and "any" are not supported for this model. @@ -1588,17 +1599,19 @@ response = client.messages.create( ### Breaking change 2: thinking blocks are preserved only for the model that produced them, or a newer one -Every `thinking` block records which model produced it. Claude Fable 5.1 and Claude Mythos 5.1 read each other's blocks and those from Claude Opus 5, Claude Fable 5, Claude Mythos 5, and earlier models that don't encrypt their reasoning in the signature (Opus 4.8 and earlier Opus, Sonnet, Haiku 4.5) - so a conversation that *moves onto* `claude-fable-5-1` keeps its earlier reasoning. They don't read Mythos Preview's blocks. **The binding is one-way: apart from Claude Mythos 5.1, no other model can read a Claude Fable 5.1 block.** +Every `thinking` block records which model produced it. Claude Fable 5.1 and Claude Mythos 5.1 read each other's blocks and those from Claude Opus 5.5, Claude Opus 5, Claude Fable 5, Claude Mythos 5, and earlier models that don't encrypt their reasoning in the signature (Opus 4.8 and earlier Opus, Sonnet, Haiku 4.5) - so a conversation that *moves onto* `claude-fable-5-1` keeps its earlier reasoning. They don't read Mythos Preview's blocks. **The binding is one-way: apart from Claude Mythos 5.1, no other model can read a Claude Fable 5.1 block.** -When a request carries a block the receiving model can't read - a router switch, a client-side retry on another model, a classifier refusal fallback (server-side or SDK middleware) - the API drops it before the model sees it: the request succeeds, the dropped block doesn't count toward `input_tokens` and isn't billed, and the target model re-plans without that reasoning (expect higher cost and latency on the first turn after a switch). A dropped block changes the cached prefix from its position onward on that request. Without the `thinking-binding-controls-2026-08-01` beta header the drop is silent; with it, the response carries a top-level `input_transformations` array naming each dropped block with `reason: "model_binding_mismatch"` (shape below). Amazon Bedrock is configured to read a narrower set today (own family only) - confirm at launch. +When a request carries a block the receiving model can't read - a router switch, a client-side retry on another model, a classifier refusal fallback (server-side or SDK middleware) - the API drops it before the model sees it: the request succeeds, the dropped block doesn't count toward `input_tokens` and isn't billed, and the target model re-plans without that reasoning (expect higher cost and latency on the first turn after a switch). A dropped block changes the cached prefix from its position onward on that request. Without the `thinking-binding-controls-2026-08-01` beta header the drop is silent; with it, the response carries a top-level `input_transformations` array naming each dropped block with `reason: "model_binding_mismatch"` (shape below). Keep passing thinking blocks back unchanged when you switch models - the API drops what the target can't read, unbilled, so there are no input tokens to save by stripping; removing blocks yourself can trigger ordering/signature 400s, and a fallback-credit retry must echo the refused body unchanged. ### Breaking change 3: thinking blocks are preserved only in the conversation that produced them -The published docs file this and breaking change 2 together under *preserved thinking* ("pass blocks back unchanged and let the API decide which the model can use"); this one is the conversation check - editing earlier turns invalidates every later thinking block. The API field names for it say `prefix_mismatch_behavior` / `prefix_binding_mismatch` - the same check. +The published docs file this and breaking change 2 together under *preserved thinking* ("pass blocks back unchanged and let the API decide which the model can use"); this one is the conversation check - editing earlier turns invalidates every later thinking block. The API field names for it say `prefix_mismatch_behavior` / `prefix_binding_mismatch` - the same check. **Claude Mythos 5.1 does not run this check** (breaking change 2, the model-binding check, still applies to it, and editing history still restarts the prompt cache). -A Claude Fable 5.1 thinking block's `signature` also records the conversation prefix that produced it - the top-level `system` prompt, the set of tools in `tools`, and every message before the block (with server-side compaction, the prefix starts at the most recent compaction block) - plus a chain to the previous thinking block across turns (earlier thinking blocks aren't part of the prefix, but each block records the one before it, which is why blocks can be removed from the *front* of the history and not from the middle). When the transcript comes back, the API checks that this prefix is unchanged. Claude Code, claude.ai, Managed Agents, and the Agent SDK keep the prefix intact for you; **if your code builds the `messages` array itself, check it before migrating** (the three-step check is below). **Who is enforced:** new accounts **created on or after August 31, 2026** (Claude API organizations, Amazon Bedrock accounts, Google Cloud projects, Microsoft Foundry resources). Anthropic plans to enforce it for every account on future models, so adopt the patterns now even if your account isn't enforced today. For accounts created earlier the API *records* the mismatch but acts on it only when the request opts in: setting `thinking.block_binding.prefix_mismatch_behavior` - **any value, including `"error"`, opts the request into enforcement**, which is also how you test from an older organization - or sending the `thinking-binding-controls-2026-08-01` header alone, which opts the request into the beta's default, `drop_block`. If you ship a tool or framework that people run with their own API key, test with the field set: your users on new organizations are enforced before you are. To see whether your own organization is enforced by default, send a request that edits history without the beta header - a 400 that names the header means it is. Platform note: the opt-in controls themselves (the beta header, `prefix_mismatch_behavior`, `input_transformations`) are on the Claude API and Claude Platform on AWS at launch, arrive per model on Amazon Bedrock and Google Cloud (until then the header is rejected there), and aren't offered on Microsoft Foundry - on a platform without the controls the opt-in test path doesn't apply and recovery is strip-and-retry (`shared/platform-availability.md` has the matrix). +To find and fix these edits in an existing harness - capture its requests, diff them, measure the drops, then one diff per cause - follow `shared/preserved-thinking-migration.md` (the `preserved-thinking-migration` subcommand). This section holds the rules that guide applies. + +A Claude Fable 5.1 thinking block's `signature` also records the conversation prefix that produced it - the top-level `system` prompt, the set of tools in `tools`, and every message before the block (with server-side compaction, the prefix starts at the most recent compaction block) - plus a chain to the previous thinking block across turns (earlier thinking blocks aren't part of the prefix, but each block records the one before it, which is why blocks can be removed from the *front* of the history and not from the middle). When the transcript comes back, the API checks that this prefix is unchanged. Claude Code, claude.ai, Managed Agents, and the Agent SDK keep the prefix intact for you; **if your code builds the `messages` array itself, check it before migrating** (the three-step check is below). **Who is enforced:** new accounts **created on or after August 31, 2026**, on every platform. Enforcement scope is decided per model - Claude Opus 5.5 also enforces it for new accounts only - so make your application compatible regardless of your account's age: the same patterns keep the prompt cache warm, and you can test against the check from any account by sending `prefix_mismatch_behavior`. For accounts created earlier the API *records* the mismatch but acts on it only when the request sets `thinking.block_binding.prefix_mismatch_behavior` - **any value, including `"error"`, opts the request into enforcement**, which is also how you test from an older organization (the beta header alone does not opt in: it lets you set the field and, on a request that leaves the field unset, lists each failing block in `input_transformations` as `thinking_mismatch_allowed` while the model still receives it). If you ship a tool or framework that people run with their own API key, test with the field set: your users on new organizations are enforced before you are. To see whether your own organization is enforced by default, send a request that edits history without the beta header - a 400 that names the header means it is. Platform note: the opt-in controls (the beta header, `prefix_mismatch_behavior`, `input_transformations`) are available under the same beta name on the Claude API, Claude Platform on AWS, Amazon Bedrock, and Google Cloud Vertex AI (Bedrock: the `anthropic_beta` body field; Vertex: the `anthropic-beta` HTTP header - the SDKs' `betas` parameter does the right thing on each); Microsoft Foundry is unconfirmed. Wherever an endpoint rejects the header or the beta name, the opt-in test path doesn't apply and recovery is strip-and-retry (`shared/platform-availability.md` has the matrix). **What invalidates every later thinking block:** @@ -1630,7 +1643,7 @@ anthropic-beta: thinking-binding-controls-2026-08-01 "messages": [ ...full history with thinking blocks replayed verbatim... ]} ``` -`thinking.block_binding.prefix_mismatch_behavior` takes `"error"` or `"drop_block"`. The defaults differ by surface: without the header, an enforced account errors on a mismatch (the 400 above); sending the header **alone** switches the request to the beta's own default, `drop_block` - so set the field explicitly rather than relying on either default (the header is what lets you set the field, and it adds `input_transformations` to responses). With `"drop_block"` the API drops the first mismatched block **and every thinking block after it** (up to the next compaction block, if any - including blocks in an assistant turn whose `tool_use` is still waiting on its `tool_result`), the request proceeds, and each drop is reported in the response's top-level `input_transformations` array: +`thinking.block_binding.prefix_mismatch_behavior` takes `"error"` or `"drop_block"`. On an enforced account the default is `"error"` with or without the header (the header only lets you set the field, and adds `input_transformations` to responses). On an account that isn't enforced, an unset field lets failing blocks through to the model, and with the header each one is listed in `input_transformations` as an entry of type `thinking_mismatch_allowed`. In the Message Batches API the unset default on an enforced account drops the failing blocks instead of failing the item; a Batches item fails as `errored` only with `prefix_mismatch_behavior: "error"` set. Set the field explicitly. With `"drop_block"` the API drops the first mismatched block **and every thinking block after it** (up to the next compaction block, if any - including blocks in an assistant turn whose `tool_use` is still waiting on its `tool_result`), the request proceeds, and each drop is reported in the response's top-level `input_transformations` array: ```json "input_transformations": [ @@ -1638,13 +1651,13 @@ anthropic-beta: thinking-binding-controls-2026-08-01 ] ``` -The drop applies to *that request only*: keep sending `"drop_block"` for the rest of the session, or remove the failing blocks from the history yourself. `reason` is `"prefix_binding_mismatch"` (your history changed) or `"model_binding_mismatch"` (the conversation switched models - not a bug in your code); ignore entries whose `type` or `reason` you don't recognize, because later checks add values. With the header, every response from a thinking-capable model carries the array (empty when nothing was dropped, never `null`); without it the field is absent. When streaming it arrives on the `message` object in `message_start` (and again in the final `message_delta` after a mid-stream server-side fallback). Sending `block_binding` without the header is a 400 ending in `block_binding: Extra inputs are not permitted`. The object is accepted alongside `thinking.type: "adaptive"` and `"enabled"`, and models that don't enforce the conversation check accept it and report only model-check drops, so one request body works across models. The launch SDKs type it in the beta namespace (`client.beta.messages.create(..., thinking={"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "drop_block"}}, betas=["thinking-binding-controls-2026-08-01"])`; typed enum names such as `PrefixMismatchBehavior` are open at launch - fall back to `extra_body` / a cast if the field isn't typed yet). Some older tooling spells the field `block_binding.mismatch_behavior` - an undocumented alias; write the canonical name and never send both. +The drop applies to *that request only*: keep sending `"drop_block"` for the rest of the session, or remove the failing blocks from the history yourself. On a `"type": "thinking_dropped"` entry, `reason` is `"prefix_binding_mismatch"` (your history changed) or `"model_binding_mismatch"` (the conversation switched models - not a bug in your code). The other entry type, `thinking_mismatch_allowed` (always `reason: "prefix_binding_mismatch"`), marks a block that failed the prefix check but still reached the model, on a request the API doesn't enforce. Ignore entries whose `type` or `reason` you don't recognize, because later checks add values. With the header, every response from a thinking-capable model carries the array (empty when no block was dropped and none failed the prefix check, never `null`); without it the field is absent. When streaming it arrives on the `message` object in `message_start` (and again in the final `message_delta` after a mid-stream server-side fallback). Sending `block_binding` without the header is a 400 ending in `block_binding: Extra inputs are not permitted`. The object is accepted alongside `thinking.type: "adaptive"` and `"enabled"`, and models that don't enforce the conversation check accept it and report only model-check drops, so one request body works across models. The launch SDKs type it in the beta namespace (`client.beta.messages.create(..., thinking={"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "drop_block"}}, betas=["thinking-binding-controls-2026-08-01"])`; typed enum names such as `PrefixMismatchBehavior` are open at launch - fall back to `extra_body` / a cast if the field isn't typed yet). Some older tooling spells the field `block_binding.mismatch_behavior` - an undocumented alias; write the canonical name and never send both. **The three-step check for an existing integration:** 1. Capture the exact request bodies it sends over a few normal turns, including a compaction or a tool change if the product has them. For each pair of consecutive requests, compare the `system` prompt, the `tools` array, and the shared prefix of `messages` - they should be byte-identical up to the newly appended turns. -2. Run a normal multi-turn session against `claude-fable-5-1` with the `thinking-binding-controls-2026-08-01` header and `prefix_mismatch_behavior: "drop_block"`, and log `input_transformations` on every response. An empty array on every turn means the history is intact; a `prefix_binding_mismatch` entry means something before the block at `path` changed since the previous request; a `model_binding_mismatch` entry means the conversation switched models. This works from any organization on a platform that offers the controls (see the platform note above; strip-and-retry is the recovery elsewhere), because setting the field opts the request into enforcement. In CI, set `"error"` instead so an edit fails the run. -3. Choose a production setting and **set it explicitly** under the `thinking-binding-controls-2026-08-01` header (the defaults differ by surface - above): `"error"` if a prefix mismatch can only mean a bug in your code, or `"drop_block"` to degrade instead of fail - and monitor the 400s or the `input_transformations` entries either way. Don't leave the field unset: on an account created before 2026-08-31, an unset field with no header means the check only records server-side - no 400s and no `input_transformations` to monitor (see the defaults note above). +2. Run a normal multi-turn session against `claude-fable-5-1` with the `thinking-binding-controls-2026-08-01` header and `prefix_mismatch_behavior: "drop_block"`, and log `input_transformations` on every response. An empty array on every turn means the history is intact; a `prefix_binding_mismatch` entry means something before the block at `path` changed since the previous request; a `model_binding_mismatch` entry means the conversation switched models. This works from any organization wherever the controls beta is accepted (see the platform note above: if a partner endpoint rejects the beta name as unknown, use strip-and-retry; Foundry unconfirmed), because setting the field opts the request into enforcement. In CI, set `"error"` instead so an edit fails the run. +3. Choose a production setting and **set it explicitly** rather than relying on the default (see the defaults note above): `"error"` if a prefix mismatch can only mean a bug in your code, or `"drop_block"` to degrade instead of fail - and monitor the 400s or the `input_transformations` entries either way. On an account that isn't enforced (created before 2026-08-31), sending the header with the field unset is a way to monitor production traffic, not one of the two settings above: failing blocks still reach the model, and each one shows up only as a `thinking_mismatch_allowed` entry in `input_transformations`. **Making a harness compatible - replace each transcript edit with its append-only form:** @@ -1663,11 +1676,11 @@ Two client-side compaction shapes **break** under the check. *Keep-tail compacti The API surface, limits, per-token pricing, tokenizer, always-on adaptive thinking, refusal handling, and `stop_details` categories all match Claude Fable 5: no `thinking` config other than `{type: "adaptive"}` (`disabled` and `budget_tokens` both 400), `display` defaults to `"omitted"` and the raw chain of thought is never returned, interleaved thinking is automatic (no header), no assistant prefill, no non-default sampling parameters, 512-token minimum cacheable prompt, mid-conversation system messages and tool changes supported. The `refusal` stop reason must be handled before reading `content` - the classifiers cover the same categories as Claude Fable 5 (a broader set than Claude Opus 5's cyber-only classifiers), so expect `stop_details.category` values `"bio"` and `"reasoning_extraction"` as well as `"cyber"`. Deltas: -- **Fallbacks:** server-side `fallbacks` (`"default"`, or the array form) and the SDK middleware work as on Claude Fable 5; the permitted targets are `claude-opus-4-8` and `claude-opus-5`, and per-category routing is applied server-side and not published (some categories decline with no fallback). The fallback model can't read Claude Fable 5.1's thinking blocks, so the API drops them (breaking change 2). Fallback credit works as on Claude Fable 5: Claude Fable 5.1 and Claude Mythos 5.1 mint a `fallback_credit_token` on refusals, redeemable on either permitted target (pattern 3 of the refusal section in § Migrating to Claude Fable 5.1 above; for Claude Mythos 5.1 its fallback targets were unwired as of late August - confirm at launch, see the Claude Mythos 5.1 section below); the credit refunds the prompt-cache cost of switching models. A mid-stream refusal is billed at normal rates; for a refusal before any output, see [How refusals are billed](https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#how-refusals-are-billed). +- **Fallbacks:** server-side `fallbacks` (`"default"`, or the array form) and the SDK middleware work as on Claude Fable 5; the permitted targets are `claude-opus-4-8` and `claude-opus-5`, and per-category routing is applied server-side and not published (some categories decline with no fallback). The fallback model can't read Claude Fable 5.1's thinking blocks, so the API drops them (breaking change 2). Fallback credit works as on Claude Fable 5: Claude Fable 5.1 and Claude Mythos 5.1 both mint a `fallback_credit_token` on refusals (the token is `null` when no credit is available for a refusal, so always handle `null`), redeemable on either permitted target (pattern 3 of the refusal section in § Migrating to Claude Fable 5.1 above); the credit refunds the prompt-cache cost of switching models. A mid-stream refusal is billed at normal rates; for a refusal before any output, see [How refusals are billed](https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#how-refusals-are-billed). - **Data retention:** Claude Fable 5.1 and Claude Mythos 5.1 are Covered Models like Claude Fable 5 - 30-day retention required, **not available under zero data retention unless expressly authorized by Anthropic**. As on Claude Fable 5, a request from an organization or workspace without 30-day retention returns `400 invalid_request_error` ("In order to access this model, your organization or workspace must have data retention enabled.") - check the retention configuration before debugging the payload. (An earlier draft of the launch docs described a 404 with the model hidden from `/v1/models`; the final wording is the 400. If you do see a 404 on the ID, check retention before anything else.) A ZDR organization that needs the model should contact its Anthropic account team (the "expressly authorized" path) or enable 30-day retention for one workspace; a ZDR org that *can* already reach the model has such an authorization, not proof the requirement is gone. (Earlier drafts of the launch docs described a time-bound enterprise exemption through 2026-12-31; that sentence was removed on Aug 28 - don't cite it.) - **Priority Tier:** not supported on Claude Fable 5.1 or Claude Mythos 5.1 (Claude Fable 5 is). A Claude Fable 5 caller on Priority Tier loses it on migration. - **Rate limits:** Claude Fable 5.1 shares one "Fable 5.x" pool with Claude Fable 5 (combined traffic; the Mythos models share a separate pool on the same terms) - re-baseline headroom if you run both during the migration. -- **Pricing:** $10 / $50 per MTok, 5-minute cache writes $12.50, 1-hour cache writes $20, batch $5 / $25 - all as Claude Fable 5 - except **cache reads at $0.25 per MTok** (0.025x base input, versus 0.1x on other models - whether Claude Mythos 5.1 shares the 0.025x rate is open at launch): a quarter of the Claude Fable 5 rate and half of Claude Opus 5's. Long agentic sessions that re-read a cached prefix get most of the saving; caching break-even math in `shared/prompt-caching.md` shifts accordingly - and because a miss is now much more expensive relative to a hit, keeping the cache warm matters more: per-message effort and turn-scoped system messages exist partly for that, and for idle gaps of 5-60 minutes a `max_tokens: 0` keep-alive re-send on the default 5-minute TTL is usually cheaper than the 1-hour TTL (send it with `stream` off; not with structured outputs or Batches - see `shared/prompt-caching.md` § Choosing the TTL). Expect cost per task at or under the Claude Fable 5 figures in `shared/cost-optimization.md`. +- **Pricing:** $10 / $50 per MTok, 5-minute cache writes $12.50, 1-hour cache writes $20, batch $5 / $25 - all as Claude Fable 5 - except **cache reads at $0.25 per MTok** (0.025x base input; Claude Mythos 5.1 shares this rate, Claude Opus 5.5 reads at 0.05x, and every other model at 0.1x): a quarter of the Claude Fable 5 rate and half of Claude Opus 5's. Long agentic sessions that re-read a cached prefix get most of the saving; caching break-even math in `shared/prompt-caching.md` shifts accordingly - and because a miss is now much more expensive relative to a hit, keeping the cache warm matters more: per-message effort and turn-scoped system messages exist partly for that, and for idle gaps of 5-60 minutes a `max_tokens: 0` keep-alive re-send on the default 5-minute TTL is usually cheaper than the 1-hour TTL (send it with `stream` off; not with structured outputs or Batches - see `shared/prompt-caching.md` § Choosing the TTL). Expect cost per task at or under the Claude Fable 5 figures in `shared/cost-optimization.md`. - **Tool surface:** the same tool versions as Claude Fable 5 - code execution `code_execution_20250825` / `_20260120` / `_20260521` (programmatic tool calling needs `_20260120` or later), tool search (`tool_search_tool_regex_20251119`, `_bm25_20251119`), computer use `computer_20251124`, browser use, structured outputs, web fetch with dynamic filtering (`web_fetch_20260318`), and the advisor tool (as executor or advisor; Claude Fable 5.1 / Claude Mythos 5.1 advisors return the encrypted `advisor_redacted_result`). Task budgets: beta (`task-budgets-2026-03-13`, 20k minimum) - confirm at launch. - **Content provenance (new, no request change):** text from Claude Fable 5.1 and Claude Mythos 5.1 carries Anthropic's statistical text watermark on every platform (no extra tokens or hidden characters, nothing about your org). Supported image, audio, and video files Claude produces in the code-execution sandbox carry signed C2PA Content Credentials when downloaded through the Files API on the Claude API - the manifest adds a few kilobytes, so the downloaded file's size and checksum differ from the file inside the container; text, PDF, and office files aren't signed. Platform scope beyond the Claude API is open at launch. - **1M context on Bedrock / Google Cloud and the batch 300k-output beta:** open at launch - confirm before promising either on a partner platform. @@ -1676,7 +1689,7 @@ The API surface, limits, per-token pricing, tokenizer, always-on adaptive thinki Three additions, each behind a beta header. All optional - a migrated request works without them - but the first two are how a harness stays cache-friendly and keeps its thinking preserved, so read them before touching an agent loop. -**1. Per-message effort - beta `mid-conversation-output-config-2026-07-01`.** On Claude Fable 5.1, Claude Mythos 5.1, and Claude Opus 5 (Claude API; Bedrock / Google Cloud / Foundry not confirmed at launch, and Claude Opus 5 is excluded on Bedrock), a `role: "system"` message with empty content and `output_config: {effort: ...}` changes effort from that point on without invalidating the prompt cache - raise it for a hard step, lower it for routine ones: +**1. Per-message effort - beta `mid-conversation-output-config-2026-07-01`.** On Claude Fable 5.1, Claude Mythos 5.1, Claude Opus 5.5, Claude Opus 5, and Claude Sonnet 5.5 (with thinking on) (Claude API and Google Cloud; Claude Platform on AWS / Bedrock / Foundry not confirmed, and Claude Opus 5 is excluded on Bedrock), a `role: "system"` message with empty content and `output_config: {effort: ...}` changes effort from that point on without invalidating the prompt cache - raise it for a hard step, lower it for routine ones: ```http POST /v1/messages @@ -1692,7 +1705,7 @@ anthropic-beta: mid-conversation-output-config-2026-07-01 ]} ``` -Values are the level names (`low`, `medium`, `high`, `xhigh`, `max`). The new level takes effect from the next `user` turn and holds until a later `role: "system"` message changes it. An effort-only message carries no text, so the placement rules for mid-conversation system messages don't apply - it can sit anywhere in `messages`, including first or between an assistant turn and the next user turn. Lowering effort this way is reliable; raising works best for large jumps (e.g. `low` to `xhigh`). On Claude Fable 5.1 prefer this form over changing the top-level value between requests: a top-level change restarts the cache *and* steers the model less reliably (its earlier replies were written at the previous level and it tends to stay consistent with them) - though a top-level change does not invalidate thinking blocks. Unsupported models, Claude Fable 5 included, 400: `output_config.effort requires a model that supports per-turn effort; this model does not`. The older spellings `mid-conversation-effort-2026-08-01` and `per-turn-control-2026-07-01` still resolve to the same feature but are undocumented - don't write new code with them. Open at launch: whether the beta opens to all organizations or stays a limited (allowlisted) beta. This supersedes the "per-turn effort is not in this launch" note in the Claude Opus 5 checklist. +Values are the level names (`low`, `medium`, `high`, `xhigh`, `max`). The new level takes effect from the next `user` turn and holds until a later `role: "system"` message changes it. An effort-only message carries no text, so the placement rules for mid-conversation system messages don't apply - it can sit anywhere in `messages`, including first or between an assistant turn and the next user turn. Lowering effort this way is reliable; raising works best for large jumps (e.g. `low` to `xhigh`). On Claude Fable 5.1 prefer this form over changing the top-level value between requests: a top-level change restarts the cache *and* steers the model less reliably (its earlier replies were written at the previous level and it tends to stay consistent with them) - though a top-level change does not invalidate thinking blocks. Unsupported models, Claude Fable 5 included, 400: `output_config.effort requires a model that supports per-turn effort; this model does not`. The older spellings `mid-conversation-effort-2026-08-01` and `per-turn-control-2026-07-01` still resolve to the same feature but are undocumented - don't write new code with them. The beta is open to any organization that sends the header. This supersedes the "per-turn effort is not in this launch" note in the Claude Opus 5 checklist. **2. Turn-scoped mid-conversation system messages - beta `mid-conversation-system-clear-at-2026-08-21`.** A harness often needs to tell the model something that is only true for one turn ("check your inbox before running code", "the user can't see that tool output"). Injecting the reminder and deleting it next request is a history edit - it restarts the prompt cache and, on Claude Fable 5.1, invalidates every later thinking block. Instead give a `role: "system"` message `clear_at: "next_user_message"`: its text carries system-prompt authority for the current turn, then stops rendering once a later `user` message exists. **Keep sending it back verbatim** - it stays in `messages`, so nothing earlier changes, the cache keeps matching, later thinking blocks stay valid, and a cleared message costs no input tokens. @@ -1737,7 +1750,7 @@ From Opus 4.8 or earlier: apply § Migrating to Claude Fable 5.1 above first (Op ### Claude Mythos 5.1 -`claude-mythos-5-1` is the same model as Claude Fable 5.1 - same capabilities, limits, API behavior, and per-token pricing (cache-read rate open at launch) - offered only to approved Project Glasswing customers, and the only model besides Claude Fable 5.1 that reads Claude Fable 5.1's thinking blocks (it also reads Claude Mythos 5's; not the reverse). Confirm the organization's access with the account team before switching IDs. Two differences from a Claude Mythos 5 migrator's point of view: **Claude Mythos 5.1 runs safeguards** that depend on the access program the organization is approved under (Claude Mythos 5 ran none) - handle `stop_reason: "refusal"`, read `stop_details.category`, and set up fallback as on Claude Fable 5.1 (its fallback targets were unwired as of late August; confirm at launch) - and it is **not offered on Claude Platform on AWS** (Claude API, Amazon Bedrock as `anthropic.claude-mythos-5-1` in us-east-1 only and not publicly listed, Google Cloud, Microsoft Foundry). It shares the Mythos rate-limit pool with Claude Mythos 5. Whether Claude Mythos 5 access carries over automatically is open at launch. +`claude-mythos-5-1` is the same model as Claude Fable 5.1 - same capabilities, limits, and per-token pricing (including the $0.25/MTok cache-read rate), and the same API behavior except that it does not run the history-editing check (breaking change 3) - offered only to approved Project Glasswing customers, and the only model besides Claude Fable 5.1 that reads Claude Fable 5.1's thinking blocks (it also reads Claude Mythos 5's; not the reverse). Confirm the organization's access with the account team before switching IDs. Two differences from a Claude Mythos 5 migrator's point of view: **Claude Mythos 5.1 runs safeguards** that depend on the access program the organization is approved under (Claude Mythos 5 ran none) - handle `stop_reason: "refusal"`, read `stop_details.category`, and set up fallback as on Claude Fable 5.1 (same targets, `claude-opus-4-8` and `claude-opus-5`; it mints a `fallback_credit_token` on refusals as Claude Fable 5.1 does) - and it is **not offered on Claude Platform on AWS** (Claude API, Amazon Bedrock as `anthropic.claude-mythos-5-1` in us-east-1 only and not publicly listed, Google Cloud, Microsoft Foundry). It shares the Mythos rate-limit pool with Claude Mythos 5. Whether Claude Mythos 5 access carries over automatically is open at launch. ### Capability improvements versus Claude Fable 5 @@ -1840,10 +1853,10 @@ The second tells it to hold the scope the user set: - [ ] **[BLOCKS]** Coming from an Opus-tier or older model (not from Claude Fable 5): apply the Claude Fable 5.1 Migration Checklist above (the Opus-tier -> Fable migration) first, plus § Coming from Claude Opus 5 - `thinking: {type: "disabled"}` now 400s at any effort, between-tool narration moves into `thinking` blocks, ZDR is lost, price doubles - [ ] **[BLOCKS]** Data retention: 30-day retention required (Covered Model; ZDR only if expressly authorized by Anthropic) - a ZDR org gets `400 invalid_request_error` on every request, as on Claude Fable 5; check the retention configuration before debugging the payload - [ ] **[BLOCKS]** Keep passing `thinking` blocks back unchanged on every turn, including empty ones and `redacted_thinking` - the history-editing check rejects edited history -- [ ] **[BLOCKS]** Preserved thinking / the history-editing check (new accounts created on/after 2026-08-31 on every platform, and any request that sets `prefix_mismatch_behavior` or sends the controls beta header; later models enforce it for everyone): stop editing history between requests - freeze the top-level `system`, use `role: "system"` messages for mid-session instructions, `tool_addition`/`tool_removal` for tool changes, turn-scoped (`clear_at`) system messages - or, without that beta, retained user-message text blocks - appended after the tool results and never deleted, for per-turn reminders, server-side context editing / compaction (summary-only if client-side) for trimming, `file_id` for cross-turn files. Run the three-step check on a platform offering the controls beta (`shared/platform-availability.md`) (`prefix_mismatch_behavior: "drop_block"` + log `input_transformations`; fix every `prefix_binding_mismatch`, `model_binding_mismatch` after a model switch is expected; `"error"` in CI), then pick a production setting and monitor it. If you ship a tool others run with their own key, test with the field set. Keep-tail and background compaction need `"drop_block"` (per request - keep sending it) or stripped thinking on the retained turns; never compact mid tool round -- [ ] **[TUNE]** Fallbacks: keep server-side `fallbacks` (targets `claude-opus-4-8` / `claude-opus-5`; routing unpublished) or the SDK middleware; the fallback model can't read 5.1 thinking blocks (dropped, unbilled); fallback credit works as on Claude Fable 5 +- [ ] **[BLOCKS]** Preserved thinking / the history-editing check (new accounts created on/after 2026-08-31 on every platform, and any request that sets `prefix_mismatch_behavior`; enforcement scope is decided per model, and Claude Opus 5.5 also enforces it for new accounts only; Claude Mythos 5.1 doesn't run it): stop editing history between requests - freeze the top-level `system`, use `role: "system"` messages for mid-session instructions, `tool_addition`/`tool_removal` for tool changes, turn-scoped (`clear_at`) system messages - or, without that beta, retained user-message text blocks - appended after the tool results and never deleted, for per-turn reminders, server-side context editing / compaction (summary-only if client-side) for trimming, `file_id` for cross-turn files. Run the three-step check (`prefix_mismatch_behavior: "drop_block"` + log `input_transformations`; fix every `prefix_binding_mismatch`, `model_binding_mismatch` after a model switch is expected; `"error"` in CI; the controls beta is also available on Claude Platform on AWS, Bedrock and Vertex, Foundry unconfirmed - `shared/platform-availability.md`), then pick a production setting and monitor it. If you ship a tool others run with their own key, test with the field set. Keep-tail and background compaction need `"drop_block"` (per request - keep sending it) or stripped thinking on the retained turns; never compact mid tool round +- [ ] **[TUNE]** Fallbacks: keep server-side `fallbacks` (targets `claude-opus-4-8` / `claude-opus-5`; routing unpublished) or the SDK middleware; the fallback model can't read 5.1 thinking blocks (dropped, unbilled); fallback credit works as on Claude Fable 5 for both Claude Fable 5.1 and Claude Mythos 5.1 (handle a `null` token) - [ ] **[TUNE]** Adopt `thinking: {type: "adaptive", display: "updates"}` with `thinking-display-updates-2026-08-18` (all platforms) if users watch long tool-calling turns; render non-empty `thinking` blocks as status lines, handle the interrupted-response sentinel, echo them back unchanged -- [ ] **[TUNE]** Adopt per-message effort (`mid-conversation-output-config-2026-07-01`; also on Claude Opus 5) where a loop mixes hard and routine steps - lowering is reliable, raising wants a big jump; re-run the effort sweep (`high` default; `medium` as cost control; `xhigh`/`max` only for capability-sensitive work; `low` often beats below-frontier models on cost per task); size `max_tokens` for `high`+ +- [ ] **[TUNE]** Adopt per-message effort (`mid-conversation-output-config-2026-07-01`; also on Claude Opus 5 and Claude Opus 5.5) where a loop mixes hard and routine steps - lowering is reliable, raising wants a big jump; re-run the effort sweep (`high` default; `medium` as cost control; `xhigh`/`max` only for capability-sensitive work; `low` often beats below-frontier models on cost per task); size `max_tokens` for `high`+ - [ ] **[TUNE]** Agent loops: measure the share of multi-tool-call turns and add the "privately list what you need next" nudge (fresh copy each turn, earlier copies kept) if it's low; remove "hold findings for the final response" / anti-narration text and anti-formatting rules before adding the progress-update and formatting snippets - [ ] **[TUNE]** Add the autonomy + scope prompts for unattended runs; the hidden-tool-output note if the harness collapses tool output; the targeted-edit and scope/test-coverage prompts for coding agents; the long-deliverable note (with the real `max_tokens`) for `xhigh`/`max` requests; the compaction summarization prompt if you summarize client-side; the mannered-prose instruction for prose-heavy work; the name-verification line for search products; a crop tool (or an image-processing container) for vision - [ ] **[TUNE]** Priority Tier is not supported on Claude Fable 5.1; rate limits share the Fable 5.x pool with Claude Fable 5 - re-baseline headroom; cache reads cost a quarter of the Claude Fable 5 rate (re-check caching break-even; for 5-60 minute idle gaps a `max_tokens: 0` keep-alive on the 5-minute TTL usually beats the 1-hour TTL - sent with `stream` off; not with structured outputs or Batches); tokenizer unchanged from Claude Fable 5, so re-baseline token counts only if you weren't on Claude Fable 5 @@ -1851,16 +1864,377 @@ The second tells it to hold the scope the user set: --- -## Verify the Migration +## Migrating to Claude Opus 5.5 -After updating, spot-check that the new model is actually being used. Replace `YOUR_TARGET_MODEL` with the model string you migrated to (e.g. `claude-fable-5-1`, `claude-opus-5`, `claude-opus-4-8`, `claude-opus-4-7`, `claude-sonnet-5`, `claude-sonnet-4-6`, `claude-haiku-4-5`) and keep the assertion prefix in sync: +> **Model ID `claude-opus-5-5` is authoritative as written here.** When the user asks to migrate to Claude Opus 5.5, write `model="claude-opus-5-5"` exactly. Do **not** WebFetch to verify - this guide is the source of truth for migration target IDs. The corresponding entry exists in `shared/models.md`. + +Claude Opus 5.5 succeeds Claude Opus 5 in the Opus line for long-running agentic coding and knowledge work, **at a lower price** - $4 / $20 per MTok input / output against Claude Opus 5's $5 / $25. Same 1M token context window (default and maximum), same 128K max output, same tokenizer as Claude Opus 5 (token counts unchanged; coming from a pre-Opus-4.7 model, follow the tokenizer guidance in the Claude Opus 5 section). Knowledge cutoff June 2026. Available at launch on the Claude API (`claude-opus-5-5`), Amazon Bedrock (`anthropic.claude-opus-5-5`), Claude Platform on AWS, Google Cloud, and Microsoft Foundry (all as `claude-opus-5-5`; on Foundry under both hosting options, "Hosted on Anthropic" and "Hosted on Azure"); Claude Opus 5 stays available on all of them. Existing Claude Opus 5 prompts should perform well out of the box; the Claude Opus 5 prompting patterns below remain a reasonable starting point. + +**Claude Opus 5.5 is the default Opus migration target.** This section is layered on top of the Claude Opus 5 migration above - a caller coming from Opus 4.8 or older applies § Migrating to Claude Opus 5 first (Opus 4.7 or older: the sections before that), with two exceptions to what that section (and the earlier ones) say: Claude Opus 5's "thinking can be disabled at `high` or below" does not carry over, and neither does the acceptance of the earlier `computer_20251124` tool outside Amazon Bedrock (breaking change 4 below). Coming from Claude Sonnet 5: the request surface already matches (adaptive thinking, no sampling parameters, no prefill) - apply this section on top of the Claude Sonnet 5 code, re-baselining for Opus-tier pricing and rate limits. + +**What changes, in one line:** four breaking changes for code running on Claude Opus 5 (thinking can't be disabled; forced `tool_choice` 400s; thinking blocks are tied to the model and the conversation - "preserved thinking"; on the Claude API and Google Cloud the `computer_20251124` tool 400s - use the computer toolset), one response-shape change that fails no request (text between tool calls comes back in `thinking` blocks), a **default effort of `medium`** where Claude Opus 5's is `high`, and a broader safety-classifier set (`bio` and `reasoning_extraction` join `cyber`). The first three breaking changes are the same mechanisms Claude Fable 5.1 introduced - the sections below give the Claude Opus 5.5 specifics and point at § Migrating to Claude Fable 5.1 from Claude Fable 5 for the shared mechanics rather than repeating them. Everything else in the Claude Opus 5 request surface carries over: mid-conversation system messages and per-message effort (which some of the tips below use), mid-conversation tool changes, task budgets, compaction, the 512-token minimum cacheable prompt, batch, the Files API, PDF support, vision, and the server-side and client-side tools. + +### Breaking change 1: thinking can't be disabled + +On Claude Opus 5, thinking is on by default and `thinking: {type: "disabled"}` is accepted at effort `high` or below. On Claude Opus 5.5 thinking is **always on**: `{"type": "disabled"}` and `{"type": "enabled", "budget_tokens": N}` both return a 400 `invalid_request_error` at every effort level, with no beta header involved: + +```text +"thinking.type.disabled" is not supported for this model. Use "thinking.type.adaptive" and "output_config.effort" to control thinking behavior. +``` + +(`"thinking.type.enabled" is not supported for this model. ...` for the budget form.) Omit the `thinking` field or send `{type: "adaptive"}`, which is equivalent. **Effort is now the control for how much the model thinks, and therefore for latency and cost** (§ Choosing an effort level below). Migrate a route that disables thinking or sets a budget as follows: + +1. **Remove the `thinking` field** (or set it to `{type: "adaptive"}`). +2. **If time to first token matters, set `output_config.effort` to `low`.** At `low` the model keeps its thinking short; how often it skips thinking altogether depends on the prompts. Measure, and move to `medium` if quality drops. A system-prompt line such as *"Answer directly without deliberating."* can reduce thinking further (and with it TTFT and cost) - measure quality on your own use case before keeping it, since less thinking can cost accuracy. +3. **Size `max_tokens` for the thinking as well as the reply.** Thinking counts toward `max_tokens` even though its text isn't returned under the default `display`, so a limit sized for a no-thinking route cuts replies off. For long agentic coding turns, 64K has worked well. +4. **Read the response by block `type`, not position.** A response can begin with one or more `thinking` blocks; under the default `display: "omitted"` they come back with an empty `thinking` string. Set `display: "summarized"` for a readable summary of the reasoning. Pass `thinking` blocks back unmodified in tool-use loops. ```python -YOUR_TARGET_MODEL = "claude-opus-5" # or "claude-opus-4-7", "claude-sonnet-5", "claude-sonnet-4-6", "claude-haiku-4-5" +# Before - accepted on Claude Opus 5, 400 on Claude Opus 5.5 +client.messages.create( + model="claude-opus-5", + max_tokens=16000, + thinking={"type": "disabled"}, + messages=[{"role": "user", "content": "..."}], +) + +# After - thinking is always on; effort is the control +client.messages.create( + model="claude-opus-5-5", + max_tokens=16000, + output_config={"effort": "low"}, + messages=[{"role": "user", "content": "..."}], +) +``` + +**Prompts written for thinking disabled.** Three follow-ups if the Claude Opus 5 integration ran with thinking off: (a) start at `low` and measure, as above; (b) **remove instructions that stood in for thinking** - a prompt that asked the model to write its reasoning into the response text as a substitute for thinking should go, and the reasoning read from `display: "summarized"` blocks instead; a prompt that pushes the model to reproduce its internal reasoning in the response can be **declined** with `stop_details.category: "reasoning_extraction"`; (c) re-test the two thinking-disabled mitigations from § Two failure modes when thinking is disabled under Claude Opus 5 - both address artifacts that appeared only with thinking off, so check whether the combined "brief sentence before a tool call / say so if no tool fits / no internal XML tags" instruction is still needed, and **delete any rule telling the model not to think either way** (it can't comply, and such rules increase tag leakage). + +### Breaking change 2: forced tool use is rejected + +As on Claude Fable 5.1: `tool_choice: {"type": "any"}` and `{"type": "tool", "name": "..."}` return a 400 `invalid_request_error` (`tool_choice: type "tool" and "any" are not supported for this model.`) on the Messages API, the Message Batches API, and the token-counting endpoint, where Claude Opus 5 accepts both. `{"type": "auto"}` (the default) and `{"type": "none"}` are unchanged; `disable_parallel_tool_use: true` still works with `auto` but now means *at most* one call. Migrate by intent - the full patterns (steering from the prompt, `strict: true` for schema-valid arguments, structured outputs for extraction, the advisor tool) are under § Breaking change 1: forced tool use is rejected in § Migrating to Claude Fable 5.1 from Claude Fable 5. The two most common: + +- **Steering toward a tool:** `tool_choice: {"type": "auto"}` plus the expectation in the prompt ("Use the `get_weather` tool to answer"), with `strict: true` on the tool definition (schema sets `additionalProperties: false`) so the arguments match the schema. Because `auto` does not guarantee a call, **check that one was made and retry if it wasn't.** +- **Extracting structured data:** if the forced call existed only to get JSON back, replace it with structured outputs (`output_config.format`). + +```python +# Before - 400 on Claude Opus 5.5 +response = client.messages.create( + model="claude-opus-5", + max_tokens=1024, + tools=tools, + tool_choice={"type": "tool", "name": "get_weather"}, + messages=[{"role": "user", "content": "What's the weather in Paris?"}], +) + +# After - auto + strict tool use, steering in the prompt, and a check that the call happened +response = client.messages.create( + model="claude-opus-5-5", + max_tokens=1024, + tools=[{**tool, "strict": True} for tool in tools], + tool_choice={"type": "auto"}, + messages=[{"role": "user", "content": "What's the weather in Paris? Use the get_weather tool."}], +) +if not any(block.type == "tool_use" for block in response.content): + ... # retry, or fall back to a text answer +``` + +### Breaking change 3: thinking blocks are tied to the model and the conversation + +Both halves of "preserved thinking" from Claude Fable 5.1 apply to Claude Opus 5.5; the mechanics (what invalidates a block, the `drop_block` request shape, `input_transformations`, the three-step audit, the append-only replacements table, which client-side compaction shapes break) are under § Breaking change 2 and § Breaking change 3 in § Migrating to Claude Fable 5.1 from Claude Fable 5 and apply verbatim. What is specific to Claude Opus 5.5: + +- **Model binding - who reads whose blocks.** Claude Opus 5.5 reads thinking blocks from Claude Opus 5 and earlier Opus, Sonnet, and Haiku models - a conversation that *moves onto* `claude-opus-5-5` keeps its reasoning - but **not** from any Fable or Mythos model. In the other direction, on the Claude API only Claude Fable 5.1 and Claude Mythos 5.1 read a Claude Opus 5.5 block; **no other model does** - so a router switch, a client-side retry on another model, or a classifier-refusal fallback (server-side or SDK middleware) to Claude Opus 5 / Claude Opus 4.8 runs the turns after the switch without Claude Opus 5.5's reasoning. The API drops what the target can't read before the model sees it: the request succeeds, dropped blocks aren't billed, and with the `thinking-binding-controls-2026-08-01` header the drop is reported in `input_transformations` with `reason: "model_binding_mismatch"`. Whether Claude Fable 5.1 / Claude Mythos 5.1 also read Claude Opus 5.5's blocks on Amazon Bedrock and Google Cloud is not documented - the docs state it for the Claude API only. Keep passing blocks back unchanged when you switch models; don't strip them yourself. +- **Conversation binding - who is enforced.** Same posture as Claude Fable 5.1: on every platform the prefix check (the `system` prompt, the `tools` array, and every earlier message must be byte-identical to when the block was produced) is enforced by default for accounts **created on or after August 31, 2026, 00:00 UTC** - a replayed block after such an edit is a 400. Older accounts opt in by setting `thinking.block_binding.prefix_mismatch_behavior` (`"error"` or `"drop_block"`, beta `thinking-binding-controls-2026-08-01`). Claude Code, claude.ai, Managed Agents, and the Agent SDK keep the prefix intact; **if your code builds `messages` itself, run the three-step check before migrating** - and do it now even on an exempt account, because it also raises prompt-cache hit rates. The three edits that break the prefix and their append-only replacements: a per-turn reminder injected and later deleted, or a system prompt changed mid-session (append a mid-conversation `role: "system"` message instead; for a one-turn reminder the `clear_at: "next_user_message"` form under beta `mid-conversation-system-clear-at-2026-08-21` is a limited beta - without it, append the reminder as a text block after the `tool_result` blocks and leave earlier copies in place); tools added or removed mid-session (declare the full set at session start and send `tool_addition` / `tool_removal` blocks, beta `mid-conversation-tool-changes-2026-07-01`); and compaction that summarizes older turns while replaying newer ones with their thinking blocks (use server-side compaction or context editing - the on-demand `compaction` parameter under beta `compact-2026-09-04`, offered on the Claude API, Claude Platform on AWS, Google Cloud, and Microsoft Foundry but not yet Amazon Bedrock, is designed to keep the retained turns' blocks valid after the swap - or client-side *simple* compaction that replaces the whole history with a summary and replays no earlier thinking, or set `drop_block`). Two compaction details that follow Claude Fable 5.1: a threshold-compaction request with custom `instructions` summarizes from the visible conversation only - earlier thinking blocks are not part of the summarizer's input, so tell it what the summary must retain (on-demand compaction's summarizer reads earlier thinking with or without `instructions`); and any assistant turn you re-insert after a compaction block needs its `thinking` / `redacted_thinking` blocks removed, or `drop_block` set. + +```http +POST /v1/messages +anthropic-beta: thinking-binding-controls-2026-08-01 + +{"model": "claude-opus-5-5", "max_tokens": 64000, + "thinking": {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "drop_block"}}, + "messages": [ ...full history with thinking blocks replayed verbatim... ]} +``` + +### Breaking change 4: computer use only through the computer toolset + +> **Re-check before promising computer use on a partner platform.** Which platforms offer the toolset can change - WebFetch the Computer Use page from `shared/live-sources.md` and read its Compatibility section. + +Claude Opus 5 accepts computer use both as the `computer_toolset_20260801` toolset and, with the `computer-use-2025-11-24` beta header, as the earlier `computer_20251124` tool. **On the Claude API and Google Cloud, Claude Opus 5.5 accepts only the toolset**: a `tools` entry of type `computer_20251124` returns a 400 `invalid_request_error` that names the rejected type and then lists the accepted ones after `Did you mean one of` (it begins `'claude-opus-5-5' does not support tool types: computer_20251124.`). The toolset is GA on the Claude API and Google Cloud with no beta header. On Amazon Bedrock, Claude Opus 5.5 still accepts `computer_20251124` (with its beta header), as Claude Opus 5 does - keep that version there. For other platforms, check the computer use tool's Compatibility section (`shared/tool-use-concepts.md` § Computer Use has the toolset summary). This is more than a `tools`-entry swap, so make and test the change on Claude Opus 5 first (it accepts both forms): + +- **Request:** drop the beta header and the beta client namespace; the entry is `{"type": "computer_toolset_20260801"}` with **no `name`** and no `display_width_px` / `display_height_px`; an optional `configs` map turns individual member tools on or off (`{"zoom": {"enabled": false}}`). All 17 members, `zoom` included, are on by default. The entry can't share a request with a `computer_20251124` entry or another tool named `computer`. +- **Agent loop:** Claude's calls are `tool_use` blocks whose `name` is the member (`screenshot`, `left_click`, `type`, `zoom`, ...) - **the action is the block's `name`, not `input.action`** - carrying `"toolset_name": "computer"`, and there can be **several per turn** (a batch action), each its own block. Return one `tool_result` per `tool_use`, matched by `tool_use_id`, all in the next `user` message, **every one echoing `"toolset_name": "computer"`** (a result that omits it is rejected); only `screenshot` and `zoom` results need an image, a short `OK` is enough for the rest. Coordinates are in the pixel space of the full screenshots you return, also after a `zoom`. Screenshots must already fit the model's image limits (the toolset takes no display dimensions and the API doesn't downscale for you). + +```python +# Before - 400 on Claude Opus 5.5 +client.beta.messages.create( + model="claude-opus-5", + max_tokens=4096, + betas=["computer-use-2025-11-24"], + tools=[{"type": "computer_20251124", "name": "computer", + "display_width_px": 1024, "display_height_px": 768}], + messages=[{"role": "user", "content": "Open the display settings."}], +) + +# After - no beta header; the toolset entry takes no name or display size +client.messages.create( + model="claude-opus-5-5", + max_tokens=4096, + tools=[{"type": "computer_toolset_20260801"}], + messages=[{"role": "user", "content": "Open the display settings."}], +) +``` + +Integrations already on the toolset, and the browser use toolset (`browser_toolset_20260801`), need no change. + +### Text between tool calls comes back in thinking blocks + +On Claude Opus 5, the short notes the model writes between tool calls (what it just found, what it's doing next) come back as `text` blocks. On Claude Opus 5.5, as on Claude Fable 5.1, notes longer than a sentence or two come back as **progress-update `thinking` blocks**, at most one before each tool call, and under the default `display: "omitted"` their text is empty - no request fails, but a client that renders only `text` blocks goes quiet for the length of a long agentic turn. Fix: set `thinking.display: "updates"` (beta `thinking-display-updates-2026-08-18`) to get a short summary of each note as text while reasoning stays hidden (`"summarized"` returns both, mixed), render each non-empty `thinking` block ahead of the `tool_use` it precedes, and pass the blocks back unchanged - the consumption rules (streaming `thinking_delta`, the interrupted-work sentinel, zero-or-more per response) are under addition 3 in § New API features of § Migrating to Claude Fable 5.1 from Claude Fable 5: + +```http +POST /v1/messages +anthropic-beta: thinking-display-updates-2026-08-18 + +{"model": "claude-opus-5-5", "max_tokens": 64000, + "thinking": {"type": "adaptive", "display": "updates"}, + "tools": [...], + "messages": [{"role": "user", "content": "Review the PRs open against our billing service."}]} +``` + +Three levers on what users see: + +1. **Receive them** - `display: "updates"` as above (a `"summarized"` display returns them too, mixed with the reasoning summaries). +2. **If the model may need to hand the user something *verbatim*** partway through a long turn - a code snippet, an exact value - give it a simple tool for sending the user a message and tell it to reserve the tool for that content. Declare the tool in `tools` from the **first** request of the session: adding it later edits the conversation's prefix and invalidates earlier thinking blocks (breaking change 3). +3. **For more frequent or predictable updates** - a one-line statement of intent before the first tool call and a short recap at the end - say so in the system prompt: when you want user-facing text and what it should contain. The model follows such instructions reasonably well; this helps most in pair programming and other human-in-the-loop work. + +### Choosing an effort level - the default is `medium`, and the levels don't map 1:1 from Claude Opus 5 + +Effort is the main control for how much Claude Opus 5.5 thinks, and with adaptive-only thinking it is the first setting to adjust when trading off intelligence, latency, and cost. Two things change from Claude Opus 5: + +- **The API default is `medium`** (Claude Opus 5 and earlier Opus models default to `high`), so a request that omits `effort` now runs one level lower than it did. **Set `effort` explicitly** and re-run the sweep rather than carrying the Claude Opus 5 setting over. Effort names don't mean the same amount of thinking across models: in Anthropic's testing, Claude Opus 5.5 at `medium` exceeds Claude Opus 5 at `high` on coding and knowledge-work evaluations, and on several coding evaluations `low` comes close to it at much lower cost. Start at `medium` and test the neighboring levels; reserve `xhigh` and `max` for work where you have measured a quality gain (all five levels are supported; `max` is uncapped). +- **At a given level, Claude Opus 5.5 tends to think more per turn than Claude Opus 5**, especially at `xhigh` and `max`. If you keep the `effort` value you set for Claude Opus 5, expect longer turns and more output tokens. To get less thinking, **lower the effort level before adding "think less" instructions** - lowering effort reduces thinking, cost, and latency more reliably than prompting does. Set `max_tokens` with room for the thinking as well as the reply (thinking counts toward it even though the text isn't returned - a limit sized for Claude Opus 5 with thinking off can cut replies off; 64K is a reasonable starting point for long agentic coding turns). + +Change effort for individual turns without invalidating the prompt cache with a **per-message effort change** (beta `mid-conversation-output-config-2026-07-01`; the request shape is addition 1 under § New API features of § Migrating to Claude Fable 5.1 from Claude Fable 5, and Claude Opus 5 already supports it). Changing the **top-level** `effort` between requests does invalidate the prompt cache. + +### Safeguards - a broader classifier set than Claude Opus 5 + +Claude Opus 5.5 runs cybersecurity **and biology** safety classifiers similar to Claude Fable 5.1's; coming from Claude Opus 5, the biology classifier is new. Everyday health and educational questions are unaffected, but requests the classifier treats as dual-use biology research (virology, toxicology, molecular design) are declined; on the cybersecurity side, finding vulnerabilities in source code is allowed. Separately - also new relative to Claude Opus 5 - a request that tries to get the model to reproduce its internal reasoning in the response text can be declined with `stop_details.category: "reasoning_extraction"`; if a prompt does this (for example, to get visible reasoning with thinking off), remove the instruction, set `display: "summarized"`, and read the `thinking` blocks. **`reasoning_extraction` declines are not retried on a fallback model.** + +A classifier decline arrives as a normal HTTP 200 with `stop_reason: "refusal"` and a `stop_details` object naming the category (`"cyber"`, `"bio"`, `"reasoning_extraction"`, ...; branch on `stop_reason`, treat `stop_details` as informational - the full handling is § `refusal` stop reason under § Migrating to Claude Fable 5.1). A refusal before any output still counts against your rate limits; for whether it is billed, see [How refusals are billed](https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#how-refusals-are-billed). Retry on another model with server-side fallbacks - `fallbacks: "default"` under beta `server-side-fallback-2026-07-01` retries on the model Anthropic recommends for that category, the array form under `server-side-fallback-2026-06-01` names your own targets (§ New API features under § Migrating to Claude Opus 5 has both shapes; read Claude Opus 5.5's permitted targets from `allowed_fallback_models` on its `/v1/models` entry, as § `refusal` stop reason describes - expect Claude Opus 5 / claude-opus-4-8), the SDK middleware on platforms without server-side fallback, or your own retry. A fallback model runs without Claude Opus 5.5's thinking blocks (breaking change 3). **Ship the opt-in from day one**, as the Claude Fable 5.1 section says. + +The classifiers can still flag benign requests - the fallback opt-in is what keeps a false positive from becoming an outage. (The docs give no prompt change that avoids Claude Opus 5.5's false positives; the **Safeguard false positives** tips under § Migrating to Claude Fable 5.1 from Claude Fable 5 are documented for Claude Fable 5.1 only - do not offer them for Claude Opus 5.5.) + +### What carries over unchanged from Claude Opus 5 - and what to re-check + +- **Feature set:** per-message effort (beta), mid-conversation system messages (no header) and tool changes (beta), task budgets, compaction (including the on-demand `compaction` parameter, beta `compact-2026-09-04`, which has its own docs), prompt caching with the 512-token minimum, batch processing (up to 300K output tokens with the `output-300k-2026-03-24` beta), the Files API, PDF support, vision, structured outputs, strict tool use, and the same server-side and client-side tools - except computer use, which needs the toolset (breaking change 4). Programmatic tool calling lists the model. +- **Pricing:** $4 / $20 per MTok; 5-minute cache writes $5 and 1-hour cache writes $8; **cache reads $0.20 per MTok (0.05x base input)**; batch $2 / $10. The cache-read discount is deeper than Claude Opus 5's, so long agentic sessions that re-read a cached prefix save more, and a miss costs relatively more - keeping the cache warm (per-message effort, append-only histories, the keep-alive patterns in `shared/prompt-caching.md`) matters more. +- **Rate limits:** a separate pool from Claude Opus 5's, with its own per-tier numbers. Re-check your tier's Claude Opus 5.5 limits before moving volume. +- **Priority Tier:** not supported (as Claude Opus 5). +- **Fast mode:** research preview on the Claude API only (not Bedrock, Claude Platform on AWS, Google Cloud, or Foundry), `speed: "fast"` under beta `fast-mode-2026-02-01`, at **$8 / $40 per MTok** (2x the standard price, the same multiple as Claude Opus 5's $10 / $50). +- **Data retention / ZDR:** nothing new is documented - treat Claude Opus 5.5 as Claude Opus 5 here. +- **SDK constants:** `Model.ClaudeOpus5_5` (C#), `anthropic.ModelClaudeOpus5_5` (Go), `Model.CLAUDE_OPUS_5_5` (Java, PHP), `Anthropic::Model::CLAUDE_OPUS_5_5` (Ruby) - published with each SDK's launch release; the bare string `"claude-opus-5-5"` works everywhere before then. + +### Capability improvements versus Claude Opus 5 + +**Cheaper per solved task, not just per token.** On many coding, analysis, and vision tasks, Claude Opus 5.5 at its default effort matched or beat Claude Opus 5 while using fewer tokens, and its price per token is 20% lower than Claude Opus 5's (60% lower for cache reads) - so expect the cost per solved task to be significantly lower for most tasks, and re-baseline `shared/cost-optimization.md`'s Claude Opus 5 figures rather than scaling them by the list price alone. + +- **Agentic coding and code review:** the largest measured gains are on multistep work in a real codebase (carrying a change through a large repository until its tests pass) - in Anthropic's testing, at its default `medium` effort it matched or beat Claude Opus 5's `high`-effort results on such tasks, in fewer steps and with about half the tokens - and on code review, where it catches more bugs with fewer false alarms. It explains its changes in plain language, which makes its work easier to review and trust. It tends to finish the same task with fewer tokens. +- **Knowledge work:** a more reliable analyst - much less likely than Claude Opus 5 to state a figure or cite a source the inputs don't support (citations pointed at the right source much more often in one customer's measurement); at `medium` it produced better long analytical deliverables than Claude Opus 5 at `high` with roughly 40% fewer output tokens; better at building and auditing financial models; more detail-oriented on large inputs (a date in a long thread that falls on the wrong weekday, a chart in a deck that doesn't match the figures) **without more false-positive flags**. +- **Communication and writing:** clearer prose, most noticeably in how it reports on agentic work - its updates while it works and its summary when it finishes say plainly what it did, what it found, and what it needs from you, with less jargon and fewer stock phrases. +- **Charts, diagrams, screenshots, and computer use:** reads visual material more accurately at every effort level without extra tooling - in Anthropic's testing, even at `low` it read charts more accurately than Claude Opus 5 at its highest effort, at roughly a tenth of the output tokens per chart (Claude Opus 5 read charts well only by running code to crop, zoom, and measure) - values on dense charts, and meaning that depends on position rather than text (which boxes an arrow connects, what changed between two diagram versions, exactly when a meeting starts and ends in a calendar screenshot). Also more reliable at multistep computer use from screenshots: at its default effort it matched the success rate Claude Opus 5 reached only at a much higher effort setting, in fewer steps and with roughly 40% fewer tokens. + +### Behavioral shifts (prompt-tunable) + +- **Re-evaluate Claude Opus 5-specific instructions.** Instructions tuned for Claude Opus 5's behavior (the verbosity, over-verification, and scope prompts under § Behavioral shifts of § Migrating to Claude Opus 5) may no longer be needed - keep them as the starting point, then re-test each on your own evals rather than carrying them over untouched. +- **Progress updates:** covered above - render the `thinking` blocks, and ask in the system prompt for the cadence you want. +- **Frontend design defaults:** asked for frontend work without design direction, it falls back on a few default styles, and a general instruction such as "avoid a generic AI look" mostly swaps one default for another. **It responds well to instructions that name specific patterns to avoid.** Work iteratively - look at which styles the first result used instead, and extend the list: + + > *"Output a vanilla HTML/CSS personal website with placeholder data. Do not use a cream or off-white background, italic accent words in headlines, numbered "01/02/03" section labels, monospace labels, or pill-shaped buttons."* + +- **Ingesting complex visual inputs:** it reads charts, diagrams, and screenshots considerably more precisely out of the box, so **harness scaffolding built for visual inputs on earlier models may no longer be needed - re-test it.** For the densest inputs, two things still add accuracy: higher-resolution images (most of all for technical drawings), and image-processing tools - run it as an agent with a container holding the raw images and libraries such as PIL and OpenCV so it can crop, zoom, measure, and verify; if a container is too much overhead, a cropping tool alone still helps. It uses these tools more effectively at higher effort levels; without tools, raising effort improves its reading of technical drawings but does little for charts. +- **Long turns:** at `xhigh` / `max`, turns run longer than on Claude Opus 5 - plan timeouts, streaming, and progress UX accordingly, and lower effort before prompting for brevity. + +### Claude Opus 5.5 Migration Checklist + +- [ ] **[BLOCKS]** Model ID -> `claude-opus-5-5` (Bedrock: `anthropic.claude-opus-5-5`). Coming from Opus 4.8 or older, the Claude Opus 5 checklist first - except that disabling thinking is not an option. +- [ ] **[BLOCKS]** Remove `thinking: {type: "disabled"}` and `{type: "enabled", budget_tokens}` on every route - both 400 at every effort level. Choose an effort level instead; size `max_tokens` for thinking plus the reply; read content blocks by `type`; pass `thinking` blocks back unmodified. +- [ ] **[BLOCKS]** Replace `tool_choice` `any` / `tool` with `auto` plus `strict: true` (steering in the prompt, and a check that the call happened) or structured outputs - on `count_tokens` and Batches too. +- [ ] **[BLOCKS]** Computer use on the Claude API and Google Cloud: declare `{"type": "computer_toolset_20260801"}` (no beta header, no `name` / display size) instead of `computer_20251124` (Amazon Bedrock still accepts `computer_20251124`), and update the agent loop for member `tool_use` blocks (action = block `name`), batch actions, and `toolset_name` on every result; confirm the toolset is offered on your platform. Test on Claude Opus 5 first. +- [ ] **[BLOCKS]** If the harness builds `messages` itself: run the preserved-thinking three-step check (§ Migrating to Claude Fable 5.1 from Claude Fable 5) - accounts created on or after 2026-08-31 are enforced by default on every platform; set `prefix_mismatch_behavior` explicitly under `thinking-binding-controls-2026-08-01` and replace every history edit with its append-only form. Declare from the first request any tool the session may need later. +- [ ] **[BLOCKS]** Handle `stop_reason: "refusal"` before reading `content` (new `bio` and `reasoning_extraction` categories) and ship a fallback opt-in; `reasoning_extraction` is not retried on a fallback. +- [ ] **[TUNE]** Set `effort` explicitly - the default is `medium`, one level below Claude Opus 5's `high` - and re-run the sweep including `low` / `medium`; lower effort before adding "think less" prompts; reserve `xhigh` / `max` for measured gains; use per-message effort (beta) to vary it without a cache reset. +- [ ] **[TUNE]** If the UI showed text between tool calls: `display: "updates"` (beta) or `"summarized"`, render non-empty `thinking` blocks; give the model a send-message tool (declared at session start) for verbatim mid-turn content; ask in the system prompt for the update cadence you want. +- [ ] **[TUNE]** If a router or fallback can move the conversation to another model, expect it to run without Claude Opus 5.5's thinking blocks (only Claude Fable 5.1 / Claude Mythos 5.1 on the Claude API keep them). +- [ ] **[TUNE]** If thinking was disabled on Claude Opus 5: start at `low`, remove reasoning-in-the-response instructions, re-test the thinking-disabled mitigation instruction, delete any "don't think" rule. +- [ ] **[TUNE]** Re-test visual-input scaffolding (may be unnecessary now); for frontend work, name the specific default patterns to avoid rather than asking for "no generic look"; re-evaluate Claude Opus 5-specific verbosity / verification / scope instructions. +- [ ] **[TUNE]** Re-baseline cost and latency at the chosen effort level: $4 / $20, cache reads $0.20 per MTok; separate rate-limit pool; no Priority Tier; fast mode is Claude API only at $8 / $40. + +--- + +## Migrating to Claude Sonnet 5.5 + +> **Model ID `claude-sonnet-5-5` is authoritative as written here.** When the user asks to migrate to Claude Sonnet 5.5, write `model="claude-sonnet-5-5"` exactly (no date suffix; `anthropic.claude-sonnet-5-5` on Amazon Bedrock). Do **not** WebFetch to verify - this guide is the source of truth for migration target IDs. The corresponding entry exists in `shared/models.md`. + +Claude Sonnet 5.5 succeeds Claude Sonnet 5 in the Sonnet line **at the same prices** - $2 / $10 per MTok input / output, 5-minute cache writes $2.50, 1-hour cache writes $4, cache reads $0.20, and Claude Sonnet 5's batch rates. Same tokenizer as Claude Sonnet 5 (token counts unchanged), 1M token context window, 128K max output (up to 300K on the Message Batches API with the `output-300k-2026-03-24` beta header). Available at launch on the Claude API (`claude-sonnet-5-5`), Amazon Bedrock (`anthropic.claude-sonnet-5-5`), Claude Platform on AWS, Google Cloud, and Microsoft Foundry (all three as `claude-sonnet-5-5`). On Foundry it is hosted on Azure only, with Global Standard deployments only, so the features Foundry doesn't offer when hosted on Azure are unavailable for it there - code execution, programmatic tool calling, Agent Skills, the Files API, and the newer web search and web fetch versions (`shared/platform-availability.md`). The SDK constants listed below may be missing from the SDK version a project pins; the bare string `"claude-sonnet-5-5"` works in every SDK (with the `anthropic.` prefix on Amazon Bedrock). Existing Claude Sonnet 5 prompts should perform well without changes; for the hardest long-horizon work, an Opus model is the better choice. + +**Claude Sonnet 5.5 is the default Sonnet migration target.** It is layered on top of the Claude Sonnet 5 migration above - a caller coming from Sonnet 4.6 or earlier applies § Migrating to Claude Sonnet 5 first (and, from Sonnet 4.5 or earlier, the older sections it points to), **except these parts of it, which this section replaces:** its `thinking: {type: "disabled"}` route (breaking change 1 below); its Bedrock-only forced `tool_choice` with thinking disabled, both halves of which 400 here (breaking changes 1 and 2); its `computer_20251124` tool version, which 400s on the Claude API and Google Cloud (breaking change 4); and all of its effort advice - including the `high` default with `xhigh` for the hardest work, the level mapping against Sonnet 4.6, the effort suggestions in its behavioral notes, and the prompts to make the model think more or less - because the levels are recalibrated here (§ Choosing an effort level). A caller coming from Claude Haiku 4.5 applies the same sections, including the Sonnet 4.5-or-earlier changes (prefill, explicit effort, beta headers, `output_format`, parsing tool input with a JSON parser) but not the Sonnet 4-or-earlier ones, then replaces `claude-haiku-4-5-20251001` or its alias, re-baselines cost at the higher price per token, and reviews prompts that were too short to cache on Claude Haiku 4.5. + +**What changes:** five breaking changes for code running on Claude Sonnet 5 (`disabled` thinking 400s - turn thinking off with `between_tools` instead; forced `tool_choice` 400s; thinking blocks are tied to the model and the conversation - "preserved thinking"; on the Claude API and Google Cloud the `computer_20251124` tool 400s - use the computer toolset; the advisor tool rejects Claude Opus 4.8, Claude Opus 4.7, and Claude Sonnet 5 advisors), one response-shape change that fails no request (text between tool calls comes back in `thinking` blocks), **recalibrated effort levels** (the default stays `high`, but a level no longer produces the same amount of thinking as on Claude Sonnet 5), and safety classifiers that decline in five categories. Breaking changes 2 and 3 are the same mechanisms Claude Fable 5.1 and Claude Opus 5.5 introduced - the shared mechanics are under § Migrating to Claude Fable 5.1 from Claude Fable 5, and this section gives the Claude Sonnet 5.5 specifics. + +### Breaking change 1: `disabled` thinking returns a 400 - turn thinking off with `between_tools` + +On Claude Sonnet 5, thinking is on by default and `thinking: {type: "disabled"}` turns it off. On Claude Sonnet 5.5, `{"type": "disabled"}` returns a 400 `invalid_request_error`: + +```text +"thinking.type.disabled" is not supported for this model. Use "thinking.type.between_tools" for the lowest thinking setting, or "thinking.type.adaptive" and "output_config.effort" to control thinking behavior. +``` + +`thinking: {"type": "between_tools"}` is the lowest thinking setting on this model: the model does no extended thinking, and the short progress updates it writes between tool calls come back as `thinking` blocks with their summary text. It needs no beta header and works on every platform that offers the model. Its limits, each a 400 `invalid_request_error` when crossed: + +- **Effort `high` or below only.** At `xhigh` or `max`, use adaptive thinking (omit `thinking` or send `{"type": "adaptive"}`). The error reads `output_config.effort 'xhigh' is not supported when thinking is disabled on this model. Use effort 'high' or below, or enable thinking.` - "enable thinking" in this and the next error means adaptive thinking; `{"type": "enabled"}` is itself a 400. +- **No other field inside `thinking`.** `display`, `budget_tokens`, or `block_binding` sent with `between_tools` is rejected, and manual budgets (`{"type": "enabled", "budget_tokens": N}`) return a 400 as well. +- **Effort can't change mid-conversation.** A per-message `output_config.effort` that differs from the level in effect is rejected (`messages.N: output_config.effort 'low' differs from the 'high' in effect before it; ...`). To vary effort per turn, use adaptive thinking. +- **Claude Sonnet 5.5 only.** Any other model rejects it: `"thinking.type.between_tools" is not supported for this model.` - so client-side code that re-sends the same body to another model (a router or a retry) must drop the field first. + +Migrate a route that disables thinking in this order: + +1. **Try adaptive thinking at `low` effort first.** At `low` the model keeps its thinking short and skips it on most simple requests. Measure time to first token at the median and the 95th percentile on the caller's own traffic, and compare quality. +2. **Otherwise send `between_tools` at `high` effort or below.** Remove any instruction that tells the model not to think - such instructions make it more likely to write internal XML tags in its visible output. +3. **Read the response by block `type`, not position.** With adaptive thinking, a response can begin with a `thinking` block whose `thinking` field is empty under the default `display: "omitted"`. +4. **Pass `thinking` blocks back unchanged** with the rest of the assistant turn, including the progress-update blocks `between_tools` returns - a block sent back gives the model the full note it wrote, not the summary. +5. **Size `max_tokens` for thinking as well as the reply.** Thinking counts toward `max_tokens` even when its text isn't returned; for long agentic coding turns, 64,000 is a reasonable starting point. + +```python +# Before - accepted on Claude Sonnet 5, 400 on Claude Sonnet 5.5 +client.messages.create( + model="claude-sonnet-5", + max_tokens=16000, + thinking={"type": "disabled"}, + messages=[{"role": "user", "content": "..."}], +) + +# After, preferred - adaptive thinking (the default) at low effort; measure latency and quality +client.messages.create( + model="claude-sonnet-5-5", + max_tokens=16000, + output_config={"effort": "low"}, + messages=[{"role": "user", "content": "..."}], +) + +# After, when the route must stay thinking-off - the lowest thinking setting, at high effort or below +client.messages.create( + model="claude-sonnet-5-5", + max_tokens=16000, + thinking={"type": "between_tools"}, + output_config={"effort": "high"}, + messages=[{"role": "user", "content": "..."}], +) +``` + +In Python, TypeScript, PHP, and Ruby, write `between_tools` as a plain value in the `thinking` object (Ruby: `type: :between_tools`). In the typed SDKs, send the `thinking` object as a raw override until their types include `between_tools`: C# `Thinking = new ThinkingConfigParam(JsonSerializer.SerializeToElement(new { type = "between_tools" }))`; Go `params.SetExtraFields(map[string]any{"thinking": map[string]any{"type": "between_tools"}})` on the `MessageNewParams` value; Java `.putAdditionalBodyProperty("thinking", JsonValue.from(Map.of("type", "between_tools")))`. + +### Breaking change 2: forced tool use is rejected + +As on Claude Fable 5.1 and Claude Opus 5.5: `tool_choice: {"type": "any"}` and `{"type": "tool", "name": "..."}` return a 400 `invalid_request_error` (`tool_choice: type "tool" and "any" are not supported for this model.`), including on the token-counting endpoint, where Claude Sonnet 5 accepts both. `{"type": "auto"}` (the default) and `{"type": "none"}` are unchanged. Migrate by intent - the patterns are under § Breaking change 1: forced tool use is rejected in § Migrating to Claude Fable 5.1 from Claude Fable 5: + +- **Steering toward a tool:** `tool_choice: {"type": "auto"}` plus a prompt that says when the tool applies, with `strict: true` on the tool definition for schema-valid arguments. Because `auto` does not guarantee a call, check that one was made and retry if it wasn't. +- **Extracting structured data:** if the forced call existed only to get JSON back, use structured outputs (`output_config.format`). + +### Breaking change 3: thinking blocks are tied to the model and the conversation + +- **Model binding - who reads whose blocks.** Claude Sonnet 5.5 reads thinking blocks from Claude Sonnet 5, Claude Opus 4.8, Claude Haiku 4.5, and earlier models - a conversation that moves from Claude Sonnet 5 onto `claude-sonnet-5-5` keeps its reasoning - but **not** from Claude Opus 5, Claude Opus 5.5, or any Claude Fable or Claude Mythos model. **No other model reads Claude Sonnet 5.5 blocks**, so a router switch, a retry on another model, or a refusal fallback runs the turns after the switch without its reasoning. The API drops what the target can't read before the model sees it: the request succeeds, dropped blocks aren't billed, and with the `thinking-binding-controls-2026-08-01` beta header the drop is reported in a top-level `input_transformations` array. Keep passing blocks back unchanged; don't strip them yourself. +- **Conversation binding - the history-editing check.** The API checks that the `system` prompt, the `tools`, and every earlier message are unchanged since a block was produced. It is enforced by default for accounts created on or after August 31, 2026, 00:00 UTC, on the Claude API and Amazon Bedrock (Google Cloud is not confirmed - check the preserved thinking docs before promising either way): on those accounts a request that replays a block after such an edit is a 400. To drop the affected blocks instead, send the `thinking-binding-controls-2026-08-01` beta header and set `thinking.block_binding.prefix_mismatch_behavior: "drop_block"`; on older accounts, setting that field to either value opts the request in. **`block_binding` works only with `thinking: {"type": "adaptive"}`** - with `between_tools`, keep the history append-only, or strip the thinking blocks from the edited turn on. The append-only replacements (mid-conversation `role: "system"` messages, which Claude Sonnet 5.5 supports and Claude Sonnet 5 doesn't; `tool_addition` / `tool_removal`; turn-scoped reminders; server-side compaction or context editing) and the three-step check are in § Migrating to Claude Fable 5.1 from Claude Fable 5. With threshold compaction, thinking blocks from before a `compaction` block aren't carried forward, so the summary is all the model has of that earlier work - if you write your own `instructions`, say what the summary must retain - and remove the `thinking` and `redacted_thinking` blocks from any assistant turn you re-insert after a compaction block (or set `drop_block`). On-demand compaction (beta `compact-2026-09-04`) can keep the kept turns' blocks valid after you replace the summarized turns with the returned `compaction` block. +- **Account binding.** On Amazon Bedrock and Google Cloud at launch, a block Claude Sonnet 5.5 produced works only in the account that produced it, or in an account linked to it; another account's blocks are dropped and the request succeeds (on Google Cloud, with the `thinking-binding-controls-2026-08-01` header, each drop is listed in `input_transformations` with `reason: "organization_binding_mismatch"`). Blocks from earlier models aren't affected. + +### Breaking change 4: computer use needs the toolset on the Claude API and Google Cloud + +On the Claude API and Google Cloud, Claude Sonnet 5.5 accepts computer use only as the `computer_toolset_20260801` toolset; `computer_20251124` returns a 400 (on the Claude API the message begins `'claude-sonnet-5-5' does not support tool types: computer_20251124.`). On Amazon Bedrock it still accepts `computer_20251124`. No platform accepts `computer_20250124`. + +| Version sent today | Starting models that send it | Send on the Claude API and Google Cloud | Send on Amazon Bedrock | +|---|---|---|---| +| `computer_20251124` | Claude Sonnet 5, Sonnet 4.6 | `computer_toolset_20260801` | `computer_20251124` | +| `computer_20250124` | Sonnet 4.5, Haiku 4.5, Sonnet 4 | `computer_toolset_20260801` | `computer_20251124` | + +The toolset's request shape and agent-loop changes (no beta header, no `name` or display size, the action is the member `tool_use` block's `name`, several calls per turn, `"toolset_name": "computer"` echoed on every result) are in `shared/tool-use-concepts.md` § Computer Use and § Migrating to Claude Opus 5.5 -> Breaking change 4. Code that already sends the toolset needs no change. Two more things to check in the agent loop: don't prune old screenshots on the client - removing an earlier screenshot invalidates every later thinking block (breaking change 3), so resize screenshots to 2000 px or less per side and use server-side tool result clearing instead, or, with adaptive thinking, keep `prefix_mismatch_behavior: "drop_block"` set from the first prune on; and replace the `fine-grained-tool-streaming-2025-05-14` header with `eager_input_streaming: true` on each tool that needs it - the header returns a 400 alongside a computer use or browser use toolset entry. + +### Breaking change 5: the advisor tool accepts fewer advisors + +With the advisor tool (beta), a Claude Sonnet 5.5 executor needs one of these advisors: Claude Opus 5, Claude Opus 5.5, Claude Sonnet 5.5, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, or Claude Mythos 5.1. Claude Opus 4.8, Claude Opus 4.7, Claude Opus 4.6, Claude Sonnet 5, and Sonnet 4.6 advisors return a 400. Every accepted advisor returns its advice encrypted, as an `advisor_redacted_result` block, so the advice text isn't readable in the response. Because the executor rejects forced `tool_choice`, nudge a consult from the prompt rather than forcing the `advisor` tool. + +### Text between tool calls comes back in thinking blocks + +On Claude Sonnet 5, text the model writes between tool calls comes back as `text` blocks. On Claude Sonnet 5.5, notes longer than a sentence or two come back as **progress-update `thinking` blocks**, empty under the default `display: "omitted"`; shorter remarks stay `text`. No request fails, but a client that renders only `text` blocks goes quiet between tool calls. With adaptive thinking, set `thinking.display: "updates"` (beta `thinking-display-updates-2026-08-18`) to get the updates alone, or `"summarized"` to get them mixed with reasoning summaries; render each non-empty `thinking` block before the `tool_use` block that follows it, and pass the blocks back unchanged. With `between_tools`, the notes come back with their text and no `display` field is needed (or allowed). + +If the interface doesn't render `thinking` blocks and the model may need to show the user something word for word mid-turn (a code snippet, a question), give it a simple tool for sending the user a message, tell it to reserve that tool for such content, and declare it in the **first** request of the session so the `tools` list doesn't change later (breaking change 3). Remove older instructions such as "hold all findings for the final response"; if updates are then wanted at predictable points, add a line that says when user-facing text is wanted and what it should contain - the model follows it - for example: + +> *"Before you start, say in a line what you're about to do; brief updates while you work help the user follow along. Close with a short recap that stands on its own so a reader who only sees the last message has the full picture."* + +If long tool-calling turns still go quiet for too long, the harness can prompt an update: count consecutive tool-calling steps with no user-facing text or progress update, and after several in a row (for example five) append a one-turn reminder after the latest tool results as a turn-scoped system message (`clear_at: "next_user_message"`, beta `mid-conversation-system-clear-at-2026-08-21` - § Migrating to Claude Fable 5.1 from Claude Fable 5). If the turn stays quiet, stop after two or three reminders; text after every tool result can make the model suspect a prompt injection (§ Behavioral shifts -> Mid-turn user messages). Because the reminder is appended, never inserted and later deleted, the cache and preserved thinking stay intact. In Anthropic's testing at `high` effort, with a send-message tool available, it made the model update the user more often and shortened its longest silent stretches, with no measurable change in task quality: + +> *"The user hasn't heard from you in a while - say in a few words what you're doing, then continue."* + +### Choosing an effort level - recalibrated levels, default still `high` + +Claude Sonnet 5.5 supports `low`, `medium`, `high`, `xhigh`, and `max`; the Claude API default is `high`. The levels are **recalibrated**: a level doesn't produce the same amount of thinking as the same level on Claude Sonnet 5, so re-run the effort sweep against the caller's own evals rather than carrying the Claude Sonnet 5 setting over, and set `output_config.effort` explicitly. + +- **Starting points:** `medium` for agentic coding and multistep tool use; `low` for chat, content generation, classification, extraction, and search. Reserve `xhigh` and `max` for work with a measured quality gain - at those levels thinking can't be turned off. At `low`, on long agentic tasks the model is more likely than at higher levels to stop and check in with the user before finishing, or to skip verifying a change (§ Behavioral shifts -> Verification on coding tasks). +- **Judge cost per completed task, not per token.** In Anthropic's testing it finishes agentic coding and multistep tool-use work in far fewer model requests than Claude Sonnet 5, and generates output faster. On most agentic coding evals it scored higher at `medium` than Claude Sonnet 5 did at `high`, typically at under a fifth of the cost; on computer use at `high` it completed substantially more tasks than Claude Sonnet 5 at its highest effort, with under a third of the tokens. +- **To get less thinking, lower the effort level.** From `medium` up, the model thinks briefly before almost every reply, even a greeting, which adds to the time before the first visible token, and a system-prompt request to think less has almost no effect at those levels. At `low`, it skips thinking on most simple requests. +- **Keep the cache warm when varying effort.** Changing the top-level `effort` between requests invalidates the prompt cache; a per-message effort change (beta `mid-conversation-output-config-2026-07-01`; request shape under § New API features of § Migrating to Claude Fable 5.1 from Claude Fable 5) keeps it - for example, run an interactive session at `low` and raise effort to `high` for a hard problem. Per-message effort needs adaptive thinking; with `between_tools` it is a 400. + +### Safeguards and fallback + +Claude Sonnet 5.5 declines in more categories than Claude Sonnet 5. A decline is a normal HTTP 200 with `stop_reason: "refusal"` and a `stop_details` category: `"cyber"` (could enable cyber harm, such as malware or exploit development - finding vulnerabilities in source code is allowed), `"bio"` (could enable biological harm), `"frontier_llm"` (could assist the development of competing AI models), `"reasoning_extraction"` (asks the model to reproduce its internal reasoning in the response text), and `"general_harms"` (another usage-policy area - benign work can also trigger it). Branch on `stop_reason` before reading `content` (handling: § `refusal` stop reason under § Migrating to Claude Fable 5.1). Server-side fallback (`fallbacks: "default"`, beta `server-side-fallback-2026-07-01`, Claude API only) retries `"cyber"` and `"frontier_llm"` declines on Claude Sonnet 5; it doesn't retry `"bio"`, `"reasoning_extraction"`, or `"general_harms"` declines. The SDK middleware or your own retry are the alternatives; a fallback model runs without Claude Sonnet 5.5's thinking blocks (breaking change 3), and a client-side retry must drop `between_tools` (breaking change 1); with server-side fallback, a `between_tools` request that falls back to Claude Sonnet 5 runs there with `thinking: {"type": "disabled"}`. Whether a refusal that arrives before any output is billed depends on its refusal category, and it counts against rate limits either way. Real-time cyber safeguards are new for code coming from Sonnet 4.6, Sonnet 4.5, and Haiku 4.5 - for legitimate security work, point the user to the [Cyber Verification Program](https://support.claude.com/en/articles/14604842-real-time-cyber-safeguards-on-claude-opus-and-sonnet). The biology safeguards are the same as Claude Sonnet 5's and leave everyday health and educational questions unaffected; if the `bio` classifier gets in the way of an organization's life-sciences work, it can apply to the Life Sciences Verification Program. If a prompt asks the model to write out its reasoning as a substitute for thinking, remove that instruction (it invites `reasoning_extraction` declines) and read `display: "summarized"` blocks instead. + +### What carries over, and what is new versus Claude Sonnet 5 + +- **New on Claude Sonnet 5.5:** mid-conversation system messages (no beta header), mid-conversation tool changes (beta), per-message effort (beta), task budgets (beta `task-budgets-2026-03-13` - but see § Behavioral shifts on interactive sessions), and tool definitions inside a mid-conversation message (beta `inline-tools-2026-09-15`) - none is available on Claude Sonnet 5. +- **Prompt caching:** the minimum cacheable prompt is 512 tokens, down from 1,024 on Claude Sonnet 5 (check the prompt caching docs before quoting the exact value). +- **Carries over:** batch processing, the Files API, PDF support, vision, structured outputs, strict tool use, threshold compaction, compaction on demand (beta `compact-2026-09-04`), and the server-side and client-side tools (computer use as in breaking change 4; on Microsoft Foundry, only what it offers when hosted on Azure). +- **Rate limits and tiers:** its own rate-limit pool, separate from Claude Sonnet 5's and from the combined Sonnet 4.x pool - re-check the tier's Claude Sonnet 5.5 limits before moving volume. Priority Tier is not supported (as on Claude Sonnet 5). +- **SDK constants** (where the pinned SDK has them; the bare string always works): `Model.ClaudeSonnet5_5` (C#), `anthropic.ModelClaudeSonnet5_5` (Go), `Model.CLAUDE_SONNET_5_5` (Java), `Model::CLAUDE_SONNET_5_5` (PHP), `Anthropic::Model::CLAUDE_SONNET_5_5` (Ruby). + +### Behavioral shifts (prompt-tunable) + +None of these break code. Re-evaluate Claude Sonnet 5-specific prompt instructions against them after the effort sweep: + +- **Remove workarounds for what got better.** The model is strongest on multistep agentic coding in a real repository, uses connected tools more reliably in agentic workflows, declines fewer benign requests, and holds a system-prompt role more reliably when a user pastes in a competing persona. If the Claude Sonnet 5 prompts carry workarounds for these - refusal steering, tool-call retry shims, instructions like "do not be lazy" - remove them and re-run the evals before tuning anything else. +- **Tool use in chat and knowledge work.** On chat and knowledge-work tasks the model sometimes answers from its own knowledge or from public web results when a connected tool, skill, or internal search would serve better, and can hold off on tools until asked directly. Remove language that discourages tool use ("only use tools when strictly necessary", "minimize tool calls" - it follows these literally). Where the product should prefer connected sources (enterprise search, account research, support agents), add: *"Use the search tool to check specifics that may have changed since your training, such as what is allowed, required or charged, even when you feel confident. For researched work such as a report or a comparison, gather current sources rather than writing from your training knowledge."* +- **Mid-turn user messages and task budgets.** The model pays close attention to where text sits relative to a tool result: a message the user typed mid-task that arrives as a mid-conversation system message placed directly after a tool result, or inside a `tool_result` block, can be read as a prompt-injection attempt (the model says so and usually ignores it or waits for confirmation). Task budgets can cause this, because the budget countdown arrives as a system message after every tool result, and so can any harness text added after the tool results on every step (a token countdown, per-step instructions or context); an occasional one-turn reminder arrives far less often - if one draws this reaction, send it less often. Deliver mid-turn user input as a user turn - a text block in the user message that carries the `tool_result` blocks, after the last `tool_result`; keep harness notices (budget countdowns, background-task completions, reminders) in a separate mid-conversation system message that follows, never in the same block as the user's words; never put user text inside a `tool_result` block; and on interactive sessions don't use a task budget (control cost with effort and `max_tokens`; keep task budgets for unattended agentic loops). +- **Verification on coding tasks.** Mostly at `low` effort, the model sometimes reports a code change as done without a check that exercises it (no `npm install`, so the tests and type-checker never ran; only a syntax check; stopping silently when a build tool is missing). If a coding agent runs at `low`, or changes are reported complete without test or build output, add to the system prompt: *"When you change code that can be run, built, or type-checked, run a real check that exercises the change before reporting it done: the project's tests, type-checker, or build, or the changed command itself. A syntax-only check, or a check command that failed to start, does not count; if all that is missing is the project's declared dependencies, install them with its own package manager (e.g. npm install, pip install -r requirements.txt) unless told not to. Only if no real check can run here, say which one you did not run and why instead of reporting the change as done."* +- **Tolerant tool-call handling.** The model occasionally calls a declared tool by a name that differs only in letter case (`bash` for `Bash`), or passes a known parameter under a slightly different name. Don't treat that as fatal: accept the call when the match is unambiguous, or return a `tool_result` with `is_error: true` that states the exact expected name - the model usually corrects the call on its next turn. +- **Dense charts and technical drawings.** Give the model a way to crop, zoom, or run code on the image; it reads them markedly more accurately with such tools. On charts the tools help at every effort level and more than raising effort does (with tools at `high` it read charts more accurately than without them at `max`, at a fraction of the cost); on technical drawings they help only from `high` up, most at `xhigh` and `max`. +- **Follow-up turns in multi-turn chat.** If the model should treat its earlier answers as settled rather than going back over them when it thinks about a new message, add at the end of the system prompt: *"Once Claude has answered something, Claude treats that answer as done. On later turns Claude's thinking goes to what the person is asking now, and Claude doesn't go back over an earlier answer unless the person asks about it or points out a problem with it."* Leave it out where earlier work should keep being re-examined - long analyses, or agentic tasks where a later step can reveal a mistake in an earlier one. + +### Claude Sonnet 5.5 Migration Checklist + +- [ ] **[BLOCKS]** Update the `model=` string to `claude-sonnet-5-5` (`anthropic.claude-sonnet-5-5` on Amazon Bedrock); no date suffix. +- [ ] **[BLOCKS]** Replace `thinking: {type: "disabled"}`: try adaptive thinking at `low` effort first; where the route must stay thinking-off, send `{type: "between_tools"}` at effort `high` or below, with no other field in `thinking` and no per-message effort change; drop it from any request client-side code re-sends to another model. +- [ ] **[BLOCKS]** Coming from Sonnet 4.6 or earlier or from Haiku 4.5: replace `budget_tokens` with an effort level and remove non-default `temperature` / `top_p` / `top_k`; from Sonnet 4.5 or earlier or Haiku 4.5, also replace assistant prefills. +- [ ] **[BLOCKS]** Read content blocks by `type` (a response can begin with `thinking` blocks) and pass `thinking` blocks back unchanged; size `max_tokens` for thinking plus the reply. +- [ ] **[BLOCKS]** Replace `tool_choice` `any` / `tool` with `auto` plus `strict: true` (steering in the prompt, and a check that the call happened) or structured outputs - on `count_tokens` too. +- [ ] **[BLOCKS]** If the harness builds `messages` itself: keep it append-only and run the preserved-thinking three-step check (§ Migrating to Claude Fable 5.1 from Claude Fable 5) - new accounts are enforced by default on the Claude API and Amazon Bedrock; `block_binding` needs adaptive thinking. +- [ ] **[BLOCKS]** On the Claude API and Google Cloud, move computer use to `computer_toolset_20260801` and update the agent loop; on Amazon Bedrock send `computer_20251124`. +- [ ] **[BLOCKS]** Advisor tool: pair the executor with an accepted advisor (not Claude Opus 4.8 / 4.7 / 4.6, Claude Sonnet 5, or Sonnet 4.6) and expect encrypted `advisor_redacted_result` advice. +- [ ] **[BLOCKS]** Handle `stop_reason: "refusal"` before reading `content` (five categories) and configure fallback; only `cyber` and `frontier_llm` declines are retried by server-side fallback. +- [ ] **[TUNE]** Re-run the effort sweep and set `effort` explicitly (start at `medium` for agentic coding, `low` for chat); lower effort rather than prompting for less thinking; use per-message effort to vary it without losing the cache. +- [ ] **[TUNE]** If the UI showed text between tool calls: `display: "updates"` (beta) or `"summarized"` with adaptive thinking, render non-empty `thinking` blocks; declare any send-message tool at session start; remove "hold all findings" instructions; if turns still go quiet, a turn-scoped reminder after about five silent steps, at most two or three times. +- [ ] **[TUNE]** Prompts: remove workarounds (refusal steering, tool-call retry shims, "do not be lazy") and tool-discouraging language; deliver mid-turn user input as a user turn and drop task budgets on interactive sessions; add the verification paragraph for low-effort coding agents; accept or correct near-miss tool names instead of failing; give crop/zoom/code tools for dense charts and drawings; the settled-answers line for multi-turn chat where it fits. +- [ ] **[TUNE]** Re-baseline cost and latency at the chosen effort level (same prices as Claude Sonnet 5; own rate-limit pool; no Priority Tier). + +--- + + +## Verify the Migration + +After updating, spot-check that the new model is actually being used. Replace `YOUR_TARGET_MODEL` with the model string you migrated to (e.g. `claude-fable-5-1`, `claude-opus-5-5`, `claude-opus-5`, `claude-opus-4-8`, `claude-opus-4-7`, `claude-sonnet-5-5`, `claude-sonnet-5`, `claude-sonnet-4-6`, `claude-haiku-4-5`) and keep the assertion prefix in sync: + +```python +YOUR_TARGET_MODEL = "claude-opus-5-5" # or "claude-opus-5", "claude-opus-4-7", "claude-sonnet-5-5", "claude-sonnet-5", "claude-sonnet-4-6", "claude-haiku-4-5" response = client.messages.create(model=YOUR_TARGET_MODEL, max_tokens=64, messages=[...]) assert response.model.startswith(YOUR_TARGET_MODEL), response.model ``` +Prefix collision: `claude-opus-5-5` starts with `claude-opus-5`, so when your target is `claude-opus-5`, also assert `not response.model.startswith("claude-opus-5-5")`. The same holds for Sonnet: `claude-sonnet-5-5` starts with `claude-sonnet-5`, so a `claude-sonnet-5` check also asserts `not response.model.startswith("claude-sonnet-5-5")`. + For rate-limit headroom changes, pricing, or capability deltas (vision, structured outputs, effort support), query the Models API: ```python @@ -1870,3 +2244,9 @@ m.capabilities["effort"]["max"]["supported"] ``` See `shared/models.md` for the full capability lookup pattern. + +--- + +## Ground the migration with an eval + +A spot-check confirms the new model answers; it doesn't confirm the app still behaves the way the user wants. When the user reports a behavioral regression on the new model - e.g. *"it refuses things the old one handled fine"*, *"tool calls dropped off after the swap"*, *"responses got twice as long"* - don't tune the prompt by feel. Read `shared/evals/build-eval.md` and build a small eval that captures the regression, then read `shared/evals/eval-hillclimb.md` to iterate the prompt or harness against that eval until the score moves. Grounding the fix in an eval keeps the migration decision honest and leaves the user with a regression test for the next model swap. diff --git a/skills/claude-api/shared/models.md b/skills/claude-api/shared/models.md index cc124164d..b683de02d 100644 --- a/skills/claude-api/shared/models.md +++ b/skills/claude-api/shared/models.md @@ -61,23 +61,27 @@ curl https://api.anthropic.com/v1/models/claude-opus-4-8 \ | Claude Mythos 5.1 | `claude-mythos-5-1` | - | 1M | 128K | Active (Project Glasswing only) | | Claude Fable 5 | `claude-fable-5` | - | 1M | 128K | Active | | Claude Mythos 5 | `claude-mythos-5` | - | 1M | 128K | Active (Project Glasswing only) | +| Claude Opus 5.5 | `claude-opus-5-5` | - | 1M | 128K | Active | | Claude Opus 5 | `claude-opus-5` | - | 1M | 128K | Active | | Claude Opus 4.8 | `claude-opus-4-8` | - | 1M | 128K | Active | | Claude Opus 4.7 | `claude-opus-4-7` | - | 1M | 128K | Active | | Claude Opus 4.6 | `claude-opus-4-6` | - | 1M | 128K | Active | +| Claude Sonnet 5.5 | `claude-sonnet-5-5` | - | 1M | 128K | Active | | Claude Sonnet 5 | `claude-sonnet-5` | - | 1M | 128K | Active | | Claude Sonnet 4.6 | `claude-sonnet-4-6` | - | 1M | 128K | Active | | Claude Haiku 4.5 | `claude-haiku-4-5` | `claude-haiku-4-5-20251001` | 200K | 64K | Active | ### Model Descriptions -- **Claude Fable 5.1** - Anthropic's most capable widely released model, for the most demanding reasoning and long-horizon agentic work. Successor to Claude Fable 5 in the same tier at the same per-token price ($10/$50 per MTok; cache reads $0.25/MTok - 0.025x, a quarter of Claude Fable 5's; batch $5/$25); stronger long-running agentic coding, knowledge work with documents/spreadsheets/slides, multistep research, vision, long-context retrieval, and computer use. Same API surface as Claude Fable 5 (thinking always on, no prefill, no sampling params, `refusal` stop reason, 512-token cache minimum) with three breaking changes: forced tool use (`tool_choice` `any` / `tool`) returns a 400; thinking blocks are bound to the producing model (only Claude Mythos 5.1 can read them - other models drop them); and editing earlier turns invalidates thinking blocks ("preserved thinking"; new accounts created on/after 2026-08-31 get a 400 on edited history; later models enforce it for everyone; the opt-in controls are per-platform - `shared/platform-availability.md`). Adds per-message `effort`, turn-scoped `clear_at` system messages, `thinking.display: "updates"` progress updates, and content provenance. Same tokenizer as Claude Fable 5; 1M context (default), 128K max output. Covered Model: 30-day retention required (ZDR only if expressly authorized by Anthropic) - ZDR orgs get `400 invalid_request_error`, as on Claude Fable 5. No Priority Tier; shares the Fable 5.x rate-limit pool. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5. +- **Claude Fable 5.1** - Anthropic's most capable widely released model, for the most demanding reasoning and long-horizon agentic work. Successor to Claude Fable 5 in the same tier at the same per-token price ($10/$50 per MTok; cache reads $0.25/MTok - 0.025x, a quarter of Claude Fable 5's; batch $5/$25); stronger long-running agentic coding, knowledge work with documents/spreadsheets/slides, multistep research, vision, long-context retrieval, and computer use. Same API surface as Claude Fable 5 (thinking always on, no prefill, no sampling params, `refusal` stop reason, 512-token cache minimum) with three breaking changes: forced tool use (`tool_choice` `any` / `tool`) returns a 400; thinking blocks are bound to the producing model (only Claude Mythos 5.1 can read them - other models drop them); and editing earlier turns invalidates thinking blocks ("preserved thinking"; new accounts created on/after 2026-08-31 get a 400 on edited history on every platform, and enforcement scope is decided per model; the opt-in controls beta is on the Claude API, Claude Platform on AWS, Bedrock, and Vertex - Foundry unconfirmed, `shared/platform-availability.md`). Adds per-message `effort`, turn-scoped `clear_at` system messages, `thinking.display: "updates"` progress updates, and content provenance. Same tokenizer as Claude Fable 5; 1M context (default), 128K max output. Covered Model: 30-day retention required (ZDR only if expressly authorized by Anthropic) - ZDR orgs get `400 invalid_request_error`, as on Claude Fable 5. No Priority Tier; shares the Fable 5.x rate-limit pool. See `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5. - **Claude Fable 5** / **Claude Mythos 5** (`claude-fable-5` / `claude-mythos-5`) - the previous Fable / Mythos release: same tier, limits and per-token pricing as Claude Fable 5.1, which adds three breaking API changes over them (see above; cache reads here are $1/MTok rather than Claude Fable 5.1's $0.25); still served and selectable by id. Claude Mythos 5 ran no safety classifiers, so `stop_reason: "refusal"` does not occur on it. Prefer claude-fable-5-1 for new work. -- **Claude Mythos 5.1** - The same model as Claude Fable 5.1 (same capabilities, limits, per-token pricing, API behavior), offered only to approved Project Glasswing customers; successor to Claude Mythos 5 (which itself succeeded the invitation-only `claude-mythos-preview`). Unlike Claude Mythos 5 it runs safeguards that depend on the access program, so handle `stop_reason: "refusal"`. Not offered on Claude Platform on AWS. Use it only when the org participates in Project Glasswing; otherwise use `claude-fable-5-1`. +- **Claude Mythos 5.1** - The same model as Claude Fable 5.1 (same capabilities, limits, per-token pricing, API behavior - except it does not run the history-editing check), offered only to approved Project Glasswing customers; successor to Claude Mythos 5 (which itself succeeded the invitation-only `claude-mythos-preview`). Unlike Claude Mythos 5 it runs safeguards that depend on the access program, so handle `stop_reason: "refusal"`. Not offered on Claude Platform on AWS. Use it only when the org participates in Project Glasswing; otherwise use `claude-fable-5-1`. +- **Claude Opus 5.5** - Successor to Claude Opus 5 in the Opus line for long-running agentic coding and knowledge work, at a lower price ($4 / $20 per MTok; cache reads $0.20). Same 1M context, 128K output, tokenizer, and feature set as Claude Opus 5, with four breaking changes: thinking can't be disabled (effort is the only control, default `medium`), forced `tool_choice` 400s, thinking blocks are tied to the model and the conversation, and computer use needs the `computer_toolset_20260801` toolset. Broader safety classifiers (`bio` and `reasoning_extraction` join `cyber`). The current Opus and the default model; see `shared/model-migration.md` -> Migrating to Claude Opus 5.5. - **Claude Opus 5** - For complex agentic coding and enterprise work; a step-change over Claude Opus 4.8, strongest on deep reasoning, agentic and long-horizon work, and test-time compute scaling, at half the cost of Claude Fable 5.1 (Claude Fable 5.1 remains the highest-capability tier). Safety classifiers can return `stop_reason: "refusal"` - handle it before reading `content`. A drop-in upgrade at Opus 4.8's pricing ($5/$25 per MTok) with the same feature set. Thinking is on by default (omitting `thinking` runs adaptive; `{type: "adaptive"}` is equivalent), and `thinking: {type: "disabled"}` is available only at effort `high` or lower - pairing it with `xhigh`/`max` returns a 400. Raw thinking tokens are never returned. Full effort ladder through `max`; 512-token prompt-cache minimum (down from 1024 on Opus 4.8); fast mode on the Claude API only. Elevated cybersecurity safeguards. Separate rate-limit bucket from the combined Opus 4.x pool. 1M context window (default and maximum), 128K max output. See `shared/model-migration.md` -> Migrating to Claude Opus 5. - **Claude Opus 4.8** - The most capable model in the Opus 4 series - highly autonomous, state-of-the-art on long-horizon agentic work, knowledge work, and memory; clearer, warmer writing. Same API surface as Opus 4.7 (adaptive thinking only; sampling parameters and `budget_tokens` removed). 1M context window at standard API pricing (no long-context premium). See `shared/model-migration.md` -> Migrating to Opus 4.8 - a 4.7 -> 4.8 move is a model-ID swap plus prompt re-tuning, no new breaking changes. - **Claude Opus 4.7** - Previous-generation Opus. Highly autonomous; strong on long-horizon agentic work, knowledge work, vision, and memory. Adaptive thinking only; sampling parameters and `budget_tokens` removed. 1M context window. See `shared/model-migration.md` -> Migrating to Opus 4.7. - **Claude Opus 4.6** - Older Opus. Supports adaptive thinking (recommended), 128K max output tokens (requires streaming for large outputs). 1M context window. -- **Claude Sonnet 5** - The best combination of speed and intelligence in the Sonnet tier; near-Opus quality on coding and agentic work. Adaptive thinking on by default (omitting `thinking` runs adaptive); manual `budget_tokens` removed; non-default sampling parameters rejected. `effort` supports `low`/`medium`/`high`/`xhigh`/`max`. New tokenizer (~30% more tokens for the same text vs Sonnet 4.6). High-resolution vision (2576px). 1M context window, 128K max output. See `shared/model-migration.md` -> Migrating to Claude Sonnet 5. +- **Claude Sonnet 5** - The previous Sonnet; near-Opus quality on coding and agentic work. Adaptive thinking on by default (omitting `thinking` runs adaptive); manual `budget_tokens` removed; non-default sampling parameters rejected. `effort` supports `low`/`medium`/`high`/`xhigh`/`max`. New tokenizer (~30% more tokens for the same text vs Sonnet 4.6). High-resolution vision (2576px). 1M context window, 128K max output. See `shared/model-migration.md` -> Migrating to Claude Sonnet 5. +- **Claude Sonnet 5.5** - Successor to Claude Sonnet 5 in the Sonnet line, at the same prices ($2 / $10 per MTok; cache reads $0.20). Same tokenizer as Claude Sonnet 5; 1M context, 128K max output. Adaptive thinking on by default; effort default `high`, with recalibrated levels. Five breaking changes: `thinking: {type: "disabled"}` returns a 400 (send `{type: "between_tools"}` at effort `high` or below to turn thinking off), forced `tool_choice` 400s, thinking blocks are tied to the model and the conversation, computer use on the Claude API and Google Cloud needs the `computer_toolset_20260801` toolset, and the advisor tool rejects Claude Opus 4.8, Claude Opus 4.7, and Claude Sonnet 5 advisors. See `shared/model-migration.md` -> Migrating to Claude Sonnet 5.5. - **Claude Sonnet 4.6** - Previous-generation Sonnet. Supports adaptive thinking (recommended). 1M context window. 128K max output tokens. - **Claude Haiku 4.5** - Fastest and most cost-effective model for simple tasks. @@ -86,7 +90,7 @@ curl https://api.anthropic.com/v1/models/claude-opus-4-8 \ | Friendly Name | Alias (use this) | Full ID | Status | |-------------------|---------------------|-------------------------------|--------| | Claude Opus 4.5 | `claude-opus-4-5` | `claude-opus-4-5-20251101` | Active | -| Claude Opus 4.1 | `claude-opus-4-1` | `claude-opus-4-1-20250805` | Deprecated (retires 2026-08-05 - migrate to `claude-opus-5`) | +| Claude Opus 4.1 | `claude-opus-4-1` | `claude-opus-4-1-20250805` | Deprecated (retires 2026-08-05 - migrate to `claude-opus-5-5`) | | Claude Sonnet 4.5 | `claude-sonnet-4-5` | `claude-sonnet-4-5-20250929` | Active | ## Deprecated Models (retiring soon) @@ -121,21 +125,24 @@ When a user asks for a model by name, use this table to find the correct model I | "mythos", "mythos 5.1" | `claude-mythos-5-1` (Project Glasswing participants only; otherwise use `claude-fable-5-1`) | | "fable 5", "mythos 5" (previous version) | `claude-fable-5` / `claude-mythos-5` (still served; prefer `claude-fable-5-1` for new work) | | "mythos preview" | `claude-mythos-5-1` (successor to `claude-mythos-preview` - see migration guide) | -| "opus" | `claude-opus-5` | +| "opus" | `claude-opus-5-5` | | "opus 5" | `claude-opus-5` | +| "opus 5.5" | `claude-opus-5-5` | | "opus 4.8" | `claude-opus-4-8` | | "opus 4.7" | `claude-opus-4-7` | | "opus 4.6" | `claude-opus-4-6` | | "opus 4.5" | `claude-opus-4-5` | -| "opus 4.1" | `claude-opus-4-1` (deprecated, retires 2026-08-05 - suggest `claude-opus-5`) | -| "opus 4", "opus 4.0" | `claude-opus-4-0` (deprecated - suggest `claude-opus-5`) | -| "sonnet", "balanced" | `claude-sonnet-5` | +| "opus 4.1" | `claude-opus-4-1` (deprecated, retires 2026-08-05 - suggest `claude-opus-5-5`) | +| "opus 4", "opus 4.0" | `claude-opus-4-0` (deprecated - suggest `claude-opus-5-5`) | +| "sonnet", "balanced" | `claude-sonnet-5-5` | | "sonnet 5" | `claude-sonnet-5` | +| "sonnet 5.5" | `claude-sonnet-5-5` | +| "cheapest sonnet", "newest sonnet", "latest sonnet" (any attribute phrasing) | `claude-sonnet-5-5` | | "sonnet 4.6" | `claude-sonnet-4-6` | | "sonnet 4.5" | `claude-sonnet-4-5` | -| "sonnet 4", "sonnet 4.0" | `claude-sonnet-4-0` (deprecated - suggest `claude-sonnet-5`) | -| "sonnet 3.7" | Retired - suggest `claude-sonnet-5` | -| "sonnet 3.5" | Retired - suggest `claude-sonnet-5` | +| "sonnet 4", "sonnet 4.0" | `claude-sonnet-4-0` (deprecated - suggest `claude-sonnet-5-5`) | +| "sonnet 3.7" | Retired - suggest `claude-sonnet-5-5` | +| "sonnet 3.5" | Retired - suggest `claude-sonnet-5-5` | | "haiku", "fast", "cheap" | `claude-haiku-4-5` | | "haiku 4.5" | `claude-haiku-4-5` | | "haiku 3.5" | Retired - suggest `claude-haiku-4-5` | diff --git a/skills/claude-api/shared/platform-availability.md b/skills/claude-api/shared/platform-availability.md index 794543737..61ad66dec 100644 --- a/skills/claude-api/shared/platform-availability.md +++ b/skills/claude-api/shared/platform-availability.md @@ -2,52 +2,53 @@ Which features work on which provider platform. **This table is the single source of truth in this skill** - per-feature sections elsewhere point here instead of restating availability. When writing code for a third-party platform (Bedrock, Vertex, Foundry) or Claude Platform on AWS, check this table first; a feature not supported there means use the first-party Claude API surface or a different approach. -Columns: **1P** = first-party Claude API, **P-AWS** = Claude Platform on AWS (Anthropic-operated, same-day parity), **Bedrock** = Amazon Bedrock, **Vertex** = Google Cloud Vertex AI, **Foundry** = Microsoft Foundry. Yes = GA, beta = beta, No = not supported. +Columns: **1P** = first-party Claude API, **P-AWS** = Claude Platform on AWS (Anthropic-operated, same-day parity), **Bedrock** = Amazon Bedrock, **Vertex** = Google Cloud Vertex AI, **Foundry** = Microsoft Foundry. Yes = GA, beta = beta, No = not supported, unconfirmed = not verified either way when this was written. | Feature | 1P | P-AWS | Bedrock | Vertex | Foundry | Notes | |---|---|---|---|---|---|---| | Messages, streaming, tool use | Yes | Yes | Yes | Yes | Yes | Core API | -| PDF input | Yes | Yes | Yes | Yes | beta | | -| Structured outputs / strict tool use | Yes | Yes | Yes | Yes | beta | | -| Adaptive thinking / effort | Yes | Yes | Yes | Yes | beta | | -| Extended thinking | Yes | Yes | Yes | Yes | beta | | +| PDF input | Yes | Yes | Yes | Yes | Yes | | +| Structured outputs / strict tool use | Yes | Yes | Yes | Yes | Yes | | +| Adaptive thinking / effort | Yes | Yes | Yes | Yes | Yes | | +| Extended thinking | Yes | Yes | Yes | Yes | Yes | | | Prompt caching (5m, 1h) | Yes | Yes | Yes | Yes | Yes | | | Automatic prompt caching | Yes | Yes | Yes | Yes | Yes | The legacy Bedrock integration (Opus 4.6 and earlier) rejects top-level `cache_control` with a 400 - explicit breakpoints only there | -| Token counting | Yes | Yes | Yes | Yes | beta | | -| Citations | Yes | Yes | Yes | Yes | beta | | -| Search results content blocks | Yes | Yes | Yes | Yes | beta | | -| Fine-grained tool streaming | Yes | Yes | Yes | Yes | Yes | | +| Token counting | Yes | Yes | Yes | Yes | Yes | | +| Citations | Yes | Yes | Yes | Yes | Yes | | +| Search results content blocks | Yes | Yes | Yes | Yes | Yes | | +| Fine-grained tool streaming | Yes | Yes | Yes | Yes | Yes | Bedrock: `eager_input_streaming` on the newer serving stack only (Opus 4.7/4.8/5, Fable 5, Sonnet 4.6/5); older deployments (Opus 4.5/4.6, Sonnet 4.0/4.5, Haiku 4.5) 400 on the field | | Compaction | beta | beta | beta | beta | beta | | | Context editing | beta | beta | beta | beta | beta | | -| Context windows (1M) | Yes | Yes | Yes | Yes | beta | | +| Context windows (1M) | Yes | Yes | Yes | Yes | Yes | | | `inference_geo` (data residency) | Yes | Yes | No | No | No | | | **Server-side tools** | | | | | | | -|   Web search | Yes | Yes | No | Yes | beta | Vertex: basic `web_search_20250305` only (no `_20260209` dynamic filtering) | -|   Web fetch | Yes | Yes | No | No | beta | | -|   Code execution | Yes | Yes | No | No | beta | | -|   Tool search | Yes | Yes | Yes | Yes | beta | Bedrock: InvokeModel API only, not Converse | +|   Web search | Yes | Yes | No | Yes | Yes | Vertex: basic `web_search_20250305` only (no `_20260209` dynamic filtering). Foundry Hosted on Azure: basic `web_search_20250305` only | +|   Web fetch | Yes | Yes | No | No | Yes | Foundry Hosted on Azure: basic `web_fetch_20250910` only | +|   Code execution | Yes | Yes | No | No | Yes | Foundry: Hosted on Anthropic deployments only - Hosted on Azure returns a 400 | +|   Tool search | Yes | Yes | Yes | Yes | Yes | Bedrock: InvokeModel API only, not Converse | |   Advisor tool | beta | beta | No | No | No | | | **Client-implemented tools** | | | | | | | -|   Bash, text editor, memory | Yes | Yes | Yes | Yes | beta | | -|   Computer use | beta | beta | beta | beta | beta | | +|   Bash, text editor, memory | Yes | Yes | Yes | Yes | Yes | | +|   Computer use | beta | beta | beta | beta | beta | `computer_20251124` and older versions: beta on all five platforms. Claude Opus 5.5 accepts only `computer_toolset_20260801` (GA, no beta header) on the Claude API and Google Cloud, and still accepts `computer_20251124` on Amazon Bedrock (`shared/model-migration.md` -> Migrating to Claude Opus 5.5, breaking change 4). Claude Sonnet 5.5 accepts only the toolset on the Claude API and Google Cloud, but still accepts `computer_20251124` on Amazon Bedrock, and rejects `computer_20250124` everywhere (`shared/model-migration.md` -> Migrating to Claude Sonnet 5.5, breaking change 4) | | **Agentic / orchestration** | | | | | | | -|   Agent Skills (Messages API) | Yes | Yes | No | No | beta | | -|   Programmatic tool calling | Yes | Yes | No | No | beta | | +|   Agent Skills (Messages API) | Yes | Yes | No | No | beta | Foundry: Hosted on Anthropic deployments only - Hosted on Azure returns a 400 | +|   Programmatic tool calling | Yes | Yes | No | No | Yes | Foundry: Hosted on Anthropic deployments only - Hosted on Azure returns a 400 | |   MCP connector | beta | beta | No | No | beta | | |   Managed Agents | beta | beta | No | No | No | Foundry: No (inferred; not in Foundry docs either way) | |   Self-hosted sandboxes | beta | beta | No | No | No | P-AWS: worker authenticates with IAM/SigV4 or an AWS-Console API key + `AnthropicSelfHostedEnvironmentAccess` (Console environment keys don't work there); sessions on self-hosted environments cannot attach memory stores; `GET /v1/environments/{id}/work` list endpoint not supported, other work endpoints OK | | **API endpoints** | | | | | | | |   Message Batches | Yes | Yes | No | No | No | | -|   Files API | Yes | Yes | No | No | beta | | +|   Files API | Yes | Yes | No | No | beta | Foundry: Hosted on Anthropic deployments only - Hosted on Azure returns a 400 | |   Models API | Yes | Yes | No | No | No | | | **Other** | | | | | | | -|   Mid-conversation system messages | Yes | Yes | Yes | Yes | No | Claude Opus 5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, Claude Mythos 5.1; not Claude Sonnet 5. Bedrock: InvokeModel passthrough, not ARN-versioned models | +|   Mid-conversation system messages | Yes | Yes | Yes | Yes | No | Claude Opus 5, Claude Opus 5.5, Claude Opus 4.8, Claude Fable 5, Claude Fable 5.1, Claude Mythos 5, Claude Mythos 5.1, Claude Sonnet 5.5; not Claude Sonnet 5. Bedrock: InvokeModel passthrough, not ARN-versioned models | +|   Mid-conversation tool changes | beta | beta | beta | beta | No | Same models as mid-conversation system messages; beta `mid-conversation-tool-changes-2026-07-01` | |   Turn-scoped (`clear_at`) system messages | beta | beta | beta | beta | No | Same models as mid-conversation system messages; beta `mid-conversation-system-clear-at-2026-08-21` (on Bedrock/Vertex pass the value as a beta) | -|   Per-message `effort` (system message `output_config`) | beta | No | No | No | No | Claude Fable 5.1, Claude Mythos 5.1, Claude Opus 5; beta `mid-conversation-output-config-2026-07-01`; Claude API at launch (Bedrock/Vertex/Foundry unconfirmed; Claude Opus 5 excluded on Bedrock) | -|   `thinking.display: "updates"` | beta | beta | beta | beta | beta | Claude Fable 5.1, Claude Mythos 5.1, Claude Fable 5; beta `thinking-display-updates-2026-08-18` (pass the beta value per platform); without it `"updates"` is rejected as an unknown `display` value | -|   Thinking block-binding controls | beta | beta | per model | per model | No | `thinking.block_binding` + `input_transformations`; beta `thinking-binding-controls-2026-08-01` (on Bedrock via the `anthropic_beta` body field); the controls beta arrives per model on Bedrock/Vertex - until then the header is rejected; the history-editing enforcement itself follows the account-age rule in `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 | +|   Per-message `effort` (system message `output_config`) | beta | unconfirmed | unconfirmed | beta | unconfirmed | Claude Fable 5.1, Claude Mythos 5.1, Claude Opus 5, Claude Opus 5.5, Claude Sonnet 5.5 (thinking on only - a 400 with `between_tools`); beta `mid-conversation-output-config-2026-07-01`; on the Claude API and Google Cloud, open to any organization that sends the header (Claude Platform on AWS/Bedrock/Foundry unconfirmed; Claude Opus 5 excluded on Bedrock) | +|   `thinking.display: "updates"` | beta | beta | beta | beta | beta | Claude Fable 5.1, Claude Mythos 5.1, Claude Fable 5, Claude Opus 5.5, Claude Sonnet 5.5 (with adaptive thinking); beta `thinking-display-updates-2026-08-18` (pass the beta value per platform); without it `"updates"` is rejected as an unknown `display` value | +|   Thinking block-binding controls | beta | beta | beta | beta | unconfirmed | `thinking.block_binding` + `input_transformations`; beta `thinking-binding-controls-2026-08-01` (the same beta name on the Claude API, Claude Platform on AWS, Bedrock, and Vertex - Bedrock: the `anthropic_beta` body field, Vertex: the `anthropic-beta` HTTP header); Foundry unconfirmed; wherever the header is rejected, use strip-and-retry; the history-editing enforcement itself follows the account-age rule in `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 | |   Server-side `fallbacks` | beta | beta | No | No | No | `"default"` -> beta `server-side-fallback-2026-07-01`; array form -> beta `server-side-fallback-2026-06-01` | -|   Fast mode | beta | No | No | No | No | Research preview, beta `fast-mode-2026-02-01`, first-party API only | +|   Fast mode | beta | No | No | No | No | Research preview, beta `fast-mode-2026-02-01`, first-party API only (Claude Opus 5 / Opus 4.8 at $10 / $50; Claude Opus 5.5 at $8 / $40) | |   Cache diagnostics | beta | No | No | No | No | First-party API only | |   Task budgets | beta | beta | No | No | No | Beta header `task-budgets-2026-03-13`; 3P availability not documented - assume unsupported | diff --git a/skills/claude-api/shared/preserved-thinking-migration.md b/skills/claude-api/shared/preserved-thinking-migration.md new file mode 100644 index 000000000..9276dab64 --- /dev/null +++ b/skills/claude-api/shared/preserved-thinking-migration.md @@ -0,0 +1,249 @@ +# Preserved Thinking - Keeping Earlier Reasoning Valid Across a Conversation + +> **If you arrived via `/claude-api preserved-thinking-migration` (or opened this file directly):** this is the right file. Execute the steps below in order rather than summarizing the guide back to the user - presenting the break profile, the ranked causes, and the measured result of each fix IS part of the execution. Start with Step 0 (scope, quality bar, baseline) and finish with Step 4's two deliverables: the break profile and the changes. + +Preserved thinking is measured in units of **conversations that keep their reasoning, not requests that pass**. One edit to an earlier turn invalidates every thinking block after it, and the same stale block fails again on every later request that replays it - so a per-request count overstates the damage and a per-conversation count (did this conversation break, and at which turn) is the number that tells you whether a fix worked. + +**What the check is, in one paragraph.** On models with preserved thinking (Claude Fable 5.1, Claude Opus 5.5, and Claude Sonnet 5.5 today; the Preserved thinking page lists the models and the enforced accounts, and says later models will enforce the check for all users), each `thinking` block's `signature` records the conversation that produced it - the top-level `system` prompt, the set of `tools`, and every message before the block - and the model that produced it. When the transcript comes back, the API recomputes that record from what you sent and requires a match (a separate model check decides whether the current model can read the block at all; see "Switching models mid-conversation" in `shared/preserved-thinking-migration/causes.md`). Integrations that keep the history append-only never notice. Integrations that rewrite earlier turns between requests - truncation, client-side compaction, a re-rendered system prompt, a tool list that grows when a plugin connects, a per-turn reminder that is injected and then stripped, old tool results trimmed after the fact, media dropped by a size cap, a lossy round trip through the app's own message types - lose the reasoning after the edit point (`drop_block`) or fail the request (`error`). The check compares the conversation *as you sent it*, before any server-side edit, so Anthropic's own server-side compaction and context editing never count as edits. The published explanation lives in `shared/model-migration.md` -> Migrating to Claude Fable 5.1 from Claude Fable 5 -> **Breaking change 3** (the three-step check and the append-only form of each edit; read it first if the check is new to you) - this workflow restates the rules only as a lookup, the cause table and keep list in `shared/preserved-thinking-migration/causes.md`; it finds which edits *this* harness makes, proves them with the API's own response, fixes them one at a time, and proves the fix the same way. + +**Where this workflow sits**: the `migrate` subcommand explains the check and lists the append-only form of each edit; this workflow is its executable form - scan, measure, fix, re-measure - for a harness that already exists. `cost-optimize` is a sibling, not a prerequisite: a history that invalidates its own thinking also restarts the prompt cache at the same point, so a fix here usually shows up in its cache-hit numbers too, and a prefix edit found there ("audit for mid-task cache-breakers") is the same finding as a break found here. Cache discipline and preserved-thinking discipline are very nearly the same discipline, so a harness that is already append-only for caching pays nothing extra here. Once the project has an eval, Step 3 runs as a hill-climb whose metric is the drop count - one change per round, measured, kept or reverted - and the `hillclimb` subcommand is the loop to use. + +Two scripts ship with this guide, extracted beside it under `shared/preserved-thinking-migration/`, and are used by Steps 1 and 2. Both are dependency-free Python 3; neither needs credentials except the probe's live modes, and neither prints them. The commands below give their paths relative to this skill's base directory (the line at the top of the prompt); run them with that directory prefixed, from the user's project directory, so that relative capture paths resolve there. A reference file is extracted beside them, `shared/preserved-thinking-migration/causes.md`: the rules for a conversation that switches models, the "Cause -> detection -> fix" table, the keep list, and the failure modes to avoid. It is a separate file so that this guide fits in one Read; Read it when a step below sends you there (Step 1.4 at the latest), not before. + +- `shared/preserved-thinking-migration/prefix_diff.py` - diff consecutive request bodies in the parts the check compares, and name the difference in the API's own vocabulary; `--scan` greps a repository for the usual culprits. +- `shared/preserved-thinking-migration/drop_block_probe.py` - replay a captured conversation with the controls turned on and record what the API dropped and why, per turn and per conversation; `--self-test` is the three-request proof that the check is running (Step 0.5) and exits non-zero when it is not. + +--- + +## Severity tiers - one-off vs. recurring + +Tier every cause before planning the work. **Tiers 0-2 are one-time fixes: apply them once and the harness stops breaking. Tiers 3-4 keep costing** - they recur on every conversation that reaches them, which is why they are the ones to measure before deciding. + +| Tier | Meaning | Causes | What to do | +|---|---|---|---| +| 0 | Fine - not an edit | `cache_control` markers; reordering the `tools` array; server-side compaction and context editing; a retry; a regenerate, rewind, restored checkpoint or branch; the latest turn edited and resubmitted; a model switch (the model check is separate and is not a prefix edit) | Nothing. None of these change the compared prefix - keep them out of the report | +| 1 | Accidental | The system prompt re-rendered with per-request content (a date, a counter, live state); drift from an SDK or domain-model round trip | Remove the edit. Nothing about the product needs it | +| 2 | Fixable | The tool set changed mid-conversation; a same-name tool's description or schema rebuilt; a per-turn reminder injected then stripped; tail state re-rendered every turn | Apply the append-only recipe (Step 3). Adding or withdrawing a tool has an append-only form (`tool_addition` / `tool_removal`, with the entry left in `tools`). A same-name description or schema change has one only under the `inline-tools-2026-09-15` beta (Claude API), where a `tool_addition` carries the new definition (Step 3); without it, keep the first-sent bytes for the life of the conversation and accept the stale definition, or offer the changed text under a new name. Append tail state as new turns rather than rewriting it | +| 3 | Recurring | Tail-kept compaction; background compaction (a second request writes the summary while the session continues, then it is swapped in); rolling truncation; a pinned document rewritten every turn | Measure the drops and decide. Without the `compact-2026-09-04` beta (on-demand compaction) no append-only client-side form exists for these: send `drop_block` from the swap onward, or strip the thinking from the kept turns; the recommended shape is simple compaction, done synchronously. With the beta, keep-tail and background compaction become append-only - see Step 3 | +| 4 | Stop | Prefix surgery - snipping, redacting, pruning old tool results, or removing content after a cache breakpoint to save cost; a missing predecessor; an unrecorded strip-and-retry | No workaround exists. The reasoning after the edit point is lost; the fix is to stop doing it | + +A tier is a property of the cause, not of one conversation: rank by tier first, then by reasoning lost within a tier (Step 3). + +## Step 0: Establish scope, quality bar, and baseline + +**Does your harness change earlier turns, the system prompt, or the tool list? If not, stop** - there is nothing to migrate, and saying so plainly is the finding. + +When a replayed thinking block no longer matches, the default is a **400 error**. Dropping the thinking instead is opt-in, and it is not a fix: it trades a visible failure for the silent loss of that reasoning, and it is not free - dropped blocks aren't billed, but the session's token usage might still increase because Claude can sometimes think more to re-create the dropped thinking ("Failure modes to avoid" in `causes.md`). + +**First, establish three things - from the request and the repository where they answer it, and from the user where they don't.** This workflow is interactive by design: a capture of real request bodies, a test slice, and every live replay need the user's involvement or approval, and "which of these edits is deliberate" is a question only they can answer. State all three at the top of the report (the baseline may read "pending Step 2" at first). + +1. **Scope.** If an official Claude product or SDK (Claude Code, claude.ai, Claude Managed Agents, the Claude Agent SDK) manages the conversation history, there is nothing to migrate - say so and stop. Otherwise: if the request names files or directories, that is the scope. Otherwise it is every place the project builds the three parts of a request the check compares - the `system` prompt, the `tools` array, and the `messages` array - across every path that touches them between two requests of one conversation: the request builder, compaction or truncation, reminder or context injection, media handling, persistence and resume (anything that re-reads the conversation from a store and re-renders it), process restart and deploy, plugin or MCP connection, sub-agent transcripts, and model switching. Note distinct traffic classes (an interactive chat path and a background agent loop are different harnesses even on one key): the profile, the fixes, and every validation later run per class. A capture taken on an older model may carry request shapes Claude Fable 5.1 rejects before any check runs - `thinking.type` `enabled` or `disabled`, a forced `tool_choice` (`any` or `tool`), an assistant prefill as the last message, `temperature`, `top_p` or `top_k` with a thinking configuration - so list them now as things to convert before measuring (Step 2.1 lists what the probe converts; it leaves a prefill alone, and that 400 shows in its "not evaluated" line). **Also establish which platform** the code targets (Claude API, Claude Platform on AWS, Amazon Bedrock, Google Cloud Vertex AI, or Microsoft Foundry) and **which model** it runs: the check applies only to models with preserved thinking. The Preserved thinking page states that beta names are the same on Amazon Bedrock and Google Cloud wherever the beta is available there; for any other platform, read that platform's own documentation and `shared/platform-availability.md` rather than assuming. Finally establish **whether the organization is enforced today**. The rule, as the Preserved thinking page reads on 2026-09-23: on Claude Fable 5.1 and Claude Opus 5.5 the check is enforced by default for accounts created on or after August 31, 2026, 00:00 UTC, with the same definition on the Claude API and on the cloud platforms; a request that sets `prefix_mismatch_behavior` opts in regardless of account age; and later models will enforce the check for all users. Confirm it from the responses production already gets: an enforced account sees `input_transformations` entries (with the header) or 400s whose text says "bound to a different conversation"; an account that is not enforced yet sees no dropped blocks and no 400s until a request sets the field; with the beta header alone, its responses list each block that would fail as a `thinking_mismatch_allowed` entry (Step 2.2). The page's own probe - send an edited history without the beta header: a 400 that names the header means the account is enforced by default, a 200 means it is not (the Message Batches API returns 200 either way). To confirm a 200, resend with the beta header and still no field: the response lists every thinking block after the edit as `thinking_mismatch_allowed`. Step 0.5 proves the enforced path - run it with a test key, never against production traffic. +2. **Quality bar.** Find the project's eval, test suite, or outcome checks for its model calls. The fixes in this workflow are behavior-preserving by construction (they change *how* the history is carried, not what the model is told), but two of them are not: replacing a compaction scheme, and choosing to drop thinking at a boundary. Those need the eval. If none exists, say so prominently in the report and do not stop: the drop count itself is a measurement, every fix that removes an edit is safe to propose, and the minimal eval recipe in Step 3 is the next step for the two that aren't. +3. **Baseline.** Two numbers, both from Step 2's probe on the same test slice: the share of conversations with at least one prefix break, and the turn at which each one first breaks. Record them before any fix. The three-arm protocol in Step 2 also asks for the eval score with the current harness and preserved thinking off (that is the eval's existing number) - write it down now if it exists. + +## Step 0.5: Prove the mechanism is wired + +Before trusting any measurement, run one deliberately broken conversation and confirm the API reports it. The probe's self-test sends three requests - one mint and two replays: it mints a thinking block with a one-turn conversation, replays it honestly as turn two (expect `input_transformations: []`), then replays it with the first user message edited (expect one `{"type": "thinking_dropped", ...}` entry with `reason: "prefix_binding_mismatch"`, and - on the Claude API - the diagnosis header naming `pattern=first_message_rewritten`): + +```text +python3 shared/preserved-thinking-migration/drop_block_probe.py --self-test --model --yes +python3 shared/preserved-thinking-migration/drop_block_probe.py --self-test --model --yes --mode error # expect the 400 instead +``` + +The probe always sends the `thinking-binding-controls-2026-08-01` beta value; add `--beta ` for any other beta your production requests carry (the bare command is enough on the Claude API). The probe is first-party only: it authenticates against `/v1/messages` with `ANTHROPIC_API_KEY` (sent as `x-api-key`) or `ANTHROPIC_AUTH_TOKEN` (sent as a bearer token; when both variables are set only the token is sent, because the API rejects a request carrying both headers) and cannot replay captures taken on Amazon Bedrock or Google Cloud Vertex AI - there, run the account's own client and read `input_transformations` from its responses. + +Three short requests (one mint, two replays), billed at the model's normal rates - state the cost and get the user's approval first, as for every run that exercises the model. If the edited replay comes back with no drop (the probe prints `NOT WIRED` and exits 1), stop: the check is not running for this request (the wrong model id, a platform without the controls, the header missing, the field misspelled, or a gateway or proxy between the harness and the API that drops the `anthropic-beta` header or the `block_binding` field - run the self-test through the same path production uses) and every later number would be meaningless. If the honest replay reports a drop, stop too: something in the probe's path is already editing the history - a proxy, an SDK middleware, a serializer - and that is finding number one. Record both responses in the report. + +## Step 1: Find the edits + +Finding the edits has three sources, in order of evidence: the API's own diagnosis (Step 2), the diff of consecutive request bodies, and the code. Do the diff and the code read in this step; they tell you where to look before spending money on replays, and they are what localizes a break to a line of code after the API has named its shape. + +### 1.1 Capture what the harness actually sends + +Capture the exact request bodies of a few normal conversations - every request, in send order, JSON as it went over the wire, including the assistant turns with their `thinking` blocks and `signature` values exactly as the API returned them. Include one conversation that runs long enough to trigger compaction or truncation if the product has either, one that connects a plugin or tool mid-session if it can, and one that is resumed from storage or survives a process restart. Capture at the HTTP layer where possible (an SDK hook, a logging transport, a proxy) rather than from the application's own message objects - the second kind of capture hides exactly the re-serialization this check catches. If requests pass through a gateway, proxy, or model router on the way to the API, capture them as they leave that layer: a router can rewrite the system prompt, the tools, or the history after your code has built the request. Store one conversation per `.jsonl` file, one request body per line. Ten to thirty conversations are enough; they double as the test slice for Step 2. + +Capture the request body and the `anthropic-beta` header only - never `x-api-key` or `Authorization`; an MCP server's `authorization_token` in the body is sent as the harness sent it (the connector's tools are part of what the check compares), so capture with a test-scoped token and rotate it afterwards. The probe reads nothing else and warns when a capture carries a credential. + +**Handling captures.** A capture is the conversation as the end users had it, and it cannot be redacted without breaking the measurement (the check compares the bytes). Keep captures outside the repository (or ignored by version control), never commit them or an eval set derived from them, run the scripts from a machine that may hold that data, and delete the captures when the work is done. The probe's `--json` output is safe to share - it holds request ids, statuses, entries, headers, digests, token counts and file names, no message content; error text for the conversation check's own 400s is stored as a reconstruction of their fixed form (the block path, the fixed clause, and the first-changed-message diagnostic - never the server line itself), and every other error is reduced to its type and field path because API validation messages can echo request values (the full text still prints on the terminal); `prefix_diff.py`'s output is not, because its attribution lines quote excerpts of the changed content. + +If the application cannot capture bodies yet, adding that capture is itself the first diff of this workflow: it is the measurement channel for everything after it. + +### 1.2 Diff consecutive pairs + +```text +python3 shared/preserved-thinking-migration/prefix_diff.py captures/conversation-0001.jsonl +``` + +For each pair of consecutive requests the script reports `MATCH`, `MISMATCH` with a verdict in the API's vocabulary - `kind=system_changed; pattern=system_rerendered; sections=system; changed_validated=system.0` - and an attribution line that names the site and the first changed character: + +```text +system[0] changed at char 53: "... Be concise." -> "... Be concise. Current time: 2026-09-02T15:04:05Z." +tools: lookup_order description changed at char 23: "...order by id." -> "...order by id. Today is 2026-09-02." +messages[2] (user) content[1] (text) removed: {"text":"Answer in one sentenc... +messages[1..2] removed (assistant, user) +``` + +Two lines matter as much as the verdict. `replayed thinking blocks in the later request: N` - when N is 0 the pair proves nothing about preserved thinking (there was no block to check), which is common for the first pair of every conversation and for harnesses that strip thinking; and the `!` chain line (printed as `CHAIN-BREAK`, counted in the exit status), which is a break, not a warning - it fires when the already-sent turns come back with their thinking blocks changed in a way the API rejects: the kept blocks must be a contiguous window of the original sequence (dropping from the front, from the back, or both is fine), so a block removed from the middle, or a reorder, fails the block after the gap even though the rest of the prefix is untouched. + +The comparison ignores what the API ignores: `cache_control` markers, string content versus a single text block, leading and trailing whitespace of a text block, whitespace-only text blocks, key order, the order of tools in the array (they are compared as a name-keyed set), a `defer_loading` tool that no tool result, tool-search result, or `tool_addition` has named yet, request parameters outside `system` / `tools` / `messages`, everything before the last server-side compaction block (the check restarts there; the diff says when it compared from one), and the thinking blocks themselves. Interior whitespace, `tool_use.input` bytes, tool-result text, image bytes, and everything else count. Treat the script's `pattern` as a guess in the API's words - the API's own header in Step 2 is the authority when the two differ. + +### 1.3 Read the code + +```text +python3 shared/preserved-thinking-migration/prefix_diff.py --scan path/to/repo +``` + +The scan prints `file:line` leads grouped by cause - timestamps and environment reads inside prompt builders, slicing of the messages array, tool lists mutated after session start, the opening message rebuilt from state, tool results trimmed after the fact, reminder tags stripped with a regex, thinking blocks filtered out, round trips through the app's own message model, media caps and URL re-signing. **They are regex leads, not findings**: read each one, and confirm it with the pair diff or the API's response before it goes in the report. The scan is optional; the checklist below is not. An application can always express an edit in words the patterns do not know, so a scan with no leads is not proof of compliance, and a scan with leads is a reading list - the pair diff is the instrument. + +Whatever the scan finds, read these by hand - this checklist is the mandatory part of Step 1.3; they are where the edits hide: + +- **Prompt assembly**: is anything in `system` or in the first user message computed per request - date or time, working directory, account or user line, git status, memory or instruction files re-read from disk, feature flags, model or client version strings, a token or turn counter? +- **Tool declaration**: is `tools` built from live state (connected MCP servers, plugins, permissions, a feature flag) so that it can differ between request 1 and request 2? Are descriptions or schemas rendered with anything dynamic? +- **History management**: any path that shortens, summarizes, reorders, or rewrites messages already sent - sliding windows, keep-last-N, client-side summaries, "micro-compaction" of old tool results, media caps, context-length recovery after an error - and whether any turns are replayed verbatim after a summary (that decides which recipe applies - Step 3's, or the betas section of `shared/preserved-thinking-migration/causes.md`). +- **Injection**: anything appended to a user turn for one request only (reminders, status lines, token counts) and removed or rebuilt on the next. +- **Modes**: does entering a mode (plan, read-only) swap the tool list or the system prompt in place? Withdraw and re-offer tools with `tool_removal` / `tool_addition`, and deliver the mode's instructions as an appended message. +- **Tools listed in the prompt**: are the callable tools named in the system prompt, behind one generic dispatcher tool? Then adding or removing one is a system prompt edit: keep the prompt fixed and announce each change in an appended message ("You can now call X"). +- **Persistence and resume**: does the conversation round-trip through a database or an ORM, and does the replay rebuild messages from those objects rather than from the stored wire JSON? Does a restart, a resume, a deploy, or a new template version re-render the system prompt or the opening message? +- **Thinking handling**: does any code filter `thinking` / `redacted_thinking` blocks (a serializer that skips a block with empty `thinking` text counts: the text is empty by default and the `signature` carries the reasoning), reorder them, store their text truncated or re-wrapped (a modified thinking block is its own 400), or retry a 400 by stripping them without recording that it did? +- **Sessions**: can a thinking block from one conversation be replayed under another (shared session keys, multiplexed users)? + +### 1.4 Name each edit and decide whether it is deliberate + +For every pair-diff verdict and every confirmed lead, record: the cause in the API's words (the `pattern`), the site in the code, which traffic class it is on, and whether the edit is **deliberate** (a compaction the product relies on; a user-invoked reset that starts a new conversation) or **accidental** (a timestamp nobody needed in the system prompt; a plugin landing inline on request 2). Accidental edits are removed outright in Step 3. Deliberate ones are replaced by their append-only form, or - where none exists yet - measured and decided (Step 2's caveats, Step 3's last section). The table under "Cause -> detection -> fix" in `shared/preserved-thinking-migration/causes.md` is the lookup for both: Read that file now if you have not yet, and check every candidate against its "Keep list" before it goes in the report. + +## Step 2: Measure with `drop_block` on a test slice + +The API's response is the only ground truth. The client-side diff can miss what it cannot see (media bytes behind a URL, an edit in a part of the request the capture didn't include) and can flag what the API tolerates; the response cannot. + +### 2.1 The request shape + +Every request in the test slice carries the beta header and sets the behavior **explicitly** - this is what turns the check on for an organization that is not enforced by default, and it is what adds the report to the response: + +```http +POST /v1/messages +anthropic-beta: thinking-binding-controls-2026-08-01 + +{"model": "", "max_tokens": 4096, + "thinking": {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "drop_block"}}, + "system": ..., "tools": [...], + "messages": [ ...the full history with thinking blocks replayed verbatim... ]} +``` + +Rules that save a debugging hour: + +- The field without the header is, today, a 400 ending in `block_binding: Extra inputs are not permitted`. That is a different 400 from the one an enforced account gets when it replays an edited history without the header, whose text says the block is "bound to a different conversation" and ends by naming the beta value the setting requires; you will meet both, and only the second one means the check ran. The header without the field does **not** turn enforcement on for an organization that is not enforced yet: no block is dropped and nothing fails. The API records the check instead and lists each block that would have failed as a `thinking_mismatch_allowed` entry (2.2) - the zero-risk way to find edits in production traffic. To measure what enforcement costs, set the field; it is the per-request opt-in. +- `prefix_mismatch_behavior` takes `"error"` or `"drop_block"`. Write exactly that field name; the probe removes any other key it finds under `block_binding` and says which. +- The object is accepted, under the header, on every model that accepts `thinking`, so one request body works before and after a model switch, provided the thinking configuration is one every model in the route accepts (`enabled` is a 400 on Claude Fable 5.1); on a model without preserved thinking it is a no-op that still reports model-check drops. +- Keep everything else in the request exactly as production sends it. The probe touches only fields outside the compared prefix and prints each change: the header, this field, `max_tokens` (capped, see 2.3), `stream` (off unless asked), `tool_choice` (set to `none`, see 2.3), and the thinking configuration - a request with no `thinking` configuration is given `{"type": "adaptive"}` (on Claude Fable 5.1 that is what a request without the key already runs with), `enabled` is rewritten to `adaptive`, `temperature` becomes 1 and `top_p` / `top_k` are removed; none of those changes the verdict. Thinking disabled is skipped. `system`, `tools` and `messages` go out exactly as captured. + +### 2.2 What the response tells you + +**Detect drops from the request diff.** `prefix_diff.py` over consecutive requests (Step 1.2) is the detector: it reads only what your harness sent, so it works whatever shape the response takes. The surfaces below confirm and explain what it finds. + +Three surfaces, in order of reliability: + +1. **`input_transformations`** (response body, top-level, sibling of `usage`) - the contract. With the header it is present on every response from a thinking-capable model: `[]` when nothing was dropped and nothing failed, otherwise one entry per block, of two types. `{"type": "thinking_dropped", "path": "messages.7.content.0", "reason": "prefix_binding_mismatch"}`: the block was removed. `thinking_mismatch_allowed` (same `path`, `reason` always `prefix_binding_mismatch`): the block failed the prefix check on a request the API does not enforce (an older account with the field unset), so it reached the model unchanged and was billed; every block after the edit gets one, and a request that sets the field never does (the probe always sets the field, so it reads such an entry as the field stripped en route: turn not evaluated, run inconclusive). `path` indexes the `messages` array as you sent it. `reason` is `prefix_binding_mismatch` (your history changed - this workflow's subject) or `model_binding_mismatch` (the conversation switched to a model that cannot read the block - not a bug in your code; see "Switching models mid-conversation" in `shared/preserved-thinking-migration/causes.md`); the probe flags any reason it does not classify as `WARN`. Dropped blocks are not billed, whichever the reason. Ignore entries whose `type` or `reason` you don't recognize; later checks add values. When streaming, the array arrives on the `message` object in `message_start` (and again in the final `message_delta` only after a mid-stream server-side model fallback). Without the header the field is absent and drops are silent. +2. **A diagnosis header, if present.** Some responses that report a drop (or a 400 in `error` mode) also carry a response header named `anthropic-thinking-prefix-mismatch` - **which can be missed on streamed responses** (the probe's `--stream` mode may then print no "why:" line), so read it as a second check and detect from the request diff. **Anthropic has not published this header; it may change or stop without notice. Use it if present; never depend on it.** If it is there, its `pattern` and `changed_validated` fields are hints - the shape of the edit, in the same words `prefix_diff.py` uses, and the first changed path in the request - and the probe prints them on its "why:" line. Treat its absence as "no diagnosis", not "no break": the detail (kind, pattern, changed path) is only given for blocks your own organization created - for other blocks the header carries only the bare fact - and a partner cloud's proxy is not guaranteed to forward it. +3. **The 400 text in `error` mode** - the same diagnosis as one sentence, for code that never sees headers: `messages.7.content.0: Invalid `signature` in `thinking` block. The block is bound to a different conversation. Remove the block, or set `thinking.block_binding.prefix_mismatch_behavior` to "drop_block". Content that preceded this block when it was created is missing from this request, starting at `messages.2`.` It usually ends with one sentence naming the first changed path, as in that example (the sentence varies with the kind of edit and is sometimes absent). The request is rejected before any output; retrying the same body fails the same way (what production code does instead: "Failure modes to avoid" in `causes.md`). + +**The token-counting endpoint runs the same conversation check.** `/v1/messages/count_tokens` applies it to the replayed blocks: in `error` mode it returns the same 400 (with the diagnosis header when that is present); in `drop_block` mode it returns 200 and leaves the dropped block out of the count. A harness that counts tokens before each request meets the 400 there first. The count endpoint costs nothing and samples nothing, so it is the cheapest first-break pass: replay the slice against it in `error` mode (`drop_block_probe.py --count-tokens --mode error`) before a paid `/v1/messages` replay. It returns no `input_transformations`, so it answers "is anything broken, and where", not "how many blocks". + +None of the three says *which line of your code* made the edit. That is what Step 1's diff and scan are for: the header's `pattern` and `changed_validated` tell you where in the request to look; the pair diff tells you what changed there; the scan tells you who wrote it. + +### 2.3 Run the slice + +```text +python3 shared/preserved-thinking-migration/drop_block_probe.py captures/ --dry-run # validates the capture, prints the plan, sends nothing +python3 shared/preserved-thinking-migration/drop_block_probe.py captures/ --yes --count-tokens --mode error # free first pass on the token-counting endpoint: 400s mark the breaks +python3 shared/preserved-thinking-migration/drop_block_probe.py captures/ --yes --json probe.json # drop_block replay; one conversation per file +python3 shared/preserved-thinking-migration/drop_block_probe.py captures/ --yes --mode error # the loud arm (prefix_mismatch_behavior "error"), if you want the 400 text +``` + +Nothing is sent without `--yes`: a bare invocation prints the plan and stops. For CI, a non-zero exit is the signal - the probe exits 0 with no break, 1 when a conversation was inconclusive, 2 when at least one conversation broke (not severity-ordered); the diff script exits 1 on any mismatch or chain break. + +Replaying a request does not run the application's own tools - the capture already holds their results - but server-side tools in the request (web search, code execution, MCP connectors) run again on the API side if the model calls them, and every request is billed; the probe caps `max_tokens` at 16 by default because the verdict is decided before the first output token, which keeps the reply short and the cost to the input side. The probe sets `tool_choice` to `none` on every request that declares tools (`tool_choice` is outside the compared prefix), so no tool - server-side or the harness's own - can be called during a replay; the 16-token cap is for cost, not safety. Do not remove tools from the capture to the same end: the tool set is part of what the check compares and removing one would turn every request into a `tool_set_changed` break. This spends real money: estimate it from the slice (requests × their input size at current rates - fetch the rates, don't quote remembered ones) and get the user's approval once, for the whole measurement budget of this workflow, before the first run. `--max-requests` and `--max-conversations` cap a first run. Use a key from the same organization as the capture - a dedicated key or workspace in that organization is ideal; a capture replayed with another organization's key is not diagnosed, so the run teaches nothing about the harness. + +**What to log per request** (the probe records all of it; production telemetry should too): the conversation id and turn index; the number of thinking blocks replayed in the request; every `input_transformations` entry; the `anthropic-thinking-prefix-mismatch` header if present; the HTTP status and, on a 400, the error text; the request id; and a client-side digest of the three bound parts - a hash of the canonical `system`, of the name-keyed `tools`, and of `messages` up to each replayed thinking block - so that the request where the digest changed can be found without the header. + +**The counting rule.** Count *new* dropped blocks per conversation, not entries per request. A block that fails once fails again on every later request that replays it, so a ten-turn conversation with one edit at turn 3 shows entries on eight responses but has one break. Identify a dropped block by the thinking block itself - resolve each entry's `path` to the block in the body you sent and key it by its `signature` (a `redacted_thinking` block by its `data`) - not by the path: paths shift whenever the history is truncated or compacted, and the same block would be counted again at its new index. The probe does this and keeps the path in the record. Its per-conversation summary gives `first_break_turn`, `distinct_dropped_blocks` (the turns of reasoning the conversation lost) and the patterns seen; the share of conversations with a first break is the baseline from Step 0, and the first-break turn is what you compare against the code: the request where the digest changed is the request after the edit. + +**Suspect the test slice before the harness.** A slice that never replays a thinking block produces a perfect score and proves nothing - the probe warns when a conversation's maximum replayed-thinking count is zero. Check that number first, on every run. The first request of every conversation replays nothing by definition; an adaptive-thinking model may answer a short turn with no thinking block at all (Claude Fable 5.1 at its default effort often does, so a slice of short exchanges can carry no thinking anywhere); a harness that strips thinking before sending has nothing to check. The slice must also be captured on the target model: a block minted by a model without preserved thinking carries no conversation record for the check to verify, so replaying such a capture can never show a prefix break (the probe prints the capture's model ids - check them). Before replaying, count the `thinking` blocks in the captured assistant turns (the probe's `--dry-run` prints the number it will replay). If the production traffic genuinely carries little thinking, say so in the report - that is a finding about exposure, not a pass - and, for a capture made specifically to test the harness, drive the conversations with tasks that need reasoning or capture them with `output_config.effort` raised (`"max"` on Claude Fable 5.1) so that the assistant turns hold thinking; keep everything else as production sends it. A zero on a slice whose conversations replay thinking on most turns is the result you want; a zero on any other slice is a broken measurement. + +### 2.4 The three-arm protocol (validation against the eval) + +When the project has an eval, treat the migration as an A/B/C experiment on one frozen set of inputs. Arm 1 is the harness as it is today, check not enforced: the eval's existing score, no new run. Arm 2 is the same harness with `drop_block` set in the eval runner's configuration (never in production code), with `input_transformations` recorded per response and joined to each conversation's score and token usage; the join answers whether the conversations that lost reasoning scored worse or used more tokens, and by how much. Arm 3 is the harness after Step 3's fixes, again with `drop_block` set; the target is no `prefix_binding_mismatch` entries on the slice (model-check entries are counted separately: "Switching models mid-conversation" in `causes.md`) and a score within noise of Arm 1. Report the three scores, the token usage and the two drop counts side by side, per traffic class. + +### 2.5 Caveats that change what the measurement means + +- **Keep-tail and background compaction have no append-only client-side form without the `compact-2026-09-04` beta.** Summarizing older turns and keeping the newest ones verbatim fails on the kept turns (their thinking was produced with the full history present); compacting off the critical path and swapping the summary in later fails the same way for every turn produced above the swap point. The choices are: server-side compaction or context editing where the product can use them; simple compaction (summary plus the new turn, nothing older replayed); keep the scheme and strip thinking from the retained turns as a deterministic, recorded strip; or keep the scheme and send `drop_block` (equivalent in effect: both lose the same reasoning; the strip is explicit, `drop_block` is one field). Measure the scheme you keep - Arm 2 tells you what it costs - record the decision, and do not assume a compaction rewrite is required. With the beta (on-demand compaction; Step 3), its append-only form is one more scheme to measure the same way, with Arm 2 as its before number. +- **Same-name tool definition changes and tools that re-list.** Under `mid-conversation-tool-changes-2026-07-01`, `tool_addition` is by reference, so it cannot express "the same tool, now with a different description", and a connector whose tool list changes on reconnect has no natural append-only form. Strictly, one exists - declare the revised tool under a new name with `defer_loading: true`, announce it with `tool_addition`, and withdraw the old name with `tool_removal` - but where `inline-tools-2026-09-15` is not available the simpler answer, and the recommendation, is to freeze each tool's text for the life of the conversation and replay it: stale but valid. On the Claude API, send the changed definition by value under that beta instead (Step 3). +- **Partner clouds run the check too** (Amazon Bedrock and Google Cloud Vertex AI). The Preserved thinking page states that beta names are the same on Amazon Bedrock and Google Cloud wherever the beta is available there; each beta's own page lists its platforms, and for any other platform, read that platform's own documentation and `shared/platform-availability.md` before assuming the controls are offered. On every platform the response is the only place the result is visible, and whether the diagnosis header is forwarded on a 200 is up to the platform. Where the controls are not offered the opt-in test does not apply, and recovery from a 400 is the same as Step 3's last resort: strip thinking from the rejected block onward, recorded and persisted. +- **The Message Batches API** drops failing blocks under its unset default instead of failing the item, and sends no header; set `"error"` explicitly if batch items should fail. Whether batch results carry `input_transformations` is unverified - test one batch before relying on it. +- **Organizations that are not enforced yet** get no dropped blocks and no 400 until a request sets the field. With the header alone they get `thinking_mismatch_allowed` entries instead, so sending the header in production and logging those entries finds the edits without changing what the model receives - a record-only pass worth running before Step 2's replay. The field opts a replayed request in, one request at a time, with the rest of production untouched. The same fact is the production hazard: copying the field into production code lifts the exemption on every request that carries it, and every conversation that breaks today starts losing its reasoning (or failing) at once - do not do that before Step 3's fixes have landed. +- **Two checks share the response.** A conversation that moves to a model that cannot read the block gets `model_binding_mismatch` entries; those are expected, unbilled, not a 400 in any test to date, and not a harness bug - but they are reasoning lost, so the probe counts them apart from prefix breaks and the report states them (see "Switching models mid-conversation" in `shared/preserved-thinking-migration/causes.md`). Only `prefix_binding_mismatch` is this workflow's metric. + +## Step 3: Fix one cause per diff, re-measure, keep or revert + +Work the causes in order of **turns of reasoning lost**: for each cause, sum `distinct_dropped_blocks` over the conversations whose first-break diagnosis carried that pattern (the probe's per-conversation summary gives both; a conversation with an early break loses more blocks than one that breaks late) - the cause that breaks every conversation at turn 2 comes before the one that breaks a tenth of them at turn 30. + +**When several causes hit the same request** - the usual case in a harness that grew over time - the unit of work is the *attribution line*, not the pattern. Run `prefix_diff.py` on the first-break pair of each conversation; every line it prints (`system[0] changed at char 78`, `messages[0] (user) content[0] (text) changed`, `messages[2] (user) content[1] (text) removed`, ...) is one edit with one site in the code, and the API reports only the earliest of them (the header names the first failing block's cause; the kind says `multiple`). Rank the lines, fix each as **its own diff**, and measure each fix with the pair diff: the fix is right when *that line* disappears from the first-break pair. Use the probe for the end-to-end re-measure only after the whole set of lines on that pair is gone - the drop count cannot move while any edit on the first-break pair remains, so a probe run after a single correct fix will show the same breaks. Do not read that as "the fix did nothing" and revert it; read the pair diff. + +Each cause that earns a place becomes **its own diff** (one cause per diff, so a revert is clean and the effect attributes). Diffs are **proposed by default** - presented to the user with the attribution line they clear and the measurement that will prove it - and **applied only when the user asks**; then measured: the pair diff first, then - once the first-break pair is clean - re-run the probe on the same slice, compare the share of conversations with a break and the first-break turn against the previous kept state, and, when the eval exists and the change is one of the two behavior-affecting kinds, re-run Arm 3. A diff whose attribution line goes to MATCH is kept; a diff that changes nothing in the pair diff is either a miss (the slice didn't exercise that path - extend the slice, not the claim) or a wrong diagnosis; a diff that clears its line but moves the eval is reverted and recorded. Never keep or revert on one conversation's swing; the slice is the unit. + +Accidental edits are removed. The recipes below are the append-only forms for the deliberate ones, the same shapes Anthropic's own agent products use, since they face every one of these problems (the "Cause -> detection -> fix" table in `shared/preserved-thinking-migration/causes.md` maps each pattern to its recipe here, and its model-switch section covers a harness that routes between models). They share one principle: **the transcript is the source of truth, and everything the model needs to know later is added at the tail, never written into the head.** + +**Freeze the rendered prompt; deliver changes as appended messages.** Render the system prompt once, at conversation start, and store the rendered bytes with the conversation record; every later request of that conversation sends the stored bytes - across process restarts, deploys, template updates, and client versions - not whatever this turn would render. Move every per-session or per-request fact out of `system` and out of the opening message: date and time, the user or account line, working directory, environment, instruction or memory files, feature flags, model and client version. Announce them once in the first user turn (or in a `role: "system"` message appended after it), and afterwards send only deltas, as a new appended message that says what changed ("Primary working directory: /repo/worktrees/x (was /repo)"; "Instruction files were re-read; these differ from their earlier copies: ..."). Mid-conversation `role: "system"` messages carry system-prompt authority and become part of the conversation record later blocks are checked against, so a change delivered this way is as strong as a re-rendered prompt and invalidates nothing; a plain one needs no beta header on Claude Fable 5.1 (only `clear_at` and the tool-change blocks below do). The only times the prompt is rendered again are deliberate boundaries - a new conversation, a user-invoked reset, the request after a full compaction - and a deliberate boundary is declared in the logs so a diff at that point is not mistaken for a bug. + +**Declare the initial tool set at the start; never edit an entry; surface late tools and withdrawals by reference.** For a small fixed set, build the full `tools` array before the first request, with `defer_loading: true` on tools that may not be ready; store the array as sent and replay it. For a large catalogue the model will mostly never use, leave not-yet-enabled tools out of `tools` and, on the turn one becomes available - or a tool connects later (an MCP server, a plugin, a permission granted mid-session) - append it to `tools` with `defer_loading: true` (a deferred tool nothing has referenced yet is outside the compared prefix, so appending it is safe; the Preserved thinking page documents this form) and announce it with a `tool_addition` block in an appended `role: "system"` message (beta `mid-conversation-tool-changes-2026-07-01`; it must follow a user message, such as the `tool_result` turn; right after a paused assistant turn that ends in a server-tool result a text-only system message is accepted but a tool change is a 400, so resume that turn first); never append a regular tool. A tool whose provider goes away is never removed from the `tools` array: to withdraw it, announce a `tool_removal` block in an appended `role: "system"` message (same beta) and leave the definition in place, returning an ordinary "not available" error if the model still calls it. Freeze each tool's description and schema text for the life of the conversation - a refreshed token, a date, a live listing, or a version string inside a description is a re-render. + +**With the tool search tool in the request, keep a tool out of `tools` until it is available.** Search can find and call a deferred tool (the Tool search page), so append the tool with `defer_loading: true` and a `tool_addition` block on the turn it becomes available. + +**Keep per-turn reminders in the history.** A reminder that should apply to one turn goes out as a turn-scoped system message - `{"role": "system", "clear_at": "next_user_message", "content": "..."}` appended after the `tool_result` message it applies to (beta `mid-conversation-system-clear-at-2026-08-21`; a `role: "system"` message must follow a user message, or an assistant message that ends in a server-tool result - anywhere else it is a 400, not a binding failure) - and **every earlier copy stays where it is**, byte for byte: a cleared message renders nothing, costs no input tokens, and is still part of the conversation record the thinking is checked against. Three details from the platform page: a turn-scoped message carries `text` content only; it takes no `cache_control` marker, so put the cache breakpoint on the user turn before it; and a user message that holds only `tool_result` blocks counts as the "next user message" that clears it. Without that beta, append the reminder as a `text` block after the `tool_result` blocks in the same user message and leave earlier copies in place; the model acts on the newest one. Rewording, rebuilding from current state, or deleting a copy already sent is an edit like any other. The same rule covers any mid-conversation `role: "system"` message: persist it with the transcript and replay it, including in sub-agent transcripts. + +**Size old messages before they are sent, never after.** Tool results, documents, and images are sized at ingestion - truncate the output, downscale the image, count the tokens - before the first request that carries them, and never touched again. Later trimming goes through server-side context editing (tool-result clearing, thinking clearing) or server-side compaction, which do not count as edits. For images specifically, the options in order of preference: (a) **downscale at ingestion** so that keeping every image in the history is affordable - the only option that loses nothing; (b) **return generated or fetched images inside the `tool_result` of the tool that produced them**, because server-side context editing can clear old tool results without a client edit, whereas there is no server-side way to prune an image that sits in a plain user turn; (c) if a client-side cap over user-turn images is unavoidable, make what it strips a deterministic function of the append-only history, stripping down to the cap *minus a headroom* so that a crossing happens once every N images rather than on every request - and say plainly that each crossing is still an edit that costs the reasoning after it; (d) the Files API (`file_id`) is for content whose *bytes* would otherwise drift between turns (a re-fetched URL, a re-encoded upload) - it does not reduce the tokens an image costs, so it is not a cap. + +**Store and echo wire bytes; never rebuild history from a domain model.** Persist the `messages` array exactly as sent and the assistant content exactly as received - in particular `tool_use.input` as the API produced it (keep a normalized copy for your own execution if you need one, but echo the original) and text blocks untrimmed. Replay those bytes. A round trip through ORM objects, dataclasses, or a "normalize" pass is where interior whitespace, number formatting, key coercion, and string-versus-block shapes drift; the check tolerates leading and trailing whitespace and the string-versus-single-text-block shape, and nothing else. + +**Compact in a shape the check honours.** Prefer server-side compaction (its `instructions` parameter takes your own summarization prompt; on Claude 5.1 and later models threshold compaction with custom `instructions` summarizes without the earlier thinking, while on-demand compaction's summarizer always reads it) or context editing - the checked prefix restarts at the compaction block. Client-side, the recommended shape is simple compaction: when the conversation grows too long, summarize it into one message and start the next request with that summary and the new user turn, replaying nothing older - no earlier turns, no earlier thinking. The summary is a plain user message, so there is no thinking left to fail the check. Threshold compaction writes its summary with the model named in the request; an on-demand compaction request can name another supported model, but kept turns' thinking stays valid only if every compaction request since it was produced ran on a model with preserved thinking. Never compact in the middle of a tool round (an assistant turn whose `tool_use` is still waiting on its `tool_result` goes back with its thinking intact). "Summarize the last N turns and drop the rest" is simple compaction too, as long as nothing older than the summary is replayed verbatim. If the product keeps a verbatim tail, strip the `thinking` and `redacted_thinking` blocks from the retained turns - text and tool calls stay - and make the strip a deterministic function of the stored compacted transcript (every assistant turn older than the compaction boundary goes out without thinking), so the same bytes go out on every later request and after a restart; a harness that re-derives the transcript from a store that still holds the thinking needs a recorded marker to get the same result. A rolling keep-last-N scheme pays this at every compaction (Step 2.5 compares the strip with `drop_block`). Name compaction in the logs as the one sanctioned boundary where the prefix legitimately changes. + +**Where the product can use one of two newer betas, its append-only form is one more scheme to measure**: `compact-2026-09-04` (on-demand compaction; not on Amazon Bedrock - its page's Compatibility list names the models and platforms) for background and keep-tail compaction, `inline-tools-2026-09-15` (Claude API) for tools learned mid-session, same-name tool changes and connectors that re-list. Recipes and rules: "Append-only forms under newer betas" in `shared/preserved-thinking-migration/causes.md`; check the platform pages for availability first. + +**Remove thinking only as a contiguous run, and record every strip.** The check accepts any contiguous window of the original thinking blocks - a run dropped from the front (the oldest first; after a compaction block, the oldest after it), a run dropped from the back, or both - and nothing else: a block removed from the middle, or a reorder, fails the block after the gap and every one after that. Two shapes follow from it. When a 400 forces a strip-and-retry, strip from the rejected block onward (a trailing run) and persist that the strip happened, so later requests send the same stripped history, not the refused blocks. And never thin the middle. Once a block is removed, leave it out: putting it back invalidates the thinking produced while it was gone. + +**Keep conversations apart.** A block from another conversation fails as `kind=unrelated` / `pattern=foreign_prefix`. That is a session-keying bug, not a prefix edit; fix the key. Reasoning cannot be carried into a new conversation: a branch that replays the history unchanged up to the fork keeps it; anything else starts from a summary. + +**When no natural append-only form exists for a shape** - keep-tail or background compaction without the `compact-2026-09-04` beta, a same-name tool definition change or a connector that re-lists without the `inline-tools-2026-09-15` beta (the rename-and-withdraw form above exists but is rarely worth it) - the diff is the decision, not a code change: measure the cost with Arm 2, choose `"error"` (a mismatch can only mean a bug, fail loudly) or `"drop_block"` (degrade, keep serving) for production, set it explicitly under the header, and log `input_transformations` or the 400s either way. Record the cause as *measured and decided* in the report, with the setting chosen and what it costs, so the next person doesn't re-litigate it. + +**Minimal eval recipe**, for the two behavior-affecting fixes when no eval exists: a frozen set of 20-30 real conversations from the capture; a per-conversation judgment that is cheapest for the workload (golden outputs to diff against, a short rubric, or an automated checker); a runner that replays one configuration and reports pass rate beside the probe's break share. Three arms, approved as one budget. + +Finally, **the production setting is its own diff, and the last one**. Under the header, choose `"error"` or `"drop_block"` and set it explicitly; do not leave the field unset, because the defaults differ by surface and by account age, and an unset field on an account that is not yet enforced means the check is recorded, not applied. Apply that diff only with the user's explicit approval, only after the slice shows zero new drops for every cause that was fixed, and never `"error"` in production while a measured-but-unfixed cause remains - `"error"` belongs in CI, where one multi-turn capture per traffic class replays with it so that a new prefix edit fails the build. In production, alert on the first `prefix_binding_mismatch` entry per conversation, not on the count. + +Before the report, check the run against "Failure modes to avoid" in `shared/preserved-thinking-migration/causes.md`. + +## Step 4: Deliverables + +1. **The break profile and plan**: the Step 0 assumptions (scope, traffic classes, platform and model, enforcement status, quality bar), the Step 2 baseline (share of conversations with a break, first-break turn distribution, replayed-thinking coverage of the slice), and the causes found - each with its `pattern`, its site in the code, whether it is deliberate, and the turns of reasoning it costs - ranked by reasoning lost. Causes with no append-only form are listed as *measured and decided*, with the setting chosen. +2. **The changes**: one diff per cause, in the order proposed (and applied, when the user asked for that), each tagged *applied and measured* (break share and first-break turn before and after; Arm 3 score where the eval exists), *proposed* (with the expected effect), or *needs an eval* (the two behavior-affecting kinds without one). Plus the production setting chosen (`"error"` or `"drop_block"`) as its own, last, explicitly approved diff - applied only once the slice is clean for the fixed causes, and never `"error"` while an unfixed cause remains - where it is set, the CI replay, and the alert. "No changes recommended" - the slice replayed thinking on most turns and nothing was dropped - is a successful outcome; say it plainly. + +**Report skeleton** (section order and required columns - keep the rest flexible): + +- **Scope / traffic classes / platform and model / enforcement status / quality bar** (Step 0) +- **Baseline** - conversations with a break (share), first-break turn (median and range), replayed-thinking coverage of the slice (Step 2) +- **Causes found** - table columns: `Pattern | Site (file:line) | Traffic class | Deliberate? | Conversations affected | First-break turn | Reasoning lost (turns)` - captioned "ranked by reasoning lost" +- **Changes** - one diff per cause, numbered in application order, each tagged *applied and measured* / *proposed* / *needs an eval*, with before-and-after break share +- **Causes measured and decided** - shapes with no append-only form: what was measured, the cost of keeping it, the setting chosen +- **Reasoning lost by routing** - conversations routed to a model that cannot read their thinking (`model_binding_mismatch`), the turns affected, and the routing decision recommended +- **Production setting, CI replay, alert** - what was set where +- **Next step / approvals needed** - measurement budget, eval prerequisite, or "no changes recommended" + +## Sources and live references + +Facts about the check, the request fields, and the response surfaces above are snapshots; the pages win where they differ. Fetch them when the user needs the full write-ups or current availability: + +- **Preserved thinking** - the platform guide (`https://platform.claude.com/docs/en/build-with-claude/preserved-thinking`): the check, `prefix_mismatch_behavior`, `input_transformations`, enforcement by account age, the beta names on Amazon Bedrock and Google Cloud, and an FAQ that covers model switches, non-Claude turns, and resuming after a restart. Anchors: `#tool-changes` (Step 3's tool recipe), `#custom-compaction-on-the-client` and `#keep-tail-compaction` (Tier 3). +- **The Claude Fable 5.1 migration guide** (`https://platform.claude.com/docs/en/models/fable-5-1/migration-guide`, breaking-changes item 3, "Editing earlier turns invalidates thinking blocks"): the three-step check and the append-only form of each edit; the same material is in `shared/model-migration.md` § "Breaking change 3", which this guide links to. +- **Extended thinking** (`https://platform.claude.com/docs/en/build-with-claude/thinking#preserved-for-model`): the section "Only for the model that produced it, or a newer one" - which models read which models' thinking blocks. +- **Tool search** (`https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool`): `defer_loading` and search. +- **Mid-conversation system messages and tool changes** (`https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages`): `role: "system"` messages, `clear_at`, `tool_addition` / `tool_removal`, and defining a tool inside a message (beta `inline-tools-2026-09-15`). +- **Compaction and context editing** - the platform's server-side compaction and context-management docs (`https://platform.claude.com/docs/en/build-with-claude/compaction`, with its on-demand compaction and "Keep thinking blocks valid" pages): the shapes the check honours, including on-demand compaction (beta `compact-2026-09-04`). +- Per-platform availability of the opt-in controls: `shared/platform-availability.md`, and the Preserved thinking page. diff --git a/skills/claude-api/shared/preserved-thinking-migration/causes.md b/skills/claude-api/shared/preserved-thinking-migration/causes.md new file mode 100644 index 000000000..4793a7272 --- /dev/null +++ b/skills/claude-api/shared/preserved-thinking-migration/causes.md @@ -0,0 +1,97 @@ +# Preserved Thinking - Causes, Fixes, and the Keep List + +> **Reference for `shared/preserved-thinking-migration.md`** (the `preserved-thinking-migration` workflow). This file is not a workflow: it holds four lookups that the guide uses - the rules for a conversation that switches models, the "Cause -> detection -> fix" table, the keep list, and the failure modes to avoid - kept apart from the guide so that the guide fits in one Read. Read this file when the guide sends you here (Step 1.4, Step 2, or Step 3), and use the step numbers below as references into that guide. + +## Switching models mid-conversation + +A harness that routes one conversation to more than one model - a cheaper model for easy turns, a fallback when the primary is unavailable, an upgrade from the model the conversation started on - meets a second check that shares the response with the prefix check. The rules below are what the API does today, as observed on the Claude API with `claude-opus-5`, `claude-opus-5-5`, `claude-fable-5`, and `claude-fable-5-1` in both `prefix_mismatch_behavior` modes; the Preserved thinking page (under "Sources and live references" in `shared/preserved-thinking-migration.md`) states the same rules, and the Extended thinking page (section "Only for the model that produced it, or a newer one") lists which models read which blocks. + +**Is the model part of what the signature records?** Yes. A `thinking` block's `signature` records the model that produced it alongside the conversation record and the previous thinking block. When the block is replayed, the API first asks whether the model now reading it reads blocks from the model that produced it (the page's rule: the model that produced it, or a newer one) - the *model check* - and only then whether the conversation before the block is unchanged - the *prefix check*. The `model` field of the request is not part of the conversation record, so changing it is not an edit: **a model switch by itself never fails the prefix check.** + +**Does a switch break the check?** As of 2026-09-03 the API behaves as follows: a model switch does not fail a request - upgrade, downgrade, a round trip, and a downgrade combined with an edit all return 200, in `"error"` mode and in `"drop_block"` mode. `"error"` makes *edits* loud; it does not make a *model switch* loud, because the model check has no error setting: a block the current model cannot read is dropped, not rejected. Read the Preserved thinking page for the current rule. What differs by direction is whether the earlier reasoning is used: + +- **Upgrade (an older model's blocks replayed to Claude Fable 5.1): nothing to do.** Claude Fable 5.1 reads thinking produced by Claude Opus 5 and Claude Fable 5, among other earlier models (the Extended thinking page has the full list). The blocks are kept, fed to the model, counted in input tokens, and `input_transformations` is `[]`. An older model's block carries no conversation record for Claude Fable 5.1 to compare, so an edit made before such a block does not invalidate it; a conversation migrated from Claude Opus 5 can only break on the turns Claude Fable 5.1 produces from then on. +- **Downgrade (Claude Fable 5.1's blocks replayed to Claude Opus 5 or Claude Fable 5): the reasoning is lost for that request, not the request.** The older model cannot read them, so the API leaves them out of that call, reports each one as `{"type": "thinking_dropped", "reason": "model_binding_mismatch", "path": ...}` when the beta header is on, and does not bill the dropped tokens. As of 2026-09-03 this is not a 400 in either mode, and there is no diagnosis header (that header belongs to the prefix check). Your `messages` array is never edited: the blocks stay in your history. +- **Round trip (Claude Fable 5.1, then an older model, then Claude Fable 5.1 again): nothing is lost, provided the client keeps the history intact.** Back on Claude Fable 5.1 every block is read again - the ones from before the switch, the older model's own thinking block, and the ones minted after the return. Observed through four turns: a Claude Fable 5.1 block minted after the older model's turn is valid, and the chain of Claude Fable 5.1 blocks does not care that a turn between them came from another model. The same four-turn history sent back to the older model drops exactly the Claude Fable 5.1 blocks and keeps its own. +- **Claude Opus 5.5 runs the same prefix check, and sits beside Claude Fable 5.1, not under it.** Everything in this file that names Claude Fable 5.1 as the model that runs the prefix check holds for Claude Opus 5.5 too (observed 2026-09-23: an edited history is a 400 in `"error"` mode, a `prefix_binding_mismatch` drop in `"drop_block"` mode, and a `thinking_mismatch_allowed` entry with the field unset). What differs is who reads whose blocks, per the Preserved thinking page: Claude Opus 5.5 reads thinking from Claude Opus 5 and earlier Opus, Sonnet and Haiku models, but not from Claude Fable or Claude Mythos models; on the Claude API, Claude Fable 5.1 and Claude Mythos 5.1 read Claude Opus 5.5's blocks, and no other model does. So Claude Opus 5 to Claude Opus 5.5, and Claude Opus 5.5 up to Claude Fable 5.1 on the Claude API, keep the reasoning (`[]`); Claude Fable 5.1 to Claude Opus 5.5, or Claude Opus 5.5 to any other model, is a downgrade in the sense above - `model_binding_mismatch` drops, 200 in both modes. +- **A downgrade can hide an edit for one request.** The model check runs first, so a block the older model cannot read is never prefix-judged: a request that switches down *and* edits earlier content reports only `model_binding_mismatch`, even in `"error"` mode. The edit is still there; it surfaces on the next Claude Fable 5.1 turn as an ordinary prefix break (a 400 in `"error"` mode, `prefix_binding_mismatch` in `"drop_block"` mode). Scan and measure every turn of a mixed-model conversation, not only the turn where the switch happened. +- **Turns from a non-Claude model** do not invalidate earlier thinking, provided they are appended after the existing history as ordinary assistant messages (`text` and `tool_use` content, no `thinking` blocks) and nothing earlier changes. Claude Fable 5.1 thinking minted after such a turn stays valid. +- **A twin model that reads the blocks but runs no conversation check** (the Extended thinking page lists which models read which): its turns report `[]` even on an edited history, so an edit made on such a turn surfaces only on the next Claude Fable 5.1 turn - the same one-request blind spot as a downgrade, with `[]` instead of a model drop to notice it by. + +**Does changing a tool description break the check?** Yes. Tools are compared as their full definitions - name, description, and `input_schema` - so changing even one tool's description invalidates every thinking block minted before the change, reported as `prefix_binding_mismatch` with `pattern=tool_schema_changed` (the API reports a description edit and a schema edit with the same word). Adding or removing a plain tool has the same effect, reported as `tool_set_changed`. Reordering tools is fine, and a `defer_loading: true` tool is outside the comparison until something references it. The fix is the `tool_schema_changed` and `tool_set_changed` rows: freeze each tool's text for the life of the conversation, and add or withdraw tools by reference - or, under `inline-tools-2026-09-15` (Claude API), append a `tool_addition` carrying the new definition ("Append-only forms under newer betas", below). + +**Cause -> detection -> fix.** A model switch produces findings in two shapes, and only the second is a harness bug: + +1. **The conversation is routed to a model that cannot read its thinking.** *Detection*: `model_binding_mismatch` entries on the older model's turns; the probe prints them as `model_drops=N` per turn and `model_drop_turns` per conversation, apart from the prefix-break count, and `prefix_diff.py` prints `model_switch=A->B` on the pair where the `model` field changed (never as a MISMATCH). *Fix*: a routing decision, not a history edit. Send the same `messages` - thinking blocks included - to every model, and leave the beta header and `block_binding` field in place across the switch (the object is accepted on every model that accepts `thinking`). If the product needs the reasoning on those turns, keep the conversation on the model that produced it and switch models at conversation boundaries; if it needs the older model on those turns, accept that they run without the newer reasoning. Report it as *reasoning lost by routing* - conversations and turns affected - beside the prefix breaks, so the owner can decide. +2. **The switch triggers an edit.** Three kinds, each an ordinary prefix break with the switch as its trigger: (a) the harness strips thinking blocks on a switch, or rebuilds the history from what each model was shown - the diff shows the blocks removed (`predecessor_missing` if from the middle), and the reasoning is gone for good when the conversation returns; *fix*: stop stripping, the API already leaves out what the current model cannot read. (b) A re-rendered system prompt or tool list that *persists* past the switch - a fallback banner that stays in `system` from then on, a prompt that depends on which model answered last - reaches Claude Fable 5.1 together with blocks minted under the old text: `system_rerendered`, `tool_set_changed`, or `tool_schema_changed` on the first Claude Fable 5.1 turn after it (on a downgrade the older model's turn in between reports only the model drop). A prompt or tool set that is a pure function of the model being called is different: every Claude Fable 5.1 request then carries the same text the blocks were minted under, so the check finds nothing (verified: the original prompt restored on the return to Claude Fable 5.1 gave 200 and `[]` with the block fed, in both modes), even though the pair diff flags the switch pairs and the older model's turns still lose the reasoning through the model check. *Fix* for the persisting case: the rows for those patterns - keep each model's prompt and tool text stable across that model's own turns, and deliver anything that must change mid-conversation as an appended `role: "system"` message. (c) A request shape the target model does not accept - a `thinking` configuration it rejects (a 400 from request validation whose text names the field, not a binding failure), or mid-conversation `role: "system"` messages, and with them `tool_addition`, `tool_removal` and `clear_at`, on a model the Mid-conversation system messages page does not list (it names Claude Sonnet 5 as unsupported: "Use the top-level system field there instead"); *fix*: one request body that every model in the route accepts. + +## Cause -> detection -> fix + +The `pattern` column is the word the API's diagnosis header and the diff script both use. "Diff shows" is the attribution line from `prefix_diff.py`; "scan lead" is the heuristic `--scan` reports; the fix is the append-only form from Step 3 of `shared/preserved-thinking-migration.md`. One caution on reading the pattern word: for a summary-plus-tail compaction the header's word depends on how much was removed - the same code path reports `tail_kept`, `compaction_summary`, or `unknown` with `kind=blocks_replaced` on different conversations - so identify that cause by the kind (`blocks_removed` or `blocks_replaced`) together with the diff's attribution lines, not by one pattern word. + +| Tier | `pattern` (and `kind`) | What the harness did | Diff shows | Scan lead | Fix | +|---|---|---|---|---|---| +| 1 | `system_rerendered` (`system_changed`) | The system prompt was rebuilt with per-request content: time, cwd, account line, memory or instruction files, flags, version strings | `system[i] changed at char N` | time and environment reads near prompt builders; templates rendered per request | Render once, store the bytes with the conversation, replay; per-session facts go into the first turn; changes go out as appended `role: "system"` messages | +| 2 | `tool_set_changed` (`tools_changed`) | A tool was added or removed after the first request: a plugin or MCP server connected late, a provider disconnected, a permission changed | `tools: X added` / `removed` | `tools` mutated after session start; a tool listing fetched per request | Declare the full set at start; append a late tool with `defer_loading: true` (safe while unreferenced) and announce it with `tool_addition` in an appended system message - never append a regular tool; never remove one from the array - withdraw it with a `tool_removal` block and leave the definition in place, returning an ordinary "not available" error if the model still calls it | +| 2 | `tool_schema_changed` (`tools_changed`) | Same tool names, different description or schema text: a date, a refreshed token, a live listing, a version inside a description | `tools: X description changed at char N` | descriptions or schemas built from templates or state | Freeze each tool's text for the conversation; store and replay the definitions as sent. Without the `inline-tools-2026-09-15` beta no append-only form expresses a same-name change - a new name is the only way to offer changed text; under it, append a `tool_addition` carrying the new definition instead (the betas section below) | +| 1-2 | `system_and_tools_changed` (`multiple`) | Both re-rendered, messages untouched: a connector landing on request 2, or a restart or resume re-deriving both | both of the above | startup, resume, reconnect paths | Replay the stored prompt and tool text across restarts, and across model switches (a switch is not a boundary: a re-render that persists into later requests on the model that minted the blocks is this break, with the switch as its trigger; a prompt that is a pure function of the model called is stable on each model's own turns and is not); the only declared boundaries are a new conversation, a user-invoked reset, and the request after a full compaction | +| 1 | `first_message_rewritten` (`blocks_modified` / `blocks_removed`, often with `system` in `sections`) | The opening user message carried context rebuilt from live state: environment, instructions, a session date | `messages[0] (user) content[j] changed at char N` | `messages[0]` assigned after creation; a context header rendered per request | Announce context once and freeze it; send later changes as an appended message describing the delta | +| 3 | `rolling_truncation` (`blocks_removed`) | The oldest turns were dropped whole - a sliding window | `messages[0..k] removed` | `messages[-N:]`, keep-last, window size | Without the `compact-2026-09-04` beta no client-side form keeps the thinking (with it, on-demand compaction does, but the window must summarize rather than only drop - the betas section below). The choices: server-side compaction or context editing; simple compaction (summary plus new turn, nothing older); or keep the window and strip the retained turns' thinking as a deterministic, recorded strip, or send `drop_block` (equivalent in effect) - measured | +| 3 | `tail_kept` (`blocks_removed`) | A run of older turns removed (or replaced by a summary the record can't see) with the first message kept and the newest turns verbatim - keep-tail compaction or keep-first truncation | `messages[i..j] removed`, `messages[0]` intact | `summarize(messages[:-k])` plus `messages[-k:]` | Same as above; the retained turns' thinking cannot verify without the `compact-2026-09-04` beta (it does behind an on-demand compaction block, the betas section below) - send `drop_block` from the compaction onward or strip that thinking as a recorded decision, never mid tool-round; measure, decide, and record the decision | +| 3 | `compaction_summary` (`blocks_replaced`) | Older turns replaced in place by a shorter summary, the tail intact | `messages[i..j] replaced by 1 message(s)` | same | Same; or move the summary to simple compaction (replay nothing older than the summary); or, under `compact-2026-09-04`, on-demand compaction (the betas section below) | +| 4 | `tool_results_rewritten` (`blocks_modified`) | Old `tool_result` content trimmed or cleared after it was sent | `messages[i] (user) content[j] (tool_result) changed at char N` | tool-result truncation applied to earlier turns | Bound outputs before the first send; later clearing through server-side context editing (`clear_tool_uses_20250919`, beta `context-management-2025-06-27`); a client-side prune only at a declared boundary, as a pure function of the growing history | +| 1 | `tool_use_rewritten` (`blocks_modified`) | Old `tool_use.input` re-encoded or normalized on replay | `content[j] (tool_use) changed` | input normalizers, `to_dict` on tool calls | Echo `tool_use.input` exactly as received; normalize a copy for execution only | +| 1 | `reserialized` (`blocks_modified`) | Many blocks differ slightly across types: a lossy round trip through the app's own message model (interior whitespace, number formatting, coerced keys, trimmed text) | many `changed at char N` lines across messages | `from_dict`/`to_dict`, JSON re-encoding of history, `.strip()` on content | Persist and replay the wire JSON; never rebuild messages from domain objects | +| 2 | `reminder_stripped` / `history_block_stripped` / `block_inserted` (`blocks_removed` / `blocks_inserted`) | A per-turn text block injected into a user turn and removed on the next request (or added after the fact) | `messages[i] (user) content[j] (text) removed` / `inserted` | regex strips of reminder tags; inject-then-strip helpers | Turn-scoped system message (`clear_at: "next_user_message"`) appended after the tool results, every earlier copy left in place; without the beta, a text block after the `tool_result` blocks, left in place | +| 2 | `system_block_rerendered` / `system_blocks_stripped` | A mid-conversation `role: "system"` message re-rendered in place, or several dropped (a sub-agent transcript replayed without them) | `messages[i] (system) changed` / `removed` | transcript stores that don't keep system messages | Persist them with the transcript and replay verbatim | +| 3 | `media_stripped` (`blocks_removed` / `blocks_modified`) | Images or documents in earlier turns dropped, downsized, or replaced by a placeholder - a client media cap | `content[j] (image) removed` | image caps, resizing of stored turns | Downscale at ingestion; return images inside the producing tool's `tool_result` so server-side context editing can clear them; if a user-turn cap is unavoidable, strip deterministically to cap-minus-headroom and accept that each crossing is an edit; `file_id` only for bytes that would drift | +| 1 | `image_url_resigned` (pattern) / `media_content_changed` (kind) | A URL-sourced image or document whose block changed, or whose bytes differ from the first fetch | `content[j] (image) changed` | URL re-signing, re-uploads | The check compares the bytes, not the URL string: a rotated URL to the same bytes is fine; for content referenced across turns use a `file_id` or base64 | +| 4 | `predecessor_missing` / `predecessor_reordered` (kinds of the chain check; the header reads `kind=predecessor_missing; pattern=not_applicable`) | A thinking block removed from the middle, or re-ordered, with the rest of the prefix intact | `! messages[i] re-sent with a different set of thinking blocks` | filters on `type == "thinking"`; serializers that drop empty fields or unknown block types (a `thinking` block with empty text is still a block); a hand-rolled stream parser that loses the `signature_delta`; strip-and-retry without a record | Keep the replayed thinking blocks a contiguous window of the original (drop from the front or the back, never the middle); make any forced strip deterministic and recorded so it replays identically | +| 3 | `unknown` with kind `blocks_removed` or `blocks_replaced` | A shortening of the history that the API does not name more specifically, or several edits at once | `messages[i..j] removed` / `replaced by 1 message(s)` | the same leads as the truncation and compaction rows | Read the attribution lines; the fix is the truncation or compaction one above | +| - | `foreign_prefix` (`unrelated`) | A block from another conversation replayed (on a long conversation; a short one reports an ordinary `multiple` / `system_and_tools_changed`) | no pair diff (it is a different conversation) | session keys, multiplexed stores | Fix the session keying | +| - | (no pattern) | A drop or 400 on a pair where nothing you sent differs - the diff shows no change and the digests match | no diff | - | Not a harness bug; report the request id to Anthropic | +| 4 | `thinking_modified` (a separate 400, "cannot be modified") | A replayed thinking block's text differs from what the API returned - truncated, summarized, re-wrapped | `! the thinking text of the block in messages[i] differs` | stores that trim or reformat thinking text | Store and replay thinking blocks byte for byte | +| 0 | `model_binding_mismatch` (model check; no `pattern`, no header) | The conversation was routed to a model that cannot read its earlier thinking - a downgrade, a cheaper-model route, a fallback | `model_switch=A->B` on the pair, verdict unchanged; the probe's `model_drops` | model ids chosen per turn; fallback or router code | Not a history edit: send the same `messages` to every model, keep the field and header in place, and decide the routing - pin the conversation to the producing model, or accept that the older model's turns run without the newer reasoning; report it as reasoning lost by routing | +| 1-2 | `system_rerendered` / `tool_set_changed` / `tool_schema_changed` / `predecessor_missing`, triggered by a switch | A re-rendered prompt or tool list that persists past the switch (a fallback banner, a prompt keyed to the last responder), or thinking stripped on the switch; on a downgrade the edit is reported only on the next Claude Fable 5.1 turn. A prompt that is a pure function of the model called is not this break | the row for that pattern, on the pair after the switch (the diff also flags a per-model prompt that the API accepts - confirm with the probe) | prompts or tool lists that change with the route; thinking filtered on a switch | The row for that pattern: each model's prompt and tool text stable across its own turns, changes as an appended `role: "system"` message, and never strip thinking on a switch | + +## Append-only forms under newer betas + +Two newer betas add an append-only form for shapes the table above marks as having none without them. On-demand compaction (`compact-2026-09-04`) is on the Claude API, Claude Platform on AWS, Google Cloud and Microsoft Foundry, not on Amazon Bedrock; the Compatibility list on its page names the models and platforms, and the Models API reports each model's `capabilities.compaction` with the beta header. Defining a tool inside a message (`inline-tools-2026-09-15`) is Claude API only; by-reference tool changes under the older `mid-conversation-tool-changes-2026-07-01` header also work on Amazon Bedrock and Google Cloud. Where a beta is not available, treat those shapes as the table says: measure and decide, freeze and replay. + +**Background and keep-tail compaction, the append-only way: on-demand compaction (beta `compact-2026-09-04`, `"compaction": {"type": "summarize"}`; the on-demand compaction page, `https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand`).** Send a compaction request that carries exactly the `messages` of a request you already sent, on the conversation's model and under its `system`, `tools` and `thinking` settings, with the `compaction` field and a `max_tokens` large enough for a summary; send the beta header on it and on every request that carries the block. Exactly those messages, because the kept turns must directly follow the summarized messages and the first kept message must not be one the API would merge into the last summarized one (the same role, or a `role: "system"` message). A compaction request whose last assistant turn is still waiting on a tool result is rejected, so send the results first. Leave out `output_config.format`, `stop_sequences` and a `tool_choice` of type `any` or `tool` (the API rejects a compaction request that carries them), and never send `output_config.task_budget.remaining` on the compaction request or on any request that carries the block (a 400). The response holds one signed `compaction` block and nothing else (`stop_reason: "compaction"`). When no summary could be written there is no block: the response is still a 200 with empty `content`, and `stop_reason` is the summarization call's own - `max_tokens` (cut off), `model_context_window_exceeded` (no room for the summarization prompt), `refusal`, `tool_use`, or `end_turn` (no text) - so give `max_tokens` a few thousand tokens at least, resend with more room or fewer messages as the reason suggests, or continue without one. Custom `instructions` replace the server's summarization prompt whole (a blank value counts as absent), so ask for text only and no tool call yourself; the summarizer reads the whole conversation either way, earlier thinking included (unlike threshold compaction with custom instructions on Claude 5.1 and later models, which leaves earlier thinking out). Keep taking turns against the full history while it runs, and do not edit anything already sent. + +On the first request after the block arrives, drop exactly the messages you sent to the compaction request from the front of the history and put the block first, as an `assistant` message of its own (the request that does this adopts the block; the API also accepts it as the first content block of the first kept message, and `prefix_diff.py` checks the kept turns only in the message-of-its-own form); everything appended since stays, thinking included, and keeps verifying. Keep nothing else from the dropped messages: summarized messages re-sent after the block are not rejected - the model sees them twice, summary then verbatim - so drop them yourself. Keep the block first on every later request; to compact again, send `compaction` on a request that starts with the current block, and from then on send only the newest block (a request that carries more than one `compaction` block is a 400). Do not send `compaction` and `context_management` in the same request. Text instructions in `role: "system"` messages and `tool_addition` / `tool_removal` blocks that sat inside the summarized messages stop applying at adoption (tool changes excepted when the block carries `tool_changes` - next sentence): re-declare them in a `role: "system"` message on the first request that adopts the block, directly after that request's new `user` turn (which comes after the kept turns - a system message between the block and the kept turns breaks their thinking), and leave it there afterward. Where the compaction request also carried `inline-tools-2026-09-15`, the block records the summarized messages' net tool changes in a `tool_changes` field: send the block back unmodified and they carry over by themselves, so re-declare no tool change; a block without that field carries none, so re-declare as above. The block is accepted on any model that supports the beta, with any later `system` or `tools`, but the kept turns' thinking verifies only on a model that can read it, only if every compaction request since that thinking was produced ran on a model with preserved thinking, and only while `system` and the tools other than `defer_loading: true` ones stay what the compaction request had: changing either can invalidate the kept turns' thinking and has no other effect, so to change them without losing any, compact the whole conversation first (keeping no turns), then change them on the next request. Nothing before the block is sent, but the kept turns are still checked against the summarized messages as they stood when you sent the compaction request, so do not touch them in between; `prefix_diff.py` compares them against the earlier request by aligning on a kept thinking block both carry, and when the compaction request itself is in the capture it checks that request's `system` and `tools` against the conversation's, compares the adopting request's `system` and `tools` against that request, and checks that the dropped range is the messages it carried. + +**Tool changes by value (beta `inline-tools-2026-09-15`, Claude API; the Mid-conversation system messages and tool changes page, "Define tools in a message").** Keep `tools` exactly as the first request sent it, on every request, and make every later change by appending one `role: "system"` message: a `tool_addition` whose `tool` is `{"type": "tool_definition", "definition": {...}}` with the full `tools` entry (name, description, `input_schema`) inside `definition`, for a new tool or for a same-name tool whose description or schema changed - a different definition replaces the tool from that message on, an identical one changes nothing (safe to resend on a retry) - and a `tool_removal` by reference to withdraw one. Nothing already sent moves, so earlier thinking keeps verifying; rewording or deleting a definition message already sent is an edit like any other. The header also covers changes by reference, so it replaces `mid-conversation-tool-changes-2026-07-01`; the placement rules are the same, including no tool change directly after a paused assistant turn. A tool added this way may itself be `defer_loading: true` (inside `definition`); a tool already known at the first request belongs in `tools` with `defer_loading: true`, shown later by reference. Keep at least one non-deferred tool in `tools` (a tool search tool counts): otherwise the first tool defined by value costs one full cache miss. `cache_control` goes on the block or in the definition, not both, and never on a deferred definition. Reusing a name for a different type of tool is a 400, and during the beta some tool types (computer use among them) cannot be defined in a message - declare those in `tools` and add them by reference. A definition stays in the history after the tool is replaced or withdrawn, so a beta header that a dated tool `type` needs goes on every later request of the conversation. For an MCP connector server, add `mcp-client-2026-09-15` (in place of `mcp-client-2025-11-20`, which it includes): the `definition` can then be an `mcp_toolset` (connection details stay in `mcp_servers`), and a response for which the API fetched a server's tool list starts with one `mcp_tool_listing` block per server fetched (code that reads `content[0]` must skip them) - send the assistant message back as it came, those blocks included, keep the header on every request that carries one, and later requests reuse that list instead of asking the server again. + +## Keep list - what never to flag + +The scan and the diff will tempt you to report things the check does not care about. These stay out of the report (or go in a "checked, fine" line): + +- Reordering tools in the `tools` array without changing them - compared as a name-keyed set. +- Adding a `defer_loading: true` tool that nothing has referenced yet. +- Adding, moving, or removing `cache_control` markers anywhere. +- Changing `model`, `max_tokens`, `temperature` and other sampling parameters, `tool_choice`, `metadata`, `stop_sequences`, `stream`, `service_tier`, `output_config` (including effort), or the `thinking` configuration itself - none is part of the compared prefix today (a model change triggers the separate model check, reported as `model_binding_mismatch`, not this one). Request headers are not part of it either, except that a beta which makes the API inject a tool server-side (code execution, the web-search fallback) changes the tool set with an identical body. +- String content versus a single text block of the same text; leading or trailing whitespace of a text block; whitespace-only text blocks; JSON key order; `1` versus `1.0`. +- A rotated or re-signed URL for an image or document that serves the same bytes. +- Removing thinking blocks from the start of the history (oldest first), from the end, or all of them - allowed, and the Preserved thinking page documents all three. The kept blocks must stay an unbroken run of the original sequence; it costs the reasoning, not the validity of the kept blocks. An assistant turn whose `tool_use` still awaits its `tool_result` should keep its thinking (the page asks for that). Two things are not on this list: removing a block from the middle, which invalidates every block after it, and putting a removed block back, which invalidates the blocks produced while it was gone. +- Anything the API itself adds or rewrites server-side (server-side compaction, context editing, thinking clearing, its own injected text) - the check runs on the request as you sent it. +- Mid-conversation `role: "system"` messages and cleared turn-scoped messages that are left in place and replayed verbatim. + +## Failure modes to avoid + +- **Measuring on a slice that never replays thinking.** The first request of every conversation replays nothing; short turns may return no thinking block; a harness that strips thinking has nothing to check. Read the replayed-thinking count before the drop count, every run. +- **Counting entries instead of breaks.** A stale block re-fails on every later request. Report conversations with a first break and the turn it happened at; an entries-per-request number only ever goes up with conversation length. +- **Trusting the header over the body.** The diagnosis header is best-effort and unpublished; `input_transformations` is the contract. A drop with no header is still a drop; a header-only pipeline misses every drop where the header did not arrive. +- **Reading a header-only run as an enforcement test.** On an organization that is not enforced yet the header alone records failures as `thinking_mismatch_allowed` and drops nothing: good for finding edits in production, no measure of what enforcement costs. Set `prefix_mismatch_behavior` for that. +- **Misspelling the field.** Only `block_binding.prefix_mismatch_behavior` is the documented name; the probe removes any other key it finds under `block_binding` and says which; production code should write the documented name. +- **Reading the capture from the app's own objects.** A capture rebuilt from ORM or domain objects hides the re-serialization the check catches. Capture at the HTTP layer. +- **Replaying someone else's conversation.** A capture from another organization still gets its blocks dropped, but it is not diagnosed, and the drop teaches nothing about the harness. Replay with a key from the organization that produced the capture. +- **Bundling fixes.** Two causes fixed in one diff cannot be attributed or reverted separately. One cause per diff, re-measured each time. +- **Prescribing a compaction rewrite as if it were required.** Keep-tail and background compaction have no append-only client-side form without the `compact-2026-09-04` beta (on-demand compaction); measure the scheme the product has, choose `error` or `drop_block`, and record the decision. +- **Setting `drop_block` and calling it done.** `"drop_block"` hides the error but doesn't fix the edit that caused it. Dropped blocks aren't billed, but a session's token usage might still increase because Claude can sometimes think more to re-create the dropped thinking. The increase tends to be larger when more thinking blocks are dropped, or when blocks are dropped on more turns of a long session. Use `drop_block` to measure (Step 2) and as a recorded stopgap, count the responses in each session whose `input_transformations` has a `prefix_binding_mismatch` entry, and fix the edit. +- **Resending the refused body, or fixing a broken session per request.** The Preserved thinking page's "Handle the error in code": retry once with the beta header and `prefix_mismatch_behavior: "drop_block"`, and store that choice with the session so every later request sends it too, including after a restart; where the beta header cannot be sent, remove every `thinking` and `redacted_thinking` block from the history once and leave them out. A saved session that now fails on every request has the edit stored in it: the same remedy applies, thinking produced from then on stays valid as long as nothing before it changes again, and the edit still has to be found so new sessions do not hit it. +- **A library, proxy or gateway that rewrites what it forwards.** Its rewrites are edits its users cannot see or fix. Forward the caller's `anthropic-beta` values and `thinking.block_binding` unchanged and return `input_transformations` to them (an options schema that rejects unknown keys stops a caller from choosing `"drop_block"`); leave a `role: "system"` message where the caller put it - moving it into the top-level `system` field invalidates every thinking block in the conversation; turn tool use off with `tool_choice: {"type": "none"}`, never by removing `tools`; and do not hide the 400 - code that catches it, strips thinking and retries on the caller's behalf logs that it did. +- **An unrecorded strip.** Stripping thinking after a 400 without making the strip deterministic and recorded re-sends the refused blocks on the next turn and fails again, every turn, for the rest of the conversation. +- **Leaving the production value unset.** Defaults differ by surface and by account age; an unset field on a not-yet-enforced account means the check is only recorded (`thinking_mismatch_allowed`), and that default changes the day the account or the model is enforced. Set it, and monitor the entries or the 400s. +- **Mistaking the model check for this one.** `model_binding_mismatch` entries after a model switch are expected and unbilled; only `prefix_binding_mismatch` is a harness finding. diff --git a/skills/claude-api/shared/preserved-thinking-migration/drop_block_probe.py b/skills/claude-api/shared/preserved-thinking-migration/drop_block_probe.py new file mode 100644 index 000000000..2c76dd078 --- /dev/null +++ b/skills/claude-api/shared/preserved-thinking-migration/drop_block_probe.py @@ -0,0 +1,1043 @@ +#!/usr/bin/env python3 +"""drop_block_probe.py -- replay a captured conversation against the Messages API with the +preserved thinking controls turned on, and report what the API drops and why. + +Dependency-free (Python 3.8+, urllib only). Credentials come from the environment and are never +printed: ANTHROPIC_API_KEY (sent as x-api-key) or ANTHROPIC_AUTH_TOKEN (sent as +"Authorization: Bearer ..."; for example the short-lived token an OAuth login provides). When both +are set only the token is sent -- the API rejects a request carrying both headers. A bearer token +also gets the oauth-2025-04-20 beta value, which the API requires for OAuth and workload-identity +tokens. + + python3 drop_block_probe.py capture.jsonl --yes # replay, mode drop_block (without --yes: plan only) + python3 drop_block_probe.py capture.jsonl --yes --mode error # the loud arm (expect 400s) + python3 drop_block_probe.py captures/ --yes # one conversation per *.jsonl file + python3 drop_block_probe.py captures/ --yes --count-tokens # free first pass on the token-counting endpoint + python3 drop_block_probe.py capture.jsonl --dry-run # validate the capture, send nothing + python3 drop_block_probe.py capture.jsonl --json out.json # machine-readable results too + python3 drop_block_probe.py --self-test --model --yes # three tiny live requests (cents); exits 1 if the check is not running + +First-party only: the probe speaks to the Claude API (POST /v1/messages). Captures +taken on Amazon Bedrock or Google Cloud Vertex AI cannot be replayed here -- their request shape, +authentication and per-model availability of the controls differ; run the account's own client +there and read input_transformations from its responses. + +Use a key from the SAME organization as the capture (a dedicated key or workspace is fine; a +capture replayed with a different organization's key is not diagnosed). Captures hold end-user +content: keep them out of the repository, share the --json output rather than the captures, +and delete them when the work is done. Requests that replay no thinking block are skipped by +default (they cannot fail the check); --include-no-thinking sends them too. + +Capture format: one request body per line, in the order the harness sent them, for ONE +conversation (a directory holds one file per conversation). A line may also be a wrapper +{"conversation_id": "...", "headers": {"anthropic-beta": "..."}, "request": {...}} so several +conversations can share a file and the capture's own beta values travel with it (they are merged +into the anthropic-beta value the probe sends). +Assistant turns must carry the thinking blocks exactly as the API returned them (signature +included) -- that is what the API checks. Captures taken from another organization's traffic +cannot be replayed meaningfully: the API only diagnoses blocks your own organization created. + +What the probe changes on every request: the anthropic-beta value thinking-binding-controls-2026-08-01 +(plus --beta values and the capture's own), thinking.block_binding.prefix_mismatch_behavior = --mode, +max_tokens = --max-tokens (default 16: the verdict is decided before the first output token, so the +reply is not needed and the replay stays cheap), and stream off unless --stream. Any +other spelling of the mismatch-behavior field found under block_binding in a capture is removed so +that only the documented name is sent. A request with no thinking configuration is sent with {type: adaptive} (on models with preserved thinking +that is what the API runs anyway, and the thinking configuration is outside the compared prefix); a request +with {type: enabled} is rewritten to adaptive for the same reason; thinking disabled is SKIPPED. On every +request that declares tools the probe sets tool_choice to none (outside the compared prefix) so that no +tool -- server-side or the harness's own -- can run during a replay; it never removes tools from the +capture, because the tool set IS compared. temperature is set to 1 and top_p / top_k are removed (a +thinking request rejects them). Every such change is printed as a "shaped:" note. Nothing is sent without +--yes. --count-tokens replays against /v1/messages/count_tokens, which runs the same conversation check +for free (a 400 in --mode error is the break signal; no input_transformations on a 200). + +Status words per turn: BREAK (a prefix-check drop or rejection), ok (replayed thinking, nothing dropped), +none (the request replayed no thinking block, so it could not fail), model (only model-check drops: a +model switch, not a prefix edit), WARN (a drop reason this probe does not classify), HTTP nnn / N/A +(not evaluated). Exit status: 0 no break, 1 inconclusive, 2 at least one break -- not severity-ordered. + +What it records per request: HTTP status, every input_transformations entry, the +anthropic-thinking-prefix-mismatch response header if the API sent one, request id, usage, and a +client-side digest of the three compared parts (system, tools, and the messages before each +replayed thinking block) so the request where a digest changed can be found without the header. +A model_binding_mismatch entry (the current model cannot read a block another model produced -- a model +switch, not a prefix edit) is counted apart from prefix breaks: per turn as model_drops, per conversation +as model_drop_turns. It never makes a request fail, in either mode. + +Per conversation: the first request (turn) with a prefix_binding_mismatch drop, the number of NEW +dropped blocks per request -- keyed by the dropped block's signature (or a redacted block's data), +resolved from the body that was sent, because paths shift when history is truncated; the path is +kept in the record -- the distinct dropped blocks in total (turns of reasoning lost), and the +diagnosis pattern(s) seen. A request whose status is not 200, and not a 400 that says "bound to a +different conversation", is NOT EVALUATED (a 401, a 429 that outlasted the retries, a network +error, ...): it is reported as such, and a conversation with any unevaluated request is marked +inconclusive and left out of the break share. A 200 whose body carries NO input_transformations +key at all is also NOT EVALUATED: the check never saw the request (a gateway stripping the beta +header, or a surface without the controls). So is a 200 carrying a thinking_mismatch_allowed +entry: the probe sets thinking.block_binding on every replay and a request that sets the field +never receives one, so the field was stripped on the way (a gateway or proxy rebuilding the +body) and the check ran record-only. Exit status: 0 clean, 1 inconclusive or errors, +2 breaks found. --json is validated before the first request and written after every request, so +a bad path costs nothing and a crash loses nothing. + +Costs real money: every replayed request is billed (input tokens; the reply is capped). Use a +small slice; --max-requests caps it; --dry-run prints an input-token estimate for the approval. +Replaying does not run the application's own tools (the capture already holds their results), and +server-side tools -- web search, code execution, MCP connectors -- cannot run either, because the +probe sets tool_choice to none on every replayed request that declares tools; never remove tools +from a capture to the same end (the tool set IS compared). +""" +import argparse +import glob +import hashlib +import http.client +import json +import os +import re +import socket +import sys +import time +import urllib.error +import urllib.request + +# With stdout/stderr redirected to a pipe (as under a tool runner), CPython on Windows encodes with +# the ANSI code page and errors="strict", so one character outside it (an emoji in an excerpt) +# would abort the run mid-report. Never raise on output. +for _stream in (sys.stdout, sys.stderr): + try: + _stream.reconfigure(errors="backslashreplace") + except (AttributeError, ValueError): # a non-TextIOWrapper stand-in; nothing to configure + pass + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +try: + import prefix_diff # optional sibling: canonical form for digests and the --dry-run pair diff +except Exception: # pragma: no cover + prefix_diff = None + +BINDING_BETA = "thinking-binding-controls-2026-08-01" +OAUTH_BETA = "oauth-2025-04-20" # required with a bearer token (OAuth or workload identity); the API 401s without it +# Fields of a create request that the token-counting endpoint does not accept (it rejects unknown fields). +COUNT_TOKENS_EXCLUDED = ("max_tokens", "stream", "temperature", "top_p", "top_k", "stop_sequences", "metadata", + "service_tier") # create-only fields the count endpoint rejects; everything else goes as captured +# The public entry types in input_transformations, written as the wire examples show them. The +# record-only entry (thinking_mismatch_allowed) is written when the beta header is present but +# thinking.block_binding is unset; a request that sets the field never receives one, and the probe +# sets it on every replay, so one on a replay proves the field was stripped on the way to the check. +DROPPED_ENTRY = {"type": "thinking_dropped"} +ALLOWED_ENTRY = {"type": "thinking_mismatch_allowed"} +PREFIX_HEADER = "anthropic-thinking-prefix-mismatch" +DEFAULT_BASE_URL = os.environ.get("ANTHROPIC_BASE_URL", "https://api.anthropic.com") +RETRY_STATUSES = {408, 409, 429, 529, 500, 502, 503, 504} +# The HTTP status each documented in-band SSE error type stands for: a streamed request can +# answer 200 and then fail with an error event before message_start (the streaming form of a +# 429/529), and the retry loop and error handling must see that as the failure it is. +STREAM_ERROR_STATUSES = {"invalid_request_error": 400, "authentication_error": 401, "permission_error": 403, + "not_found_error": 404, "request_too_large": 413, "rate_limit_error": 429, + "api_error": 500, "overloaded_error": 529} + + +# ----------------------------------------------------------------------------- capture loading + + +def iter_lines(path): + with open(path, "r", encoding="utf-8") as f: + for n, line in enumerate(f, 1): + line = line.strip() + if line: + try: + yield n, json.loads(line) + except ValueError as e: + raise SystemExit("%s:%d: not valid JSON (%s)" % (path, n, e)) + + +def load_capture(path): + """Return an ordered dict conversation_id -> list of (label, body).""" + convs, skipped_compaction = {}, [] + files = sorted(glob.glob(os.path.join(path, "*.jsonl"))) if os.path.isdir(path) else [path] + if not files: + raise SystemExit("no *.jsonl files under %s" % path) + for fpath in files: + base = os.path.splitext(os.path.basename(fpath))[0] + for n, obj in iter_lines(fpath): + hdrs = {} + if isinstance(obj, dict) and "request" in obj and isinstance(obj["request"], dict): + # Only the documented conversation_id groups (the same rule as + # prefix_diff.load_requests); a wrapper's id is a per-request stamp in some + # captures and a correlation key in others, so it never decides grouping. + cid = str(obj["conversation_id"]) if obj.get("conversation_id") else base + body = obj["request"] + hdrs = {k.lower(): v for k, v in (obj.get("headers") or {}).items()} if isinstance(obj.get("headers"), dict) else {} + else: + cid, body = base, obj + if not isinstance(body, dict) or "messages" not in body: + raise SystemExit("%s:%d is not a Messages API request body" % (fpath, n)) + if isinstance(body.get("compaction"), dict): + # A compaction request (beta compact-2026-09-04; the same test as prefix_diff.is_compaction_request): + # replaying it would buy a summary, not test a turn, and its 200 would count as a passing turn. + skipped_compaction.append("%s:%d" % (os.path.basename(fpath), n)) + continue + convs.setdefault(cid, []).append(("%s:%d" % (os.path.basename(fpath), n), body, hdrs)) + if skipped_compaction: + sys.stderr.write("skipped %d compaction request(s) (top-level compaction field), not conversation turns: %s\n" + % (len(skipped_compaction), ", ".join(skipped_compaction))) + return convs + + +def count_thinking(body): + n = 0 + for m in body.get("messages", []): + c = m.get("content") + if isinstance(c, list): + n += sum(1 for b in c if isinstance(b, dict) and b.get("type") in ("thinking", "redacted_thinking")) + return n + + +def estimate_input_tokens(body): + """Rough input-token estimate for an approval number: JSON bytes / 4 (images excluded).""" + slim = json.loads(json.dumps(body)) + for m in slim.get("messages", []): + if isinstance(m.get("content"), list): + for b in m["content"]: + if isinstance(b, dict) and b.get("type") in ("image", "document") and isinstance(b.get("source"), dict): + b["source"] = {"type": b["source"].get("type")} + return len(json.dumps(slim)) // 4 + + +def resolve_block(body, path): + """messages.{i}.content.{j} -> the block in the body that was sent, or None.""" + try: + parts = path.split(".") + if parts[0] != "messages" or parts[2] != "content": + return None + m = body["messages"][int(parts[1])] + c = m.get("content") + return c[int(parts[3])] if isinstance(c, list) else None + except (KeyError, IndexError, ValueError, TypeError): + return None + + +def block_key(block, path): + """Stable identity of a thinking block across requests: its signature (thinking) or data + (redacted_thinking); the path only as a last resort.""" + h = lambda s: hashlib.sha256(s.encode("utf-8")).hexdigest()[:16] + if isinstance(block, dict): + if block.get("type") == "thinking" and block.get("signature"): + return "sig:" + h(block["signature"]) + if block.get("type") == "redacted_thinking" and block.get("data"): + return "data:" + h(block["data"]) + return "path:" + path + + +def digests(body): + """Client-side digests of the compared parts: system, tools, and messages before each replayed + thinking block. Uses prefix_diff's canonical form when available.""" + h = lambda s: hashlib.sha256(s.encode("utf-8")).hexdigest()[:16] + if prefix_diff is not None: + sysd = h(prefix_diff.fp(prefix_diff.canon_system(body.get("system")))) + inline, deferred, refd = prefix_diff.canon_tools(body.get("tools"), body.get("messages")) + toolsd = h(prefix_diff.fp(inline)) + raw_msgs, start = prefix_diff.from_compaction(body.get("messages")) # the check restarts at the last compaction block + msgs = prefix_diff.canon_messages(raw_msgs) + before = [] + acc = [] + for i, x in enumerate(msgs): + if x["sigs"]: + before.append({"path": "messages.%d" % (i + (start or 0)), "digest": h(prefix_diff.fp(acc))}) + acc.append(x["msg"]) + return {"system": sysd, "tools": toolsd, "messages_before_thinking": before} + j = lambda o: json.dumps(o, sort_keys=True, separators=(",", ":")) + return {"system": h(j(body.get("system"))), "tools": h(j(body.get("tools"))), "messages_before_thinking": []} + + +def validate(convs): + """Structural checks; returns a list of problems (strings).""" + problems = [] + for cid, reqs in convs.items(): + for label, body, hdrs in reqs: + for name in hdrs: + low = str(name).lower() + if any(w in low for w in ("key", "authorization", "token", "secret", "cookie")): + problems.append("%s: the capture wrapper carries a %s header that looks like a credential -- remove credentials " + "from captures (the probe never sends or prints them; only anthropic-beta is read)" % (label, name)) + if not body.get("model"): + problems.append("%s: no model" % label) + for srv in body.get("mcp_servers") or []: + if isinstance(srv, dict) and srv.get("authorization_token"): + problems.append("%s: mcp_servers[%s] carries an authorization_token -- the probe sends it as the harness did (the " + "connector's tool list is part of what the check compares, so it cannot be removed); use a " + "test-scoped token in the capture and rotate it afterwards" % (label, srv.get("name"))) + break + for i, m in enumerate(body.get("messages", [])): + c = m.get("content") + if isinstance(c, list): + for j, b in enumerate(c): + if isinstance(b, dict) and b.get("type") == "thinking" and not b.get("signature"): + problems.append("%s: messages.%d.content.%d thinking block has no signature (the API needs the signature it returned)" % (label, i, j)) + if prefix_diff is not None: + for (la, a, _ha), (lb, b, _hb) in zip(reqs, reqs[1:]): + r = prefix_diff.diff_pair(a, b) + if r["verdict"] == "chain-warning": + problems.append("%s -> %s: %s" % (la, lb, "; ".join(r.get("thinking_notes", [])))) + if r["verdict"] == "mismatch": + problems.append("%s -> %s: prefix edit in the capture itself (%s) -- expected if this is the harness under test, " + "but a capture that was hand-edited will be diagnosed as such" % (la, lb, prefix_diff.header_line(r))) + return problems + + +# ----------------------------------------------------------------------------- request shaping + + +def prepare(body, mode, model=None, stream=False, max_tokens=16): + """Return (shaped body, skip reason or None, notes). Only fields OUTSIDE the compared prefix are touched: + the model (on request), the thinking configuration, block_binding, max_tokens, stream, tool_choice and + the sampling parameters a thinking request rejects. system, tools and messages go out exactly as captured.""" + notes = [] + b = json.loads(json.dumps(body)) # deep copy + if model: + b["model"] = model + th = b.get("thinking") + if not isinstance(th, dict): + # On models with preserved thinking a request with no thinking key already runs with adaptive thinking, + # and the thinking configuration is outside the compared prefix, so adding it changes no verdict. + th = {"type": "adaptive"} + notes.append("no thinking configuration in the capture: sent with {type: adaptive} (outside the compared prefix)") + if th.get("type") == "disabled": + return None, "thinking is disabled in this request; nothing to check", notes + bb = th.get("block_binding") + if th.get("type") == "enabled": + # The budget form is rejected by models that only take adaptive thinking; adaptive is accepted everywhere the + # check runs, and the configuration is outside the compared prefix. + th = {"type": "adaptive"} + notes.append("thinking {type: enabled} rewritten to {type: adaptive} (budget_tokens dropped; outside the compared prefix)") + if not isinstance(bb, dict): + bb = {} + removed = [k for k in bb if k != "prefix_mismatch_behavior"] + for k in removed: + bb.pop(k) # only the documented key goes out + if removed: + notes.append("removed undocumented block_binding key(s) %s" % removed) + bb["prefix_mismatch_behavior"] = mode + th["block_binding"] = bb + b["thinking"] = th + # A thinking request rejects temperature != 1, top_p and top_k; none is part of the compared prefix. + if b.get("temperature") not in (None, 1, 1.0): + b["temperature"] = 1 + notes.append("temperature set to 1 (required with a thinking configuration)") + for k in ("top_p", "top_k"): + if k in b: + b.pop(k) + notes.append("%s removed (rejected with a thinking configuration)" % k) + # No tool may run during a replay, server-side or the harness's own: tool_choice is outside the compared + # prefix, so "none" is safe to set on every request that declares tools or MCP servers. Never remove tools + # from the capture (the tool set IS compared; removing one would turn the request into a tool_set_changed break). + if b.get("tools") or b.get("mcp_servers"): # connector tools are declared server-side, so mcp_servers alone counts + prior = b.get("tool_choice") + b["tool_choice"] = {"type": "none"} + if isinstance(prior, dict) and prior.get("type") in ("any", "tool"): + notes.append("forced tool_choice %s replaced by none (no tool runs during a replay)" % prior.get("type")) + # mcp_servers stay as captured: the API fetches the connector's tool list server-side and those tools are + # part of the compared tool set, so removing the entry would read as a tool_set_changed break the harness + # never made. validate() warns when the entry carries an authorization_token (it goes out as the harness + # sent it); tool_choice none keeps the connector's tools from being called. + if max_tokens: + b["max_tokens"] = max_tokens + b["stream"] = bool(stream) + return b, None, notes + + + +def stream_error(body, status, raw): + """The error object of an in-band SSE error event that arrived on a streamed 200 before any + message_start, or None. An error after message_start is a mid-stream cut of a request the + check already saw, so it is left to parse_body.""" + if status != 200 or not (isinstance(body, dict) and body.get("stream")): + return None + for line in raw.decode("utf-8", errors="replace").split("\n"): + if not line.startswith("data:"): + continue + try: + ev = json.loads(line[5:].strip()) + except ValueError: + continue + if ev.get("type") == "message_start": + return None + if ev.get("type") == "error": + return ev.get("error") or {} + return None + + +def post_with_retries(base_url, path, body, betas, api_key, timeout, retries=3, backoff=5.0): + """post(), retrying the same request on 408/409/429/529/5xx (unless x-should-retry: false). + An in-band SSE error event on a streamed 200 is surfaced as the status it stands for + (STREAM_ERROR_STATUSES), so an overloaded_error is retried and reported, never misread + as a response without the input_transformations key.""" + attempt = 0 + while True: + status, headers, raw, secs = post(base_url, path, body, betas, api_key, timeout) + err = stream_error(body, status, raw) + if err is not None: + status = STREAM_ERROR_STATUSES.get(err.get("type"), 500) + raw = json.dumps({"type": "error", "error": err}).encode("utf-8") + should = headers.get("x-should-retry", "").lower() + if (status is None or status in RETRY_STATUSES) and should != "false" and attempt < retries: + attempt += 1 + wait = backoff * attempt + try: + wait = float(headers.get("retry-after", wait)) + except ValueError: + pass + time.sleep(wait) + continue + if attempt: + headers = dict(headers) + headers["x-probe-retries"] = str(attempt) + return status, headers, raw, secs + + +def read_credentials(): + """Credential headers from the environment, or None when neither variable is set. Values are + never printed by this script. Exactly one header is sent: the API rejects a request carrying + x-api-key and Authorization together, so when both variables are set the token wins.""" + key = os.environ.get("ANTHROPIC_API_KEY") + token = os.environ.get("ANTHROPIC_AUTH_TOKEN") + if token: + if key: + sys.stderr.write("both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN are set; sending only the token" + " (the API rejects a request carrying both headers)\n") + return {"authorization": "Bearer " + token} + if key: + return {"x-api-key": key} + return None + + +CREDENTIALS_HINT = "set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN (neither is ever printed)" + + +def post(base_url, path, body, betas, api_key, timeout): + data = json.dumps(body).encode("utf-8") + req = urllib.request.Request(base_url.rstrip("/") + path, data=data, method="POST") + req.add_header("content-type", "application/json") + for name, value in (api_key if isinstance(api_key, dict) else {"x-api-key": api_key}).items(): + req.add_header(name, value) + req.add_header("anthropic-version", "2023-06-01") + req.add_header("anthropic-beta", ",".join(betas)) + t0 = time.time() + try: + with urllib.request.urlopen(req, timeout=timeout) as resp: + raw = resp.read() + return resp.status, dict((k.lower(), v) for k, v in resp.headers.items()), raw, time.time() - t0 + except urllib.error.HTTPError as e: + raw = e.read() + return e.code, dict((k.lower(), v) for k, v in e.headers.items()), raw, time.time() - t0 + except (urllib.error.URLError, socket.timeout, OSError, http.client.HTTPException) as e: + # HTTPException covers a body cut short mid-read (IncompleteRead, BadStatusLine): it is not + # an OSError, and without this arm it would abort the whole run instead of counting as a + # network error the retry path and the unevaluated bookkeeping already handle. + err = json.dumps({"type": "error", "error": {"type": "network_error", "message": "%s: %s" % (type(e).__name__, e)}}).encode("utf-8") + return None, {"x-probe-network-error": str(e)}, err, time.time() - t0 + + +def parse_body(status, raw, streamed): + """Return (message_json_or_error, input_transformations). The second value is a list when the + response carried the key (empty included), and None when a 200 carried no key at all -- the + guide's contract is that every checked response has it, so its absence means the check never + saw the request and the caller must not read the turn as clean.""" + text = raw.decode("utf-8", errors="replace") + if streamed and status == 200: + # take input_transformations from message_start, then message_delta if it carries one + msg, transformations = None, None + for line in text.split("\n"): + if not line.startswith("data:"): + continue + try: + ev = json.loads(line[5:].strip()) + except ValueError: + continue + if ev.get("type") == "message_start": + msg = ev.get("message", {}) + if "input_transformations" in msg: + transformations = msg.get("input_transformations") or [] + elif ev.get("type") == "message_delta" and "input_transformations" in ev: + transformations = ev.get("input_transformations") or [] + return msg or {"type": "stream", "raw_head": text[:200]}, transformations + try: + obj = json.loads(text) + except ValueError: + return {"type": "unparsable", "raw_head": text[:300]}, [] + if status != 200: + return obj, [] + if "input_transformations" not in obj: + return obj, None + return obj, (obj.get("input_transformations") or []) + + +def parse_header(value): + out = {} + for part in (value or "").split(";"): + if "=" in part: + k, v = part.strip().split("=", 1) + out[k.strip()] = v.strip() + return out + + +# ----------------------------------------------------------------------------- the run + + +# The conversation check's own 400s are fixed-form: the failing block's path, +# then the fixed clause. The binding form is pinned to the exact documented +# clause (the error-codes reference spells it), with nothing free between the +# path and the clause; the modified form's wire text is not published in full, +# so its middle is bounded and quote-free - API validation messages echo +# request values inside quotes, so an echoed capture value carrying the same +# words cannot ride it. Backticks stay allowed in that middle: the API's own +# clauses use them for field names (see the binding clause), so refusing them +# would fail every genuine modified 400, while a match forged by unquoted echo +# text can only mislabel that one conversation - the stored text is +# reconstructed from validated pieces either way, never copied. A +# conversation-check 400 the anchors miss fails closed: the turn is not +# evaluated and its error is reduced, never shared. The block path's two +# indices carry the same six-digit budget as shareable_field_path and +# FIRST_CHANGE_RE: real indices are short, a longer digit run is a +# capture-derived number echoed into the text, and the lookahead refuses the +# whole match rather than truncating it, so a forged head falls through to +# the reduced branch instead of riding the reconstruction. +_BLOCK_PATH = r"(messages\.\d{1,6}(?!\d)\.content\.\d{1,6}(?!\d))" +BINDING_400_RE = re.compile( + _BLOCK_PATH + r": Invalid `signature` in `thinking` block\." + r" The block is bound to a different conversation\.") +MODIFIED_400_RE = re.compile(_BLOCK_PATH + r": [^\"'\n]{0,120}cannot be modified") +# The trailing first-changed-message diagnostic some conversation-check 400s +# end with: fixed words plus a bounded index. Real message indices are short; +# a longer digit run is a capture-derived number echoed into the text (the +# same class shareable_field_path's digit budget blocks), so the lookahead +# refuses it outright rather than truncating it, and the search runs only +# over the text AFTER the matched clause, never the free middle. +FIRST_CHANGE_RE = re.compile(r"starting at `?messages\.\d{1,6}(?!\d)`?") +# Top-level Messages API request fields: the only heads a field path in the +# reduced form may start with, so an echoed value at the head of a message is +# not copied into the shared output as the "field path". +REQUEST_FIELD_HEADS = frozenset(( + "model", "messages", "max_tokens", "system", "tools", "tool_choice", + "thinking", "metadata", "stream", "temperature", "top_p", "top_k", + "stop_sequences", "service_tier", "betas", "mcp_servers", "container", +)) +# Known structural sub-field names of Messages API request shapes: the only +# non-numeric tail segments a reduced-form field path may carry. Any other +# segment - a pydantic extra-field path names the unknown key itself, and +# unknown keys here come from the captures - ends the emitted path. +STRUCTURAL_SUBFIELDS = frozenset(( + "role", "content", "type", "text", "source", "data", "media_type", "url", + "file_id", "name", "input", "id", "tool_use_id", "is_error", "thinking", + "signature", "description", "input_schema", "cache_control", "ttl", + "budget_tokens", "user_id", "title", "context", "citations", +)) +PATH_SEGMENT_RE = re.compile(r"([A-Za-z0-9_]+)(?:\[[0-9]{1,6}\])*\Z") + + +def shareable_field_path(path): + """The leading run of `path` segments that are provably request structure: + the head a top-level request field, every later dot-segment a short + numeric index or a known structural sub-field name (short numeric bracket + indices allowed on either - real array indices are small, while a long + digit run can be a capture-derived numeric key, e.g. an account number + used as a JSON key). The whole kept path also has a six-digit budget + across all its indices, so splitting a long number over several segments + or bracket groups buys nothing over what one segment may carry. Cut at + the first segment that does not conform, marking the cut with a "*" + segment, and cap the whole path - so a capture-derived key name inside + a validation path (or text shaped like one) is never copied into the + shared output. None when even the head is not a request field.""" + segments = path.split(".") + kept = [] + digit_budget = 6 + for i, segment in enumerate(segments): + m = PATH_SEGMENT_RE.match(segment) + base = m.group(1) if m else None + if i == 0: + if base not in REQUEST_FIELD_HEADS: + return None + elif base is None or not ((base.isdigit() and len(base) <= 6) or base in STRUCTURAL_SUBFIELDS): + kept.append("*") + break + digits = sum(c.isdigit() for c in segment) + if digits > digit_budget: + if i == 0: + return None + kept.append("*") + break + digit_budget -= digits + kept.append(segment) + out = ".".join(kept) + return out[:120] + "..." if len(out) > 120 else out + + +def is_conversation_check_400(status, error_text): + return bool(status == 400 and error_text and (BINDING_400_RE.match(error_text) or MODIFIED_400_RE.match(error_text))) + + +def shareable_error(status, msg, error_text, conversation_check): + """The error text as stored in the --json results file. The conversation + check's own 400s are stored as a reconstruction built only from validated + pieces - the matched block path, this file's fixed clause, and the + digits-only trailing diagnostic - never as a copy of the server line, so + even a validation 400 that echoed capture words into the anchored shape + could put no capture-derived byte in the shared file. Every other error is + reduced to its type and any leading allowlisted field path, because API + validation messages can echo request values (which here come from the + captures) and the --json file is documented as safe to share. The branch + re-checks the anchored fixed form itself, so no caller flag can route + echoed text into it. The full text still prints on the terminal, which + already shows the captures.""" + if not error_text: + return error_text + if conversation_check and is_conversation_check_400(status, error_text): + m = BINDING_400_RE.match(error_text) + got = m or MODIFIED_400_RE.match(error_text) + clause = ("Invalid `signature` in `thinking` block. The block is bound to a different conversation." + if m else "`thinking` block cannot be modified.") + tail = FIRST_CHANGE_RE.search(error_text, got.end()) + return "%s: %s%s (reconstructed; the terminal output has the server text)" % ( + got.group(1), clause, (" " + tail.group(0)) if tail else "") + etype = (msg.get("error") or {}).get("type") if isinstance(msg, dict) else None + m = re.match(r"([A-Za-z0-9_]+(?:\.[A-Za-z0-9_\[\]]+)*)\s*:", error_text) + where = "" + if m: + path = shareable_field_path(m.group(1)) + if path: + where = " at " + path + return "%s%s (message withheld from --json: not the conversation check; the terminal output has it)" % (etype or ("HTTP %s" % status), where) + + +def run_conversation(cid, reqs, args, api_key, betas, sink=None, flush=None): + """sink/flush: main's shared results list and --json writer, fed after every request so a crash + or Ctrl-C mid-conversation keeps the turns already billed.""" + def record(rec): + results.append(rec) + if sink is not None: + sink.append(rec) + if flush: + flush() + results = [] + seen_keys = set() + first_break = None + patterns = [] + model_drop_turns = [] + skipped = 0 + unevaluated = [] + for turn, (label, body, hdrs) in enumerate(reqs, 1): + if args.max_requests and turn > args.max_requests: + break + replayed = count_thinking(body) + shaped, skip, shaping_notes = prepare(body, args.mode, args.model, args.stream, args.max_tokens) + if not skip and replayed == 0 and not args.include_no_thinking: + skip = "replays no thinking block (nothing for the check to verify); --include-no-thinking to send it anyway" + if skip: + skipped += 1 + if not args.quiet: + print(" turn %2d SKIP %s" % (turn, skip)) + record({"conversation": cid, "turn": turn, "label": label, "status": None, "skipped": skip, + "replayed_thinking_blocks": replayed}) + continue + req_betas = list(betas) + for v in (hdrs.get("anthropic-beta") or "").split(","): + v = v.strip() + if v and v not in req_betas: + req_betas.append(v) + if args.count_tokens: + # the token-counting endpoint runs the same conversation check for free: a 400 in error mode is the + # break signal; a 200 carries no input_transformations, so it cannot count dropped blocks. It takes only + # the parameters of a count request, so the create-only fields are left out. + for k in COUNT_TOKENS_EXCLUDED: + shaped.pop(k, None) + path = "/v1/messages/count_tokens" if args.count_tokens else "/v1/messages" + status, headers, raw, secs = post_with_retries(args.base_url, path, shaped, req_betas, api_key, + args.timeout, args.retries, args.backoff) + msg, transformations = parse_body(status, raw, args.stream and not args.count_tokens) + # A 200 with no input_transformations key was never checked (the count endpoint aside, + # which carries none by design): reading it as clean would be a false pass, so the turn + # is not evaluated and the conversation goes inconclusive. + missing_transformations = transformations is None and status == 200 and not args.count_tokens + if transformations is None: + transformations = [] + if not args.quiet and shaping_notes: + for n in shaping_notes: + print(" shaped: " + n) + prefix_drops = [t for t in transformations if t.get("type") == DROPPED_ENTRY["type"] and t.get("reason") == "prefix_binding_mismatch"] + # A model_binding_mismatch drop comes from the separate model check (the conversation switched to a + # model that cannot read the block). It is not a prefix edit, so it is counted apart from the prefix + # breaks this script measures; it is still reported per turn and per conversation. + model_drops = [t for t in transformations if t.get("type") == DROPPED_ENTRY["type"] and t.get("reason") == "model_binding_mismatch"] + # A thinking_mismatch_allowed entry on a replay means thinking.block_binding never reached the + # check (the probe sets it on every request; a request that sets it never receives one): the check + # ran record-only, so the turn measured nothing and reading it as clean would be a false pass. + allowed_entries = [t for t in transformations if t.get("type") == ALLOWED_ENTRY["type"]] + other_drops = [t for t in transformations if t not in prefix_drops and t not in model_drops and t not in allowed_entries] + keys = {} + for t in prefix_drops: + keys[t.get("path")] = block_key(resolve_block(shaped, t.get("path") or ""), t.get("path") or "") + new_keys = sorted(set(keys.values()) - seen_keys) + new_paths = sorted(p for p, k in keys.items() if k in new_keys) + seen_keys |= set(keys.values()) + hdr = headers.get(PREFIX_HEADER) + diag = parse_header(hdr) if hdr else None + error_text = None + if status != 200: + error_text = (msg.get("error") or {}).get("message") if isinstance(msg, dict) else None + binding_m = BINDING_400_RE.match(error_text) if status == 400 and error_text else None + rejected = bool(binding_m) + # a thinking block whose text was changed is a different 400 ("cannot be modified"): a harness finding, + # not an unevaluated request. The binding classification is pinned to the exact documented clause and + # the modified one to a bounded quote-free middle (see BINDING_400_RE) so echoed capture text cannot + # forge a BREAK; either way the shared record stores only reconstructed text, never the server line. + modified_m = MODIFIED_400_RE.match(error_text) if status == 400 and error_text else None + modified = bool(modified_m) + if modified: + rejected = True + patterns.append("thinking_modified") + if rejected: + # the block path persisted in the shared record is the classifier + # match's own bounded capture, never a re-parse of the free text + p = (binding_m or modified_m).group(1) + kkey = block_key(resolve_block(shaped, p), p) + keys[p] = kkey + if kkey not in seen_keys: + new_keys.append(kkey) + new_paths.append(p) + seen_keys.add(kkey) + binding_field_stripped = bool(allowed_entries) and status == 200 + evaluated = (status == 200 and not missing_transformations and not binding_field_stripped) or rejected + if not evaluated: + if missing_transformations: + unevaluated.append((turn, status, "200 without the input_transformations key: the check never saw this" + " request (a gateway stripping anthropic-beta, or a surface without the controls);" + " run --self-test through the same path")) + elif binding_field_stripped: + unevaluated.append((turn, status, "200 with thinking_mismatch_allowed entries although the request set" + " thinking.block_binding: the field was stripped before the check (a gateway or" + " proxy rebuilding the body and dropping thinking.block_binding), so the check ran" + " record-only; run --self-test through the same path")) + else: + unevaluated.append((turn, status, (shareable_error(status, msg, error_text, False) or "")[:120])) + is_break = bool(prefix_drops) or bool(rejected) + if is_break and first_break is None: + first_break = turn + if model_drops: + model_drop_turns.append(turn) + if diag and diag.get("pattern"): + patterns.append(diag["pattern"]) + usage = msg.get("usage") if isinstance(msg, dict) else None + rec = { + "conversation": cid, "turn": turn, "label": label, "status": status, + "request_id": headers.get("request-id") or headers.get("x-request-id") or (msg.get("request_id") if isinstance(msg, dict) else None), + "replayed_thinking_blocks": replayed, + "input_transformations": transformations, + "prefix_drops": len(prefix_drops), "new_dropped_blocks": len(new_keys), "new_dropped_paths": new_paths, + "dropped_block_keys": sorted(set(keys.values())), "model_drops": len(model_drops), + "model_dropped_paths": [t.get("path") for t in model_drops], "other_drops": other_drops, + "mismatch_allowed": len(allowed_entries), "binding_field_stripped": binding_field_stripped, + "client_digests": digests(shaped), "retries": int(headers.get("x-probe-retries", 0)), + "prefix_mismatch_header": hdr, "diagnosis": diag, + "error": shareable_error(status, msg, error_text, bool(rejected)), + "shaping": shaping_notes, "endpoint": path, + "missing_input_transformations": missing_transformations, + "evaluated": evaluated, "seconds": round(secs, 2), + "usage": {k: usage.get(k) for k in ("input_tokens", "output_tokens", "cache_read_input_tokens", "cache_creation_input_tokens")} if isinstance(usage, dict) else None, + } + record(rec) + if not args.quiet: + print_turn(rec, error_text) + if status == 400 and args.mode == "error" and not args.continue_after_error: + if not args.quiet: + print(" (error mode: stopping this conversation at the first 400; --continue-after-error to keep going)") + break + sent = [r for r in results if "evaluated" in r] + summary = {"conversation": cid, "requests_sent": len(sent), "requests_skipped": skipped, "first_break_turn": first_break, + # which model the replayed blocks were minted by (and replayed on, absent --model): a capture taken on + # a model without preserved thinking cannot show a prefix break, so the reader must see the ids + "models": sorted({b.get("model") for _l, b, _h in reqs if b.get("model")}), + "replayed_model": args.model or None, + "distinct_dropped_blocks": len(seen_keys), "patterns": sorted(set(patterns)), + "model_drop_turns": model_drop_turns, + "replayed_thinking_blocks_max": max([r["replayed_thinking_blocks"] for r in results] or [0]), + "statuses": sorted(set(str(r["status"]) for r in sent)), + "unevaluated": [{"turn": t, "status": s, "error": e} for t, s, e in unevaluated], + "inconclusive": bool(unevaluated) or not sent, + "_results": sent} + return results, summary + + +def print_turn(r, full_error=None): + if not r.get("evaluated"): + flag = "N/A " if r["status"] is None else "HTTP %s" % r["status"] + else: + if r["prefix_drops"] or (r["status"] == 400 and r["error"] and ("different conversation" in r["error"] or "cannot be modified" in r["error"] or ("thinking" in r["error"] and "modified" in r["error"]))): + flag = "BREAK" + elif r["replayed_thinking_blocks"] == 0: + flag = "none " # nothing replayed: this request could not fail the check + elif r.get("other_drops"): + flag = "WARN " # a drop reason this probe does not classify: read the entries + elif r.get("model_drops"): + flag = "model" # only model-check drops: reasoning lost by routing, not a prefix break + else: + flag = "ok" + line = " turn %2d %-5s replayed_thinking=%d" % (r["turn"], flag, r["replayed_thinking_blocks"]) + if r["status"] == 200: + line += " dropped=%d new_blocks=%d new_paths=%s" % (r["prefix_drops"], r["new_dropped_blocks"], r["new_dropped_paths"] or "[]") + if r.get("model_drops"): + line += " model_drops=%d paths=%s (model_binding_mismatch: this model cannot read those blocks -- a model switch, not a prefix edit)" % (r["model_drops"], r["model_dropped_paths"]) + if r["other_drops"]: + line += " other=%s" % [(t.get("type"), t.get("reason")) for t in r["other_drops"]] + print(line + " (%s, %.1fs%s)" % (r["request_id"], r["seconds"], ", %d retr." % r["retries"] if r.get("retries") else "")) + if r["diagnosis"]: + d = r["diagnosis"] + print(" why: kind=%s pattern=%s sections=%s changed_validated=%s position=%s item=%s" % ( + d.get("kind"), d.get("pattern"), d.get("sections"), d.get("changed_validated"), d.get("position"), d.get("item"))) + elif r["status"] == 200 and r["prefix_drops"]: + print(" why: (no %s header on this response -- the header is best-effort; use the client-side diff)" % PREFIX_HEADER) + err = full_error if full_error is not None else r["error"] + if err: + print(" error: %s" % err[:400]) + if r.get("missing_input_transformations"): + print(" no input_transformations key on this 200: the check never saw the request (a gateway" + " stripping anthropic-beta, or a surface without the controls) -- not evaluated") + if r.get("binding_field_stripped"): + print(" thinking_mismatch_allowed on this 200 although the request set thinking.block_binding:" + " the field was stripped before the check (a gateway or proxy rebuilding the body), so the check ran" + " record-only -- not evaluated") + + +def print_summary(summaries): + print("\n== per conversation ==") + for s in summaries: + print(" %-24s requests=%d%s first_break_turn=%s dropped_blocks=%d max_replayed_thinking=%d patterns=%s statuses=%s%s%s%s" % ( + s["conversation"][:24], s["requests_sent"], (" (+%d skipped)" % s["requests_skipped"]) if s["requests_skipped"] else "", + s["first_break_turn"], s["distinct_dropped_blocks"], + s["replayed_thinking_blocks_max"], ",".join(s["patterns"]) or "-", s["statuses"], + (" models=%s" % ",".join(s["models"])) if s.get("models") else "", + (" model_drop_turns=%s" % s["model_drop_turns"]) if s.get("model_drop_turns") else "", + " INCONCLUSIVE" if s["inconclusive"] else "")) + conclusive = [s for s in summaries if not s["inconclusive"]] + nothing_sent = [s for s in summaries if s["inconclusive"] and not s["unevaluated"]] + n = len(conclusive) + broken = sum(1 for s in conclusive if s["first_break_turn"] is not None) + never_replayed = sum(1 for s in conclusive if s["replayed_thinking_blocks_max"] == 0) + lost = sum(s["distinct_dropped_blocks"] for s in conclusive) + unevald = [u for s in summaries for u in s["unevaluated"]] + print("\n%d conversation(s) evaluated: %d with a prefix break (%.0f%%); %d distinct thinking block(s) dropped in total (turns of reasoning lost)" % (n, broken, 100.0 * broken / n if n else 0, lost)) + unclassified = sum(1 for s in summaries for r in s.get("_results", []) if r.get("other_drops")) + if unclassified: + print("%d request(s) reported a drop reason this probe does not classify -- read their input_transformations entries" % unclassified) + model_switched = sum(1 for s in conclusive if s.get("model_drop_turns")) + if model_switched: + print("%d conversation(s) also had model_binding_mismatch drops (a model that cannot read earlier blocks) -- counted apart from prefix breaks; see each turn's model_drops" % model_switched) + if unevald: + by = {} + for u in unevald: + by[u["status"]] = by.get(u["status"], 0) + 1 + print("!! %d request(s) not evaluated (%s) -- %d conversation(s) marked inconclusive and left out of the share" % ( + len(unevald), ", ".join("HTTP %s x%d" % (k, v) if k is not None else "network error x%d" % v for k, v in by.items()), + len(summaries) - n)) + if nothing_sent: + print("!! %d conversation(s) had no request to send (every request was skipped: thinking disabled, or no replayed thinking) -- inconclusive, left out of the share" % len(nothing_sent)) + if never_replayed: + print("!! %d conversation(s) never replayed a thinking block -- a clean result on those proves nothing about binding" % never_replayed) + + +def self_test(args, api_key, betas): + """Three tiny live requests: one mint, then two replays of its thinking block - an honest replay + (expect []) and an edited replay (expect a drop).""" + if not args.model: + raise SystemExit("--self-test needs --model ") + q1 = "What is 17*23? Think it through, then answer with just the number." + body1 = {"model": args.model, "max_tokens": 400, "thinking": {"type": "adaptive"}, + "output_config": {"effort": "max"}, # a short turn at default effort often carries no thinking + "messages": [{"role": "user", "content": q1}]} + print("self-test 1/3: minting a thinking block ...") + shaped, _skip, _n = prepare(body1, args.mode, None, False, max_tokens=0) # the mint needs a real reply + status, headers, raw, _ = post_with_retries(args.base_url, "/v1/messages", shaped, betas, api_key, args.timeout) + msg, tr = parse_body(status, raw, False) + if status != 200: + raise SystemExit("turn 1 failed: HTTP %d %s" % (status, (msg.get("error") or {}).get("message") if isinstance(msg, dict) else raw[:200])) + content = msg.get("content", []) + if not any(b.get("type") == "thinking" for b in content): + raise SystemExit("turn 1 returned no thinking block; pick a model that returns thinking with a signature") + print(" ok: %d content block(s), input_transformations=%s, request %s" % (len(content), tr, headers.get("request-id"))) + follow = {"role": "user", "content": "Now add 9 to that. Just the number."} + honest = {"model": args.model, "max_tokens": 200, "thinking": {"type": "adaptive"}, + "messages": [{"role": "user", "content": q1}, {"role": "assistant", "content": content}, follow]} + edited = json.loads(json.dumps(honest)) + edited["messages"][0]["content"] = "What is 17*23? Answer with just the number." # the edit + outcomes = {} + for name, body, expect in (("honest replay", honest, "[]"), ("edited first message", edited, "a dropped-block entry (or a 400 in --mode error)")): + print("self-test: %s (expect %s) ..." % (name, expect)) + shaped, _skip, _n = prepare(body, args.mode, None, args.stream, args.max_tokens) + status, headers, raw, secs = post_with_retries(args.base_url, "/v1/messages", shaped, betas, api_key, args.timeout) + msg, tr = parse_body(status, raw, args.stream) + hdr = headers.get(PREFIX_HEADER) + print(" HTTP %d input_transformations=%s" % (status, json.dumps(tr))) + print(" %s: %s" % (PREFIX_HEADER, hdr or "(absent)")) + err = ((msg.get("error") or {}).get("message") or "") if (status != 200 and isinstance(msg, dict)) else "" + if err: + print(" error: %s" % err[:400]) + print(" request %s, %.1fs" % (headers.get("request-id") or headers.get("x-request-id"), secs)) + outcomes[name] = (status, tr, err) + h_status, h_tr, _ = outcomes["honest replay"] + e_status, e_tr, e_err = outcomes["edited first message"] + honest_ok = h_status == 200 and h_tr == [] + if args.mode == "error": + edited_ok = e_status == 400 and "bound to a different conversation" in e_err + else: + edited_ok = e_status == 200 and any(t.get("type") == DROPPED_ENTRY["type"] and t.get("reason") == "prefix_binding_mismatch" for t in (e_tr or [])) + if honest_ok and edited_ok: + print("WIRED: the honest replay reported no drop and the edited replay was caught (%s)." % ("a 400, as the loud arm should" if args.mode == "error" else "reported as a dropped block")) + return 0 + why = [] + if not honest_ok: + if h_status == 200 and h_tr is None: + why.append("the honest replay's 200 carried no input_transformations key at all -- the beta header did not" + " reach the API (a gateway or proxy stripping anthropic-beta, or a surface without the controls)") + else: + why.append("the honest replay did not come back 200 with an empty input_transformations (something in the path already edits the history)") + if not edited_ok: + if e_status == 200 and any(t.get("type") == ALLOWED_ENTRY["type"] for t in (e_tr or [])): + why.append("the edited replay came back 200 with thinking_mismatch_allowed entries although the request set" + " thinking.block_binding: anthropic-beta reached the API but the field did not (a gateway or" + " proxy rebuilding the body and dropping thinking.block_binding), so the check records instead" + " of enforcing") + else: + why.append("the edited replay was not caught (wrong model id, the header missing, a platform without the controls, or the field misspelled)") + print("NOT WIRED: " + "; ".join(why)) + return 1 + + +def main(argv=None): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("capture", nargs="?", help="capture.jsonl or a directory of *.jsonl (one conversation each)") + ap.add_argument("--mode", choices=["drop_block", "error"], default="drop_block") + ap.add_argument("--model", help="override the model id in every request (default: the capture's own)") + ap.add_argument("--base-url", default=DEFAULT_BASE_URL) + ap.add_argument("--beta", action="append", default=[], help="extra anthropic-beta value(s); %s is always sent" % BINDING_BETA) + ap.add_argument("--stream", action="store_true", help="replay with stream:true (input_transformations read from message_start)") + ap.add_argument("--max-requests", type=int, default=0, help="cap per conversation (0 = all)") + ap.add_argument("--max-conversations", type=int, default=0) + ap.add_argument("--continue-after-error", action="store_true") + ap.add_argument("--max-tokens", type=int, default=16, help="max_tokens on every replayed request (0 = keep the capture's); the verdict is decided before the first output token") + ap.add_argument("--count-tokens", action="store_true", help="replay against /v1/messages/count_tokens instead of /v1/messages: free, runs the same conversation check; implies --mode error because a 400 is its only break signal (no input_transformations on a 200, so it cannot count dropped blocks)") + ap.add_argument("--yes", action="store_true", help="send the replay; without it the plan is printed and nothing is sent (same as --dry-run), so a live run is always an explicit decision") + ap.add_argument("--include-no-thinking", action="store_true", help="also send requests that replay no thinking block (skipped by default: they cannot fail the check and only add cost)") + ap.add_argument("--retries", type=int, default=3, help="retries of the same request on 408/409/429/529/5xx (x-should-retry: false is honoured)") + ap.add_argument("--timeout", type=float, default=600) + ap.add_argument("--backoff", type=float, default=5) + ap.add_argument("--json", help="write full per-request results to this file") + ap.add_argument("--dry-run", action="store_true", help="validate the capture (structure, signatures, consecutive-pair diff via prefix_diff.py) and print the plan with an input-token estimate; send nothing") + ap.add_argument("--self-test", action="store_true", help="three tiny live requests to check the wiring (needs --model and --yes); exits 1 if the check is not running") + ap.add_argument("--quiet", action="store_true") + args = ap.parse_args(argv) + + betas = [BINDING_BETA] + [b for b in args.beta if b != BINDING_BETA] + creds = read_credentials() + if creds and "authorization" in creds and OAUTH_BETA not in betas: + # a bearer token (OAuth or workload identity) authenticates only with this beta value; without it the API + # 401s every request, so the token path the guide advertises would never run + betas.append(OAUTH_BETA) + if args.count_tokens and args.mode != "error": + # on the count endpoint a drop-mode 200 carries no input_transformations, so every turn would read "ok" + # and the run would be a false clean; the loud arm is the only one the endpoint can report + print("--count-tokens implies --mode error (the count endpoint reports a break only as a 400); using error mode.") + args.mode = "error" + if args.self_test: + if not args.yes: + # every live path sits behind --yes, the self-test included: a bare invocation prints the plan and stops + print("self-test plan: three small live requests to %s on model=%s (one mint with a real reply, two replays" + " of its thinking block), billed at the model's normal rates; mode=%s; betas=%s" + % (args.base_url, args.model or "<--model required>", args.mode, ",".join(betas))) + print("nothing sent: re-run with --yes to send them (they spend real money).") + return 0 + if not creds: + raise SystemExit(CREDENTIALS_HINT) + return self_test(args, creds, betas) + if not args.capture: + ap.error("capture path required (or --self-test)") + convs = load_capture(args.capture) + if args.max_conversations: + convs = dict(list(convs.items())[: args.max_conversations]) + n_req = sum(len(v) for v in convs.values()) + n_think = sum(count_thinking(b) for v in convs.values() for _, b, _h in v) + n_tokens = sum(estimate_input_tokens(b) for v in convs.values() for _, b, _h in v) + problems = validate(convs) + # the plan uses prepare() so the "will be sent" count and the shaping notes match the live run exactly + plan = [] + for v in convs.values(): + for label, b, _h in v: + shaped, skip, notes = prepare(b, args.mode, args.model, args.stream, args.max_tokens) + if not skip and count_thinking(b) == 0 and not args.include_no_thinking: + skip = "replays no thinking block" + plan.append((label, b, skip, notes)) + n_send = sum(1 for _l, _b, skip, _n in plan if not skip) + n_tokens_send = sum(estimate_input_tokens(b) for _l, b, skip, _n in plan if not skip) + shaping = {} + for _l, _b, skip, notes in plan: + for n in notes: + shaping[n] = shaping.get(n, 0) + 1 + print("capture: %d conversation(s), %d request(s) of which %d replay thinking and will be sent, %d replayed thinking block(s) in total; mode=%s; betas=%s; base_url=%s" + % (len(convs), n_req, n_send, n_think, args.mode, ",".join(betas), args.base_url)) + capture_models = sorted({b.get("model") for v in convs.values() for _l, b, _h in v if b.get("model")}) + print("model(s) in the capture: %s%s -- a block is judged by the model that minted it, so a capture whose blocks were" + " minted by a model without preserved thinking cannot show a prefix break (see 'Suspect the test slice' in the guide)" + % (", ".join(capture_models) or "(none)", ("; every request replayed as %s" % args.model) if args.model else "")) + print("estimated input tokens for the %d request(s) to be sent: about %s (JSON bytes / 4, images excluded; all %d requests would be about %s; output capped at max_tokens=%d) -- price it at the model's current input rate for the approval" + % (n_send, "{:,}".format(n_tokens_send), n_req, "{:,}".format(n_tokens), args.max_tokens)) + for p in problems: + print(" note: " + p) + for n, c in sorted(shaping.items()): + print(" shaping (%d request(s)): %s" % (c, n)) + skipped_plan = [(l, s) for l, _b, s, _n in plan if s] + if skipped_plan and not args.quiet: + print(" %d request(s) will be skipped: %s" % (len(skipped_plan), "; ".join("%s (%s)" % ls for ls in skipped_plan[:8]) + (" ..." if len(skipped_plan) > 8 else ""))) + if n_think == 0: + print("!! no request in this capture replays a thinking block: the prefix check has nothing to verify. " + "Capture turns AFTER the first assistant reply, with its thinking blocks included.") + if args.dry_run or not args.yes: + print("dry run: nothing sent." if args.dry_run else "nothing sent: re-run with --yes to send the replay above (it spends real money).") + return 0 + if not creds: + raise SystemExit(CREDENTIALS_HINT) + all_results, summaries = [], [] + + def flush(): + if args.json: + with open(args.json, "w") as f: + json.dump({"mode": args.mode, "betas": betas, "results": all_results, + "summaries": [{k: v for k, v in s.items() if k != "_results"} for s in summaries]}, f, indent=2) + + if args.json: + try: + flush() # an unwritable --json path must fail here, before the first request is billed + except OSError as e: + raise SystemExit("--json %s: %s" % (args.json, e)) + try: + for cid, reqs in convs.items(): + if not args.quiet: + print("\nconversation %s (%d request(s))" % (cid, len(reqs))) + _results, summary = run_conversation(cid, reqs, args, creds, betas, all_results, flush) + summaries.append(summary) + flush() + finally: + try: + flush() + except OSError: + pass # the per-request flushes already persisted what they could + print_summary(summaries) + if args.json: + print("wrote %s" % args.json) + if any(s["first_break_turn"] is not None for s in summaries): + return 2 + if any(s["inconclusive"] for s in summaries) or not summaries: + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/skills/claude-api/shared/preserved-thinking-migration/prefix_diff.py b/skills/claude-api/shared/preserved-thinking-migration/prefix_diff.py new file mode 100644 index 000000000..cfa88044b --- /dev/null +++ b/skills/claude-api/shared/preserved-thinking-migration/prefix_diff.py @@ -0,0 +1,1532 @@ +#!/usr/bin/env python3 +"""prefix_diff.py -- find what changed between two consecutive Messages API requests +in the parts a thinking block is bound to (system, tools, and the earlier messages), +and say it in the API's own vocabulary. + +Dependency-free (Python 3.8+). Two modes: + + 1. DIFF consecutive request bodies of one conversation + + python3 prefix_diff.py capture.jsonl # one request body per line, in send order + python3 prefix_diff.py req_003.json req_004.json + python3 prefix_diff.py captures/ # *.jsonl: one conversation per file (a wrapper + # line's conversation_id overrides); pairs + # never cross conversation boundaries + python3 prefix_diff.py --json capture.jsonl # machine-readable + + For every pair (N, N+1) it reports whether request N+1 still carries request N's + system prompt, tool set and messages unchanged, and if not: a guessed kind/pattern + (same words the API uses in its anthropic-thinking-prefix-mismatch header), the + first changed path, and a Claude-Code-style attribution line such as + system[0] changed at char 47: "...Be concise." -> "...Be concise. Current time: ..." + + 2. SCAN a repository for the code paths that usually cause those edits + + python3 prefix_diff.py --scan path/to/repo [--ext py,ts,js] + + Prints file:line leads grouped by cause. These are regex heuristics -- LEADS to read, + not findings. The diff mode (or the API's own response) is the evidence. + +What the comparison ignores, on purpose (the API ignores them too): + cache_control markers anywhere; an explicit strict: false / eager_input_streaming: false on a tool (the defaults); + string content vs a single text block (same thing); leading/trailing whitespace of a + text block, and whitespace-only text blocks; key order; tool ORDER in the tools array + (tools bind as a name-keyed set); a defer_loading tool that no tool_reference (at any depth), + tool-search result or tool_addition has named yet; thinking / redacted_thinking blocks themselves (they are what is validated, not + part of the compared prefix) -- the API only requires that every kept thinking block + after the first was minted right after the kept block now in front of it, so any + CONTIGUOUS WINDOW of the original sequence replays fine (drop from the front, drop from + the back, or both); a block removed from the middle, or a reorder, is flagged separately + because the chain check fails for the block that follows the gap; request parameters + outside system / tools / messages. A URL-sourced image or document is compared without + its URL string: a rotated URL to the same bytes is a MATCH here and at the API, but the + same URL serving different bytes is a break the API catches and this script cannot see. + +Interior whitespace, tool_use.input bytes, tool_result text, image bytes, a tool's strict / +eager_input_streaming flags and every other difference count. + +A pair where the later request keeps the earlier one's messages up to some point and replaces +everything after it, with NO replayed thinking block at or after that point (a branch, a +regenerate, a retry of the last turn), is reported as MATCH with a "branch" note: nothing the +API would check has changed. This assumes consecutive requests of one conversation; a capture +that skips requests can make a real edit look like a branch. + +This script's kind/pattern is a guess from the bodies alone; the API's own response (the +input_transformations entries, the 400 text, and the diagnosis header when present) is the +authority when they differ. One modelling limit to keep in mind: the pair diff compares each +request with the previous one, while the API judges each replayed block against the request that +produced it. The two agree for an append-only or steadily edited history; they differ when a +harness alternates prompts per model and restores them (handled: the diff then compares against +the last request on the same model) and for blocks minted before a later-referenced tool entered +the prefix (the "at most" count). In particular its counts of removed, inserted and modified items can differ from these, and its +treatment of image and document bytes may become finer-grained than the whole-source comparison here. +""" +import argparse +import difflib +import glob +import json +import os +import re +import sys + +# With stdout/stderr redirected to a pipe (as under a tool runner), CPython on Windows encodes with +# the ANSI code page and errors="strict", so one character outside it (an emoji in an excerpt of a +# changed block) would abort the run mid-report. Never raise on output. +for _stream in (sys.stdout, sys.stderr): + try: + _stream.reconfigure(errors="backslashreplace") + except (AttributeError, ValueError): # a non-TextIOWrapper stand-in; nothing to configure + pass + +IGNORED_TOOL_KEYS = {"cache_control"} # strict and eager_input_streaming ARE compared +MEDIA_TYPES = {"image", "document", "image_url", "document_url"} +INT_LIMIT = 2 ** 63 + + +def clean(obj): + """Recursively drop None-valued keys, collapse integral floats (1.0 -> 1), and sort nothing + (fp() sorts keys). Applied to every compared value.""" + if isinstance(obj, dict): + return {k: clean(v) for k, v in obj.items() if v is not None} + if isinstance(obj, list): + return [clean(v) for v in obj] + if isinstance(obj, float) and obj.is_integer() and abs(obj) < INT_LIMIT: + return int(obj) + return obj + + +def canon_tool_entry(entry): + """One tools[] entry or by-value tool definition, in the shape the API compares: the API + stores a member left at its default as the member spelled out, so `type: "custom"` equals + type absent, `description: ""` equals description absent, `allowed_callers: ["direct"]` + equals allowed_callers absent, and a custom entry's strict / eager_input_streaming default + to False (their VALUES are compared; cache_control is not).""" + entry = clean({k: v for k, v in entry.items() if k not in IGNORED_TOOL_KEYS}) + if entry.get("description") == "": + entry.pop("description") + if entry.get("allowed_callers") == ["direct"]: + entry.pop("allowed_callers") + if not entry.get("type") or entry.get("type") == "custom": + entry.pop("type", None) + entry.setdefault("strict", False) + entry.setdefault("eager_input_streaming", False) + return entry + +# ----------------------------------------------------------------------------- loading + + +def load_requests(paths): + """Return a list of (label, body, conversation) in order. A .jsonl file yields one body per + line; a .json file yields one body (or a list of bodies); a directory yields its *.json / + *.jsonl sorted. The conversation key keeps pairs from crossing conversation boundaries (two + conversations legitimately differ in the compared parts, so a cross-boundary pair is a false + mismatch): each .jsonl FILE is one conversation -- a wrapper line's conversation_id overrides, + so several conversations can share a file, as for the probe -- while bare .json bodies given + together, on the command line or inside one directory, stay one conversation in sorted order + (the two-.json-files usage).""" + out = [] + + def add_file(p, default_conv): + with open(p, "r", encoding="utf-8") as f: + if p.endswith(".jsonl"): + conv = os.path.splitext(p)[0] + for i, line in enumerate(f, 1): + line = line.strip() + if not line: + continue + try: + out.append(("%s:%d" % (os.path.basename(p), i), json.loads(line), conv)) + except ValueError as e: + raise SystemExit("%s:%d: not valid JSON (%s)" % (p, i, e)) + else: + try: + data = json.load(f) + except ValueError as e: + raise SystemExit("%s: not valid JSON (%s)" % (p, e)) + if isinstance(data, list): + for i, b in enumerate(data, 1): + out.append(("%s[%d]" % (os.path.basename(p), i), b, default_conv)) + else: + out.append((os.path.basename(p), data, default_conv)) + + for p in paths: + if os.path.isdir(p): + for fp in sorted(glob.glob(os.path.join(p, "*.json")) + glob.glob(os.path.join(p, "*.jsonl"))): + add_file(fp, p) + else: + add_file(p, "") + bodies = [] + for label, b, conv in out: + if isinstance(b, dict) and "request" in b and isinstance(b["request"], dict) and "messages" in b["request"]: + # a capture wrapper {request:..., conversation_id:..., headers:..., response:...}. + # Only the documented conversation_id groups; a wrapper's id is a per-request stamp + # in some captures and a correlation key in others, so it never decides grouping. + if b.get("conversation_id"): + conv = str(b["conversation_id"]) + b = b["request"] + if not isinstance(b, dict) or "messages" not in b: + raise SystemExit("%s: not a Messages API request body (no 'messages')" % label) + bodies.append((label, b, conv)) + return bodies + + +def is_compaction_request(body): + """True for a body carrying the top-level compaction field (beta compact-2026-09-04): a request for a summary, whose + reply is the signed block (or nothing), not a conversation turn. drop_block_probe.py keeps the same predicate.""" + return isinstance(body, dict) and isinstance(body.get("compaction"), dict) + + +# ----------------------------------------------------------------------------- canonical view + + +def _text_block(text): + return {"type": "text", "text": text} + + +def canon_blocks(content): + """Normalise a content field (string or list of blocks) to the bound view: a list of + blocks with cache_control removed, whitespace-only text dropped, text edges trimmed. + Returns (blocks, thinking_signatures, thinking_text_by_signature).""" + if content is None: + return [], [], {} + if isinstance(content, str): + content = [_text_block(content)] + if not isinstance(content, list): + content = [content] + blocks, sigs, texts = [], [], {} + for wire_idx, b in enumerate(content): + if not isinstance(b, dict): + blocks.append({"type": "_raw", "value": b, "_wire": wire_idx}) + continue + t = b.get("type") + if t in ("thinking", "redacted_thinking"): + sig = b.get("signature") or b.get("data") or "" + sigs.append((wire_idx, sig)) + if t == "thinking": + texts[sig] = b.get("thinking") # kept so a later request's text can be compared (a modified block is a break) + continue + b = clean({k: v for k, v in b.items() if k != "cache_control"}) + tl = b.get("tool") + if t == "tool_addition" and isinstance(tl, dict) and tl.get("type") == "tool_definition" \ + and isinstance(tl.get("definition"), dict): + # compared in the API's canonical shape, exactly as a tools[] entry: a replay that only + # spells out a default (type "custom", description "") is not an edit + b["tool"] = dict(tl, definition=canon_tool_entry(tl["definition"])) + if b.get("citations") in ([], None): + b.pop("citations", None) + if t == "text": + txt = (b.get("text") or "") + if txt.strip() == "": + continue + b["text"] = txt.strip() + if t == "tool_result": + if b.get("is_error") is False: + b.pop("is_error") + if isinstance(b.get("content"), str): + b["content"] = [_text_block(b["content"])] + if isinstance(b.get("content"), list): + inner, _s, _t = canon_blocks(b["content"]) + b["content"] = inner + if t in ("image", "document") and isinstance(b.get("source"), dict) and b["source"].get("type") == "url": + # the API compares the bytes behind the URL, not the URL string: drop it, keep the rest + b["source"] = {k: v for k, v in b["source"].items() if k != "url"} + b["type"] = t + "_url" + b["_wire"] = wire_idx + blocks.append(b) + return blocks, sigs, texts + + +def canon_system(system): + blocks, _s, _t = canon_blocks(system if system is not None else []) + return blocks + + +def _toolset_key(server): + return "mcp_toolset:%s" % server if isinstance(server, str) and server else None + + +def canon_tools(tools, messages): + """Inline tools as a name-keyed map; deferred tools kept separately and compared only + once something in the messages names them (see walk). Server tools (type != custom) are + keyed by type+name, MCP toolsets by server name. A compaction block's tool_changes field + is not read. Once a compaction block has replaced the messages that named a deferred + tool, that tool counts as unnamed and edits to it are not reported (diff_pair adds a + note when the block has a tool_changes field). The exception is the first request that + carries the block: it is compared against an earlier request whose messages still name + the tool.""" + referenced = set() + + def walk(blocks): + # a deferred tool binds once something names it: a tool_reference at any depth (including inside a + # tool_result's content), a tool-search result's tool_references, or a tool_addition block in a + # mid-conversation system message, by reference or by value (the docs do not say whether a + # definition by value binds a deferred entry of the same name; counting it is the cautious choice) + for b in blocks or []: + if not isinstance(b, dict): + continue + t = b.get("type") + if t == "tool_reference": + name = b.get("tool_name") or b.get("name") + if isinstance(name, str) and name: + referenced.add(name) + elif t == "tool_addition": + tool = b.get("tool") if isinstance(b.get("tool"), dict) else b + if tool.get("type") == "tool_definition" and isinstance(tool.get("definition"), dict): + tool = tool["definition"] # inline-tools-2026-09-15 sends a whole tools[] entry in this wrapper + name = tool.get("name") or tool.get("tool_name") + if tool.get("type") in ("mcp_toolset", "mcp_toolset_reference"): + name = _toolset_key(tool.get("mcp_server_name") or tool.get("server_name")) + elif tool.get("server_name") and name and not str(name).startswith(str(tool["server_name"])): + name = "%s_%s" % (tool["server_name"], name) + if isinstance(name, str) and name: + referenced.add(name) + inner = b.get("content") + if isinstance(inner, list): + walk(inner) + elif isinstance(inner, dict): + walk([inner]) + refs = b.get("tool_references") + if isinstance(refs, list): + walk(refs) + if isinstance(inner, dict) and isinstance(inner.get("tool_references"), list): + walk(inner["tool_references"]) + + for m in messages or []: + c = m.get("content") + if isinstance(c, list): + walk(c) + inline, deferred, server_names = {}, {}, {} + for t in tools or []: + if not isinstance(t, dict): + continue + t = canon_tool_entry(t) + name = t.get("name") + if isinstance(name, str) and name: + key = name + elif not name: + key = "%s" % t.get("type") + else: + key = fp(name) + if t.get("type") == "mcp_toolset" and _toolset_key(t.get("mcp_server_name")): + key = _toolset_key(t["mcp_server_name"]) + elif t.get("type") and t.get("type") != "custom" and isinstance(name, str) and name: + key = "%s:%s" % (t["type"], name) + server_names[key] = name + if t.get("defer_loading"): + d = {k: v for k, v in t.items() if k != "defer_loading"} + deferred[key] = d + else: + inline[key] = t + # a server tool is keyed type:name, but a reference or a by-value definition names it by name alone + referenced |= {k for k, n in server_names.items() if n in referenced} + return inline, deferred, referenced + + +def canon_messages(messages): + out = [] + for m in messages or []: + blocks, sigs, texts = canon_blocks(m.get("content")) + entry = {"role": m.get("role"), "content": blocks} + for k in ("clear_at", "output_config"): + if m.get(k) is not None: + entry[k] = m[k] + out.append({"msg": entry, "sigs": sigs, "texts": texts}) + return out + + +def _strip_wire(obj): + if isinstance(obj, dict): + return {k: _strip_wire(v) for k, v in obj.items() if k != "_wire"} + if isinstance(obj, list): + return [_strip_wire(v) for v in obj] + return obj + + +def fp(obj): + return json.dumps(clean(_strip_wire(obj)), sort_keys=True, separators=(",", ":"), ensure_ascii=False) + + +def wire(block, fallback): + return block.get("_wire", fallback) if isinstance(block, dict) else fallback + + +# ----------------------------------------------------------------------------- attribution + + +def first_diff_char(a, b): + n = min(len(a), len(b)) + for i in range(n): + if a[i] != b[i]: + return i + return n if len(a) != len(b) else -1 + + +def excerpt(s, at, width=40): + lo = max(0, at - 12) + return ("..." if lo else "") + s[lo:at + width].replace("\n", "\\n") + ("..." if at + width < len(s) else "") + + +def attribute(label, old, new): + """Claude-Code-style one-liner for a changed scalar/object.""" + so, sn = (old if isinstance(old, str) else fp(old)), (new if isinstance(new, str) else fp(new)) + at = first_diff_char(so, sn) + if at < 0: + return "%s changed (representation only)" % label + return '%s changed at char %d: "%s" -> "%s"' % (label, at, excerpt(so, at), excerpt(sn, at)) + + +# ----------------------------------------------------------------------------- the diff + + +def diff_system(a, b): + """Returns list of attribution strings (empty if equal).""" + notes = [] + n = max(len(a), len(b)) + for i in range(n): + if i >= len(a): + notes.append("system[%d] added: %s" % (i, excerpt(fp(b[i]), 0))) + elif i >= len(b): + notes.append("system[%d] removed: %s" % (i, excerpt(fp(a[i]), 0))) + elif fp(a[i]) != fp(b[i]): + if a[i].get("type") == "text" and b[i].get("type") == "text": + notes.append(attribute("system[%d]" % i, a[i]["text"], b[i]["text"])) + else: + notes.append(attribute("system[%d]" % i, a[i], b[i])) + return notes + + +def diff_tools(ta, tb): + ia, da, refa = ta + ib, db, refb = tb + notes, set_changed = [], False + for name in sorted(set(ia) | set(ib)): + if name not in ib: + notes.append("tools: %s removed" % name); set_changed = True + elif name not in ia: + notes.append("tools: %s added" % name); set_changed = True + elif fp(ia[name]) != fp(ib[name]): + for field in sorted(set(ia[name]) | set(ib[name])): + if fp(ia[name].get(field)) != fp(ib[name].get(field)): + notes.append(attribute("tools: %s %s" % (name, field), ia[name].get(field, ""), ib[name].get(field, ""))) + # deferred tools: bound only once referenced in request N (the minting request's view) + for name in sorted(refa | refb): + if name in da or name in db: + if name not in db: + notes.append("tools: deferred %s (referenced) removed" % name); set_changed = True + elif name not in da: + pass # newly loaded by reference: allowed + elif fp(da[name]) != fp(db[name]): + notes.append(attribute("tools: deferred %s (referenced)" % name, da[name], db[name])) + return notes, set_changed + + +def block_type(b): + return b.get("type") if isinstance(b, dict) else type(b).__name__ + + +def _item_count(msgs): + """Server-style item count: one item per message plus one per content block.""" + return sum(1 + len(x["msg"]["content"]) for x in msgs) + + +def _block_diffs(A, B, changed_idx): + """Block-level opcodes for in-place message edits. Returns (mod, rem, ins, notes, + first_path, first_pos) where mod/rem/ins are lists of (msg_index, block_type).""" + mod, rem, ins, notes = [], [], [], [] + first_path, first_pos, first_wire = None, None, None + for i in changed_idx: + ai, bi = A[i]["msg"], B[i]["msg"] + ca, cb = ai["content"], bi["content"] + if ai.get("role") != bi.get("role") or ai.get("clear_at") != bi.get("clear_at"): + notes.append("messages[%d] role or clear_at changed" % i) + mod.append((i, "message")) + if first_path is None: + first_path, first_pos, first_wire = "messages.%d" % i, "at", (i, -1) + bsm = difflib.SequenceMatcher(a=[fp(x) for x in ca], b=[fp(x) for x in cb], autojunk=False) + for bop, a1, a2, b1, b2 in bsm.get_opcodes(): + if bop == "equal": + continue + if first_path is None: + if bop == "delete": + w = wire(ca[a1 - 1], a1 - 1) if a1 else 0 + first_path, first_pos = ("messages.%d.content.%d" % (i, w), "after" if a1 else "before") + first_wire = (i, wire(ca[a1], a1)) + else: + first_path, first_pos = ("messages.%d.content.%d" % (i, wire(cb[b1], b1)), "at") + first_wire = (i, wire(cb[b1], b1)) + if bop == "replace" and (a2 - a1) == (b2 - b1): + for q in range(a2 - a1): + ta, tb = block_type(ca[a1 + q]), block_type(cb[b1 + q]) + mod.append((i, ta)) # the API keys the change on the STORED (old) item's type + label = "messages[%d] (%s) content[%d] (%s%s)" % (i, ai["role"], wire(ca[a1 + q], a1 + q), ta, "" if ta == tb else " -> " + tb) + notes.append(attribute(label, ca[a1 + q], cb[b1 + q])) + elif bop == "replace": + # unequal lengths: pair old and new blocks of the same type in order and call those + # changes; only the leftovers are removals / insertions + old_q, new_q = list(range(a1, a2)), list(range(b1, b2)) + paired = [] + for q in old_q: + for r in new_q: + if r not in [pr for _, pr in paired] and block_type(ca[q]) == block_type(cb[r]): + paired.append((q, r)) + break + for q, r in paired: + mod.append((i, block_type(ca[q]))) + notes.append(attribute("messages[%d] (%s) content[%d] (%s)" % (i, ai["role"], wire(ca[q], q), block_type(ca[q])), ca[q], cb[r])) + for q in old_q: + if q not in [pq for pq, _ in paired]: + rem.append((i, block_type(ca[q]))) + notes.append("messages[%d] (%s) content[%d] (%s) removed: %s" % (i, ai["role"], wire(ca[q], q), block_type(ca[q]), excerpt(fp(ca[q]), 0))) + for r in new_q: + if r not in [pr for _, pr in paired]: + ins.append((i, block_type(cb[r]))) + notes.append("messages[%d] (%s) content[%d] (%s) inserted: %s" % (i, ai["role"], wire(cb[r], r), block_type(cb[r]), excerpt(fp(cb[r]), 0))) + else: + for q in range(a1, a2): + rem.append((i, block_type(ca[q]))) + notes.append("messages[%d] (%s) content[%d] (%s) removed: %s" % (i, ai["role"], wire(ca[q], q), block_type(ca[q]), excerpt(fp(ca[q]), 0))) + for q in range(b1, b2): + ins.append((i, block_type(cb[q]))) + notes.append("messages[%d] (%s) content[%d] (%s) inserted: %s" % (i, ai["role"], wire(cb[q], q), block_type(cb[q]), excerpt(fp(cb[q]), 0))) + return mod, rem, ins, notes, first_path, first_pos, first_wire + + +def _classify_in_place(A, B, changed_idx): + mod, rem, ins, notes, first_path, first_pos, first_wire = _block_diffs(A, B, changed_idx) + n_mod, n_rem, n_ins = len(mod), len(rem), len(ins) + if n_mod and not n_rem and not n_ins: + kind = "blocks_modified" + elif n_rem and not n_mod and not n_ins: + kind = "blocks_removed" + elif n_ins and not n_mod and not n_rem: + kind = "blocks_inserted" + else: + kind = "blocks_replaced" + touched = sorted(set(i for i, _ in mod + rem + ins) | set(changed_idx)) + first_message_only = touched and max(touched) == 0 + types_changed = {t for _, t in mod} | {t for _, t in rem} | {t for _, t in ins} + only_media = bool(types_changed) and types_changed <= MEDIA_TYPES + blocks_carried = sum(len(x["msg"]["content"]) for x in A) + roles_touched = {A[i]["msg"]["role"] for i in touched} + + def user_text_removed(entry): + i, t = entry + return t == "text" and A[i]["msg"]["role"] == "user" + + def later_user_turns(i): + return sum(1 for x in A[i + 1:] if x["msg"]["role"] == "user") + + pattern = "unknown" + if kind == "blocks_modified": + if types_changed <= {"image_url", "document_url"}: + pattern = "image_url_resigned" + elif types_changed <= {"tool_result"}: + pattern = "tool_results_rewritten" + elif types_changed <= {"tool_use", "server_tool_use", "mcp_tool_use"}: + pattern = "tool_use_rewritten" + elif roles_touched == {"system"}: + pattern = "system_block_rerendered" + elif only_media: + pattern = "media_stripped" + elif n_mod >= 4 and ((n_mod * 2 >= blocks_carried and len(types_changed) > 1) or n_mod >= blocks_carried): + pattern = "reserialized" + elif first_message_only: + pattern = "first_message_rewritten" + elif kind == "blocks_removed": + if n_rem == 1: + i, t = rem[0] + if user_text_removed(rem[0]) and later_user_turns(i) <= 1 and i != 0: + pattern = "reminder_stripped" + elif only_media: + pattern = "media_stripped" + elif first_message_only: + pattern = "first_message_rewritten" + elif user_text_removed(rem[0]): + pattern = "history_block_stripped" + else: + if roles_touched == {"system"}: + pattern = "system_blocks_stripped" + elif only_media: + pattern = "media_stripped" + elif first_message_only: + pattern = "first_message_rewritten" + elif kind == "blocks_inserted": + if n_ins == 1 and ins[0][1] in ("text",) and ins[0][0] != 0: + pattern = "block_inserted" + elif any(t == "compaction" for _, t in ins): + pattern = "compaction_summary" + elif first_message_only: + pattern = "first_message_rewritten" + else: + if only_media: + pattern = "media_stripped" + elif first_message_only: + pattern = "first_message_rewritten" + return {"kind": kind, "pattern": pattern, "notes": notes, "changed_validated": first_path, + "position": first_pos, "items_modified": n_mod, "items_removed": n_rem, "items_inserted": n_ins, + "messages_delta": 0, "first_changed_msg": min(touched), "first_changed_wire": first_wire, "reading": "in-place"} + + +def _classify_runs(A, B, ops): + notes = [] + deletes = [op for op in ops if op[0] == "delete"] + replaces = [op for op in ops if op[0] == "replace"] + inserts = [op for op in ops if op[0] == "insert"] + first_i1 = min(op[1] for op in ops) + for op, i1, i2, j1, j2 in ops: + if op == "delete": + notes.append("messages[%d..%d] removed (%s)" % (i1, i2 - 1, ", ".join(A[q]["msg"]["role"] for q in range(i1, i2)))) + elif op == "insert": + notes.append("%d message(s) inserted before messages[%d] (%s)" % (j2 - j1, i1, ", ".join(B[q]["msg"]["role"] for q in range(j1, j2)))) + else: + notes.append("messages[%d..%d] replaced by %d message(s): %s" % (i1, i2 - 1, j2 - j1, excerpt(fp(B[j1]["msg"]), 0, 60))) + removed_msgs = sum(i2 - i1 for op, i1, i2, j1, j2 in ops if op != "insert") + inserted_msgs = sum(j2 - j1 for op, i1, i2, j1, j2 in ops if op != "delete") + items_removed = sum(1 + len(A[q]["msg"]["content"]) for op, i1, i2, j1, j2 in ops if op != "insert" for q in range(i1, i2)) + items_inserted = sum(1 + len(B[q]["msg"]["content"]) for op, i1, i2, j1, j2 in ops if op != "delete" for q in range(j1, j2)) + res = {"notes": notes, "items_removed": items_removed, "items_inserted": items_inserted, "items_modified": 0, + "messages_delta": inserted_msgs - removed_msgs, "first_changed_msg": first_i1, "reading": "runs"} + removed_types = {block_type(b) for op, i1, i2, j1, j2 in ops if op != "insert" for q in range(i1, i2) for b in A[q]["msg"]["content"]} + only_media = bool(removed_types) and removed_types <= MEDIA_TYPES and not inserts + if len(deletes) == 1 and not replaces and not inserts: + op, i1, i2, j1, j2 = deletes[0] + res["kind"] = "blocks_removed" + if i1 == 0 and (i2 - i1) >= 1: + res.update(pattern="rolling_truncation", changed_validated="messages.0", position="before") + elif all(A[q]["msg"]["role"] == "system" for q in range(i1, i2)) and (i2 - i1) >= 2: + res.update(pattern="system_blocks_stripped", changed_validated="messages.%d" % (i1 - 1), position="after") + elif only_media: + res.update(pattern="media_stripped", changed_validated="messages.%d" % (i1 - 1), position="after") + elif (i2 - i1) >= 2 and (i2 - i1) >= 2 * i1: + res.update(pattern="tail_kept", changed_validated="messages.%d" % (i1 - 1), position="after") + else: + res.update(pattern="unknown", changed_validated="messages.%d" % (i1 - 1), position="after") + return res + if len(replaces) == 1 and not deletes and not inserts: + op, i1, i2, j1, j2 = replaces[0] + has_compaction = any(isinstance(b, dict) and b.get("type") == "compaction" for q in range(j1, j2) for b in B[q]["msg"]["content"]) + res["kind"] = "blocks_replaced" + if has_compaction or (items_removed >= 8 and items_removed >= 2 * items_inserted and (j2 - j1) < (i2 - i1)): + res.update(pattern="compaction_summary", changed_validated="messages.%d" % j1, position="at") + elif (j2 - j1) < (i2 - i1): + res.update(pattern="unknown", changed_validated="messages.%d" % j1, position="at", + note="shorter run in place, but fewer than 8 items removed: the API names this compaction_summary only past that size") + res["notes"].append(res.pop("note")) + else: + res.update(pattern="unknown", changed_validated="messages.%d" % j1, position="at") + return res + if inserts and not deletes and not replaces: + res.update(kind="blocks_inserted", pattern="unknown", changed_validated="messages.%d" % inserts[0][3], position="at") + return res + res.update(kind="many_changes" if len(ops) > 2 else "blocks_replaced", pattern="unknown", + changed_validated="messages.%d" % first_i1, position="at") + return res + + +def diff_messages(A, B): + """A, B: canonical message lists (request N, request N+1). Returns a dict describing the + messages-section verdict, or None when A is an unchanged prefix of B (chain warnings aside).""" + fa = [fp(x["msg"]) for x in A] + fb = [fp(x["msg"]) for x in B] + res = {"notes": [], "thinking_notes": []} + + # thinking-chain check over the shared region: the API requires every kept thinking block + # after the first to have been minted right after the kept block now in front of it, so the + # kept blocks must be a contiguous window of the original sequence (any window). A missing + # predecessor of a kept block, or a reorder, fails the block after the gap. + shared = min(len(A), len(B)) + seq_a = [(i, s) for i in range(shared) for _w, s in A[i]["sigs"] if fa[i] == fb[i]] + seq_b = [(i, s) for i in range(shared) for _w, s in B[i]["sigs"] if fa[i] == fb[i]] + sa = [s for _i, s in seq_a] + sb = [s for _i, s in seq_b] + later = [(i, s) for i in range(shared, len(B)) for _w, s in B[i]["sigs"]] # blocks minted after request N + if sb and sa != sb and later and [s for s in sb if s in sa] and [s for s in sb if s in sa][-1] != sa[-1]: + res["thinking_notes"].append( + "the newest already-sent thinking block (messages[%d]) was removed while a later block (messages[%d]) remains: " + "that later block was minted right after the removed one, so its recorded predecessor is gone (predecessor_missing)" + % (seq_a[-1][0], later[0][0])) + elif sb and sa != sb: + kept = [s for s in sb if s in sa] + new_in_b = [s for s in sb if s not in sa] + idx = [sa.index(s) for s in kept] + ok = (not new_in_b) and idx == sorted(idx) and (not idx or idx == list(range(idx[0], idx[0] + len(idx)))) + if not ok: + if idx != sorted(idx): + what = "re-sent in a different order (predecessor_reordered)" + elif new_in_b: + what = "re-sent with thinking blocks that were not in the earlier request" + else: + gaps = [sa[q] for q in range(idx[0], idx[-1]) if q not in idx] + first_gap = next((i for i, s in seq_a if s in gaps), None) + what = "re-sent with a thinking block removed from the MIDDLE of the kept run (first gap in messages[%s]); the kept block after the gap fails the predecessor check (predecessor_missing)" % first_gap + res["thinking_notes"].append( + "thinking blocks in the already-sent turns %s: the API accepts any contiguous window of the original " + "blocks (drop from the front, from the back, or both) and nothing else" % what) + + # a thinking block replayed with different text than the earlier request sent under the same signature + texts_a = {s: t for x in A for s, t in x.get("texts", {}).items()} + for i, x in enumerate(B): + for s, t in x.get("texts", {}).items(): + if s in texts_a and texts_a[s] != t: + res["thinking_notes"].append( + "the thinking text of the block in messages[%d] differs from the earlier request that carried the same signature: " + "the API rejects a modified thinking block with a 400 (truncated, summarized or re-wrapped thinking is an edit)" % i) + break + + if len(B) >= len(A) and fb[:len(A)] == fa: + return None if not res["thinking_notes"] else {"pattern": None, "kind": None, **res} + + # branch / regenerate / retry: the shared head is intact up to the divergence and no replayed + # thinking block sits at or after it -> nothing the API checks has changed + k = 0 + while k < min(len(A), len(B)) and fa[k] == fb[k]: + k += 1 + if all(i < k for i, _w in thinking_positions(B)): + res.update({"kind": None, "pattern": None, "branch": True, "divergence": k}) + res["notes"].append("tail replaced from messages[%d] on, after the last replayed thinking block -- " + "nothing the API checks has changed (a compaction that replays no earlier thinking, a branch, a regenerate, or a retry)" % k) + return res + + # two readings: in place (same positions, some messages differ) vs runs removed/replaced + in_place = None + if len(B) >= len(A): + changed_idx = [i for i in range(len(A)) if fa[i] != fb[i]] + in_place = _classify_in_place(A, B, changed_idx) + sm = difflib.SequenceMatcher(a=fa, b=fb, autojunk=False) + ops = [list(op) for op in sm.get_opcodes() if op[0] != "equal"] + if ops and ops[-1][0] == "replace" and ops[-1][2] == len(fa) and (ops[-1][4] - ops[-1][3]) > (ops[-1][2] - ops[-1][1]): + op, i1, i2, j1, j2 = ops[-1] # A's tail replaced by a longer B tail = edit + new turns + ops[-1] = ["replace", i1, i2, j1, j1 + (i2 - i1)] + if ops and ops[-1][0] == "insert" and ops[-1][2] == len(fa): + ops = ops[:-1] # the newly appended turns are expected + runs = _classify_runs(A, B, [tuple(op) for op in ops]) if ops else None + if in_place is not None and runs is not None: + # server-style item costs: a changed message counts its own item plus its changed blocks + cost_in_place = (in_place["items_modified"] + in_place["items_removed"] + in_place["items_inserted"] + + len([i for i in range(len(A)) if fa[i] != fb[i]])) + cost_runs = runs["items_removed"] + runs["items_inserted"] + chosen = in_place if cost_in_place <= cost_runs else runs + else: + chosen = in_place or runs + if chosen is None: + return None + res.update(chosen) + return res + + +def thinking_positions(B): + """(msg_index, wire_block_index) of every thinking block in request N+1.""" + out = [] + for i, x in enumerate(B): + for w, _s in x["sigs"]: + out.append((i, w)) + return out + + +def _model_key(model): + """Dated aliases of one model (claude-x-5-1 and claude-x-5-1-20260901, or -latest) read each other's blocks; + compare on the undated name so they count as the same model here.""" + return re.sub(r"-(\d{8}|latest)$", "", model or "") + + +def compaction_start(messages): + """Index of the message holding the last compaction block with non-null content, or 0. The API compares the + messages from that block on (with server-side compaction the checked prefix restarts there); everything + before it is outside the check, so a client that drops those messages has not edited anything.""" + start = None + for i, m in enumerate(messages or []): + c = m.get("content") if isinstance(m, dict) else None + if isinstance(c, list): + for b in c: + if isinstance(b, dict) and b.get("type") == "compaction" and b.get("content") is not None: + start = i + return start + + +def _has_signed_block(message): + """True when a message holds a compaction block with a signature and a summary (the compact-2026-09-04 kind).""" + c = message.get("content") if isinstance(message, dict) else None + return isinstance(c, list) and any(isinstance(b, dict) and b.get("type") == "compaction" and b.get("signature") + and b.get("content") is not None for b in c) + + +def _signed_block_alone(messages): + """True when messages[0] is a compaction block signed with a summary (the compact-2026-09-04 block) sent as a message + of its own. Before it, compaction blocks with null content and fallback blocks are skipped; after it, a pinned MCP + listing (an mcp_tool_listing block) and null-content compaction blocks are skipped. Anything else returns False, and + diff_pair falls back to the compaction-boundary comparison (nothing before the block is compared).""" + first = (messages or [None])[0] + c = first.get("content") if isinstance(first, dict) else None + if not isinstance(c, list) or not c or not all(isinstance(b, dict) for b in c): + return False + signed = next((i for i, b in enumerate(c) if b.get("type") == "compaction" and b.get("signature") + and b.get("content") is not None), None) + if signed is None: + return False + ignored = lambda b: b.get("type") == "compaction" and b.get("content") is None + return (all(ignored(b) or b.get("type") == "fallback" for b in c[:signed]) + and all(ignored(b) or b.get("type") == "mcp_tool_listing" for b in c[signed + 1:])) + + +def _signed_blocks(messages): + """Positions ("messages[i].content[j]") of every compaction block signed with a summary, in order as sent.""" + out = [] + for i, m in enumerate(messages or []): + c = m.get("content") if isinstance(m, dict) else None + for j, b in enumerate(c if isinstance(c, list) else []): + if isinstance(b, dict) and b.get("type") == "compaction" and b.get("signature") and b.get("content") is not None: + out.append("messages[%d].content[%d]" % (i, j)) + return out + + +def _carries_tool_changes(messages): + first = (messages or [None])[0] + c = first.get("content") if isinstance(first, dict) else None + return any(isinstance(b, dict) and b.get("type") == "compaction" and "tool_changes" in b + for b in (c if isinstance(c, list) else [])) + + +def _kept_alignment(A_all, B_msgs): + """For a later request whose messages[0] holds a compaction block the earlier request lacks: the index in the + earlier request's messages that lines up with messages[1] of the later request, or None. The anchor is a thinking + signature: a kept assistant message whose block the earlier request also carries fixes the offset (signatures are + unique, so this is never a coincidence, where equal message bytes - a repeated "continue" - could be); with no such + anchor this is the older shape (the block arrived in the reply and everything behind it is new) and None is + returned. Several anchors vote by how many kept messages they line up byte for byte, the later offset on a tie. + A kept message that was edited lines up all the same and the edit is then reported. Called only for a signed block + sent as a message of its own (_signed_block_alone).""" + fa = [fp(x["msg"]) for x in A_all] + fb = [fp(x["msg"]) for x in B_msgs] + sig_at = {} + for j, x in enumerate(A_all): + for _w, s in x["sigs"]: + if s: # an unsigned block anchors nothing: "" is not unique + sig_at.setdefault(s, j) + best, best_score = None, -1 + for k in range(1, len(B_msgs)): + for _w, s in B_msgs[k]["sigs"]: + j = sig_at.get(s) if s else None + if j is None or j - (k - 1) < 0: + continue + offset = j - (k - 1) + score = sum(1 for q in range(1, len(fb)) if offset + q - 1 < len(fa) and fa[offset + q - 1] == fb[q]) + if score > best_score or (score == best_score and offset > best): + best, best_score = offset, score + return best + + +def from_compaction(messages): + """The messages the check compares: from the last applied compaction block's message on. + Returns (messages, start index or None when there is no compaction block).""" + s = compaction_start(messages) + return ((messages or [])[s:] if s is not None else (messages or [])), s + + +def _signed_signature(message): + """The signature of the first compaction block with a summary in a canonical message's content, or None.""" + c = message.get("content") if isinstance(message, dict) else None + return next((b.get("signature") for b in (c if isinstance(c, list) else []) + if isinstance(b, dict) and b.get("type") == "compaction" and b.get("signature") and b.get("content") is not None), None) + + +def _pick_compaction_request(pending, A_all, kept_at): + """Which of the pending compaction requests the adopting request took its block from: one whose messages are the + first messages of the earlier request (or the earlier request's messages plus the reply), and, when a kept thinking + block fixes the dropped range (kept_at), whose message count equals it; the latest such request wins. Returns + (label, body, n) or (None, None, 0).""" + tied = [] + a_fp = [fp(x["msg"]) for x in A_all] + for label, body in pending or []: + C_msgs = canon_messages(body.get("messages")) if isinstance(body.get("messages"), list) else [] + n = len(C_msgs) + c_fp = [fp(x["msg"]) for x in C_msgs] + # the request carried the first n messages of the earlier request, or every message of it plus the reply and + # any tool results added before the compaction was sent: either way the block replaces a prefix of the earlier + # request + if n and a_fp and (a_fp[:n] == c_fp or c_fp[:len(a_fp)] == a_fp): + tied.append((label, body, n)) + if kept_at is not None: + exact = [t for t in tied if t[2] == kept_at] + if exact: + return exact[-1] + return tied[-1] if tied else (None, None, 0) + + +def diff_pair(reqA, reqB, mint_models=None, compaction_requests=None): + """mint_models: optional {signature: model id that produced the block}, built by run_diff from the + capture order. Used only to separate blocks minted by the model now being called from blocks + minted by another model, which are judged by the model check rather than this prefix comparison. + compaction_requests: the captured compaction requests (beta compact-2026-09-04) not yet adopted, as (label, body), + in capture order. When the later request adopts a block, the API checks the kept turns against the system prompt + and tools of the compaction request that produced it, not the previous turn's, so that request becomes the + reference side, and the adopting request must have dropped exactly the messages it carried. The result then + carries adopted_compaction=True and reference_request=