Handling outages
Providers fail in ordinary ways: a rate limit, an overloaded server, a dropped connection. Freyja reports each one as an Error once and does not hide it. Retry tries the same model again after a wait, and Fallback hands the call to a second model. Both wrap a model, so an agent built on them needs no other change.
Retrying a call
A 429 or a 5xx surfaces to you once, unless the model is wrapped in Retry, which tries the failed call again:
use freyja::{Agent, Client, EndpointPreset, Retry};
// Connects to Anthropic with the key in ANTHROPIC_API_KEY.
let client = Client::from_env(EndpointPreset::Anthropic)?;
let model = Retry::new(client, |wait| tokio::time::sleep(wait));
let agent = Agent::new(model);
Retry repeats a call when is_retryable() is true. It waits what the endpoint asked in Retry-After, or else one, two, then four seconds, over three tries in all. attempts, backoff and max_wait change those, and an endpoint asking for more than max_wait, one minute by default, gets its error returned rather than waited out. Freyja picks no async runtime, so you pass the sleep of yours.
Retry the call, not the run. An agent that fails on its fifth model call and is asked again starts over and runs every tool again, so an email tool sends twice. Behind Retry only the failed call is repeated and the run goes on from where it was.
A stream is retried only until it starts. A failure after events arrived is returned, because a retry would show the same output again and pay for the whole prompt a second time.
Timeout and Body failures are retryable but not idempotent: the endpoint may have accepted and billed a request whose reply never arrived. Bound the attempts. For anything Retry does not cover, backon and tower::retry compose with is_retryable() and retry_after() directly.
Falling back to another model
Fallback sends a call to a second model when the first is down, such as Anthropic overloaded with OpenAI as the backup, or a large model with a lite one behind it:
use freyja::{Agent, Client, EndpointPreset, Fallback, Retry};
let primary = Client::from_env(EndpointPreset::Anthropic.config().default_model("claude-sonnet-5-5"))?;
let backup = Client::from_env(EndpointPreset::OpenAi.config().default_model("gpt-5-mini"))?;
let model = Fallback::new(
Retry::new(primary, |wait| tokio::time::sleep(wait)),
Retry::new(backup, |wait| tokio::time::sleep(wait)),
);
let agent = Agent::new(model);
It falls over on what is_retryable() names, and on two errors that waiting does not cure but another endpoint does: QuotaExceeded, and an Http error of kind Connect, an endpoint that could not be reached. Any other error, such as a BadRequest, comes back from the primary without the backup being asked. When the backup fails too, its error is returned.
The same request goes to both models, so each client names its own model with default_model, and the agent sets none. A model name set with Agent::model would be sent to the backup as well, where it most likely does not exist.
Wrapped in Retry as above, the primary gets its three tries before the backup is asked. A Fallback inside a Fallback makes a chain of three. A stream falls over only until it starts, as with Retry. run.usage and cost tracking follow the model that answered, since GenerateResponse::model names it.