> ## Documentation Index
> Fetch the complete documentation index at: https://phaseo.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Phaseo Gateway base URL: https://api.phaseo.app/v1. Send `Authorization: Bearer $PHASEO_API_KEY` with every request.
> Model IDs look like `provider/model`, for example `openai/gpt-6-astra`. Do not guess IDs: list them with `GET /v1/models` or check https://phaseo.app/models. `phaseo/free` routes to an available free model and needs no wallet balance.
> For new text integrations prefer `POST /v1/responses`. `POST /v1/chat/completions` (OpenAI format) and `POST /v1/messages` (Anthropic format) suit existing code.
> The full API contract is https://phaseo.app/docs/openapi/v1/openapi.yaml. Official SDKs: `@phaseo/sdk` (TypeScript) and `phaseo` (Python), plus Go, C#, PHP, Ruby and Rust.
> Search these docs over MCP at https://phaseo.app/docs/mcp, and install Phaseo skills with `npx skills add https://phaseo.app/docs`. If a page is wrong or out of date, report it with the MCP `submit_feedback` tool.
> Manage Phaseo accounts with the `phaseo` CLI (`npm install -g @phaseo/cli`, then `phaseo login` or `phaseo login --device-code`), the `/v1` Management API or the MCP server at `https://mcp.phaseo.app/mcp?access=manage`. Do not send users to the dashboard for account tasks. A person is still needed for sign-up, approving a sign-in, paying the first top-up on the checkout link that `phaseo credits add` returns when no card is saved, confirming each saved-card top-up from the emailed link, and account security (password, MFA, passkeys). Create API keys with `phaseo keys create --name <name> --json`; never guess one. Credit top-ups need `phaseo login --scopes billing:write` first. Task map: https://phaseo.app/docs/v1/developers/manage-from-an-agent.md.

# Events, Retries, and Errors

> Observe synchronous Rust agent runs and handle model or tool failures.

## Lifecycle events

Use `run_with_events(...)` or `continue_with_events(...)` to receive events synchronously:

```rust theme={null}
use phaseo_agent::{AgentEvent, RunOptions};

let mut on_event = |event: &AgentEvent| {
    println!(
        "{} run={} details={}",
        event.event_type,
        event.run_id,
        event.details
    );
};

let result = agent.run_with_events(
    &mut client,
    RunOptions::new("Check project health"),
    Some(&mut on_event),
)?;
```

The runtime currently emits:

* `run.started`
* `model.request.started`
* `model.response.completed`
* `tool.started`
* `tool.completed`
* `run.paused`
* `run.resumed`
* `run.completed`
* `run.stopped`

Callbacks execute on the run's thread. Keep them fast or hand work to another component.

## Model retries

```rust theme={null}
use std::time::Duration;

let definition = AgentDefinition::new(
    "resilient-agent",
    "openai/gpt-6-astra",
)
.model_retries(2, Duration::from_millis(250));
```

`max_retries` counts additional attempts after the first request. `RunStep.model_attempts` records how many attempts produced the successful model response.

## Usage

`RunResult.usage` aggregates:

* `input_tokens`
* `output_tokens`
* `cached_tokens`
* `total_tokens`
* `cost`, when the model client populates it

Each `RunStep` also contains its own usage summary, request ID, provider, model, and finish reason.

The built-in `0.1` Gateway adapter does not map Gateway `cost_nanos` or `cost_cents` into `UsageSummary.cost`. Read the Gateway response or generation record when exact spend is required.

## Errors

All runtime failures use `AgentError`:

```rust theme={null}
match agent.run(&mut client, options) {
    Ok(result) => println!("{}", result.output),
    Err(error) => eprintln!("Agent failed: {}", error.message()),
}
```

Gateway `PhaseoError` values are converted into `AgentError`. If an application needs the original Gateway status and body, call the `phaseo` client directly or implement a custom `ModelClient` that preserves those fields in its own error handling.

Local tool executor errors are returned to the model as tool results instead of ending the run immediately.

## Step limits

`AgentDefinition::max_steps(...)` sets the default model-turn limit. Override it for one run:

```rust theme={null}
let mut options = RunOptions::new("Investigate this issue");
options.max_steps = Some(4);
```

When the limit is reached, `run.status` is `stopped` and `run.stop_reason` is `max_steps:<limit>`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.