Aquaregia

Errors and Production

Handle failures, retries, cancellation, and operational metadata.

Production LLM calls need clear boundaries around time, retries, cancellation, and error reporting.

Aquaregia returns one error type from provider calls, agent runs, streams, structured output, and embeddings: aquaregia::Error.

Handle errors by code

use aquaregia::{ChatRequest, ErrorCode, providers::openai};

let client = openai::Client::from_env()?;

match client
    .generate(ChatRequest::from_prompt("gpt-5.5", "hello"))
    .await
{
    Ok(response) => println!("{}", response.output_text),
    Err(err) => match err.code {
        ErrorCode::AuthFailed => eprintln!("check API credentials"),
        ErrorCode::RateLimited => eprintln!("retry later"),
        ErrorCode::Cancelled => eprintln!("request was cancelled"),
        ErrorCode::UnsupportedOperation => eprintln!("provider does not support this operation"),
        _ => eprintln!("request failed: {err}"),
    },
}

Error also carries provider metadata when available:

FieldMeaning
codestable Aquaregia error category
messagehuman-readable diagnostic
providerprovider slug such as openai or anthropic
statusHTTP status when the error came from a provider response
retryablewhether retrying may succeed
request_idprovider request id when exposed in headers
raw_bodybest-effort provider error body
retry_after_secsRetry-After value when present

Retry and timeout policy

Configure retries and request timeout on the provider client.

use aquaregia::providers::openai;
use std::time::Duration;

let client = openai::Client::builder()
    .api_key_from_env("OPENAI_API_KEY")
    .timeout(Duration::from_secs(45))
    .max_retries(2)
    .build()?;

Retryable error codes are:

Error codeTypical cause
RateLimitedprovider returned HTTP 429
ProviderServerErrorprovider returned HTTP 5xx
Transportnetwork or DNS failure
Timeoutconfigured timeout elapsed

Authentication, validation, unsupported operation, invalid tool arguments, and cancellation errors are not retryable by default.

Cancel work the caller no longer needs

Attach a CancellationToken to a direct request:

use aquaregia::{ChatRequest, providers::openai};
use tokio_util::sync::CancellationToken;

let token = CancellationToken::new();

let req = ChatRequest::builder("gpt-5.5")
    .user("Write a long report.")
    .cancellation_token(token.clone())
    .build()?;

token.cancel();

let result = openai::Client::from_env()?.generate(req).await;

Attach the same kind of token to an agent when a whole tool loop should stop.

let agent = client
    .agent("gpt-5.5")
    .cancellation_token(token.clone())
    .build()?;

Cancellation is checked before HTTP sends, while reading streams, and between agent steps.

Bound agent loops

Always set a step cap for tool-using agents unless you have a specific reason not to.

let agent = client
    .agent("gpt-5.5")
    .tool(search_docs())
    .max_steps(6)
    .build()?;

If the model keeps requesting tools after the cap, the run fails with ErrorCode::MaxStepsExceeded.

Tool failure policy

By default, a failing tool becomes an error-shaped tool result and the agent can recover.

use aquaregia::ToolErrorPolicy;

let agent = client
    .agent("gpt-5.5")
    .tool(read_file())
    .tool_error_policy(ToolErrorPolicy::FailFast)
    .build()?;

Use FailFast when a tool failure means the whole application operation should stop.

Error code reference

CodeMeaning
InvalidRequestinvalid model, messages, sampling values, schema, or request shape
AuthFailedmissing, empty, invalid, or unauthorized credentials
RateLimitedprovider rate limit
ProviderServerErrorprovider 5xx response
Transportnetwork or transport failure
Timeoutrequest timeout
StreamProtocolmalformed or incomplete streaming payload
UnknownToolmodel requested a tool that was not registered
InvalidToolArgsmodel produced tool arguments that did not match the schema
ToolExecutionFaileda Rust tool returned an execution error
MaxStepsExceededagent exceeded its tool-loop step cap
InvalidResponseprovider response could not be parsed or normalized
Cancelledcaller cancelled with a token
UnsupportedOperationprovider adapter does not support the requested operation

On this page