Daniel Salazar 679f577cd8 feat(ai): a declined Claude request reruns on a fallback chain, and its conversation stays on the model that answered (#4641)
* Rerun declined Claude requests on Anthropic's fallback model

A Claude model with safety classifiers can decline a request and end it with
stop_reason "refusal". Requests to the models that have server-side fallback
(Fable 5.2, 5.1 and 5, Opus 5.5 and 5, Sonnet 5.5) now carry
`fallbacks: "default"` and the server-side-fallback beta, so Anthropic reruns
a declined request on the model it recommends for that refusal and the caller
gets that model's answer in the same response.

- `refusalFallback: false` turns it off for a request. The Anthropic route
  takes the native `fallbacks` field instead: "default" or a list of models is
  the caller's own chain, null turns it off.
- The `fallback` content block is a `refusal_fallback` chunk in a stream and
  `refusalFallbacks` on a non-streaming result, where it is taken out of
  `message.content`. The Anthropic route writes it back as a native block and
  accepts it in replayed messages.
- `usageDetails.fallbackModel` names the model that answered.
- A final refusal is forwarded as before, without the fallback-credit fields
  of `stop_details`.
- A request Anthropic rejects over a fallback the caller did not ask for is
  sent again without it.

* Meter each Claude fallback attempt on the model that ran it

A request Anthropic reruns on a fallback model is several attempts, each
billed at its own model's rates. `usage.iterations` carries one entry per
attempt and the top-level counts describe only the attempt that answered.

- Every attempt is its own charge, priced at the rates of the model that ran
  it and recorded under that model's key: the answer from the top-level
  counts, each declined attempt from its iteration entry. Nothing is counted
  twice, and a fast request's attempts are priced at each model's fast rates.
- The reported `usage` keeps the answering attempt under the plain keys and
  adds a declined attempt's tokens under `declined_` keys. Like the advisor's,
  only their recorded cost prices them, and they are no part of the request's
  own prompt or output totals.
- A provider's `requestPricing` can name the models the upstream may rerun
  the request on. The credit gate then prices the prompt and the capped
  output once per model, each at its own rates and cost factor, for the
  affordability check, the output cap and the hold. Claude names the priciest
  target of default routing, or every model of a chain the caller named.

* Charge a declined Claude request only when Anthropic bills it

Anthropic bills a request its classifiers decline before any output only in
the bio, frontier_llm and reasoning_extraction categories; any other
category, or none, is not billed. A request declined after it produced
output is billed for its prompt and that output.

Metering follows the same rule for each attempt: the final refusal by its
`stop_details.category`, and an attempt that declined before a fallback by
the `trigger.category` of its `fallback` block. An attempt Anthropic does not
bill is recorded with its token counts at no cost. An attempt whose refusal
details are missing is charged as before.

* Estimate images inside tool results as attachments

The credit gate's prompt estimate read a `tool_result` as the text of its
serialized content, so a base64 image inside one counted as text, at many
times the tokens the image costs. An image or document block inside a tool
result is now estimated the way a top-level one is, from its payload size.
Text and other blocks in the result are counted as before.

* Name each Claude model's fallback chain, and keep a declined conversation on the model that answered

A request to a Claude model with fallback targets names them in order
(Opus 5, then Opus 4.8) instead of asking for default routing, which
leaves some refusal categories declined.

puter.js remembers the model that answered a declined turn and names it
on later turns of the same conversation, so they skip the declined
attempt. The backend runs such a request on that model and presents it
in the requested model's response format.

* Rerun a declined Claude request from the driver, one attempt at a time

A request a Claude model declines is now rerun by the chat driver on the
model's fallback models, instead of by Anthropic inside the same API call.
The request sent upstream is the same as before this branch: no `fallbacks`
field and no extra beta header.

Each rerun is a whole attempt of its own. It goes through the credit gate
against the balance left after the declined attempt, takes its own hold once
the declined attempt's hold is released, and is metered by the provider under
its own model. A request reserves one attempt at a time, as any other chat
request does. When the balance can't cover the rerun, or the rerun fails, the
response is the refusal.

A non-streamed request is rerun on any refusal. A streamed request is rerun
only when it was declined before any output, and the answer follows a
`refusal_fallback` chunk on the same stream. The response reports the model
that answered in `usageDetails.fallbackModel`, each switch in
`refusalFallbacks`, the declined attempts' tokens under `declined_` usage
keys, and the total charged in `usage.cost`. It keeps the requested model's
format.

A conversation pinned to a fallback model that declines continues down the
rest of the requested model's fallback models. The Anthropic route reads
`fallbacks: null` or `false` as an opt-out and ignores any other value.

Claude restores `puter_path` parts after a refusal, so a rerun uploads the
files again.
2026-11-14 13:04:39 -07:00
…
…

Puter.com, The Personal Cloud Computer: All your files, apps, and games in one place accessible from anywhere at any time.

The Open-Source Internet Computer!

« LIVE DEMO »

Puter.com · App Store · AI Builder · Developers · X

screenshot


Puter

Puter is an advanced, open-source, self-hostable internet computer designed to be feature-rich, fast, and highly extensible.

For Users

Puter's goal is to provide you with every app and feature you need to work, create, and play under one roof. From a simple Notepad and Voice Recorder to Spreadsheet and Camera, Puter wants to be the all-in-one solution for your digital life.

For Developers

Puter provides everything you need to build and publish web apps and games. From AI to Cloud Storage and Database to Serverless Workers, Puter has you covered. Puter also helps you get users! Once you build your app, you can publish it on our App Store to reach and monetize users.


Getting Started

💻 Local Development

git clone https://github.com/HeyPuter/puter
cd puter
npm install
npm start

→ This should launch Puter at http://puter.localhost:4100

To run this checkout with Docker, follow Building from source. Create a local docker-compose.override.yml to select the local build; keeping these settings out of docker-compose.yml avoids conflicts when pulling updates and keeps local configuration out of pull requests.


🚀 Self-Hosting

Linux/macOS

curl -fsSL https://puter.com/selfhost | sh

Windows

irm https://puter.com/selfhost?os=windows | iex

→ For more details, see Self-Hosting Puter.


☁️ Puter.com

Puter is available as a hosted service at puter.com.


Support

Connect with the maintainers and community through these channels:

We are always happy to help you with any questions you may have. Don't hesitate to ask!


License

This repository, including all its contents, sub-projects, modules, and components, is licensed under AGPL-3.0 unless explicitly stated otherwise. Third-party libraries included in this repository may be subject to their own licenses.


Translations

Languages
TypeScript 62.3%
JavaScript 34.9%
CSS 1.6%
HTML 1.1%