# Routing overview

Settings → Model services → Routing (beside Providers in the page header) shows which session and branch talks to which model, and what happened when a model failed. Only the failover switches are collected for it; the rest is read from existing records.

## Sources

`GET /api/routing/overview?window=<seconds>` (`openprogram/interfaces/server/routes/settings/routing.py`) joins:

| Source | Gives |
| --- | --- |
| Session index (`list_sessions`) | Title, `source` / `channel`, `running` status of the current turn |
| Session node graph (`list_branches`, `SessionNodeWriter.load`) | Every open branch tip, the model of its last reply, whether the tip starts at a sub-agent spawn root |
| Usage ledger `usage_events` | Provider calls per session and model in the window; a `parent_session_id` naming another session marks a child session |
| Usage ledger `route_events` | One row per failover switch: calling session, failed `provider/model`, next `provider/model`, reason |
| Credential store | Each account of a provider in use: status, end of its rate-limit cooldown |

- **Listed when**: The turn is running, the session was updated in the window, or the ledger has a call for it there
- **Limits**: 8 sessions, running first, then latest activity; the rest count as idle. 4 branches per session, active first

## Lanes

A session's rows are lanes, each wired to one model.

| Lane | Is |
| --- | --- |
| `branch` | An open branch tip. The longest chain is the trunk, labelled `main`; others use their branch name. |
| `sub_agent` | A tip whose chain starts at a sub-agent spawn root |
| `children` | Child sessions grouped by the model they called, with a count |
| `calls` | A session with ledger calls but no reply node (a program calling models directly) |
| `background` | Ledger calls no listed session accounts for: program runtimes with their own `op-…` ids, titling, memory upkeep. Grouped by model under one Background calls entry, so the map adds up to the call count above it. |

Live: the active tip of a running turn, or a ledger call in the last 90 seconds.

**Keeping the request fast**

Opening a session reads its node graph file by file. Every read gives up the GIL and waits to get it back while other server threads are busy, so a cold open can take seconds.

- A session and its lanes are cached per `(session, index updated_at)`; an unchanged session is never reopened.
- Cold sessions open on one background thread; the request waits at most 1.5 s.
- A session not ready by then is shown from its index row and ledger calls. The response carries `partial: true` and the client asks again two seconds later.

## Failover switches

`stream_with_failover` appends a `route_events` row, tagged with the session from the usage context, each time it moves to the next candidate. When a session's latest switch landed on a lane's model, the map draws a dashed edge from the active lane to the failed model, marks that model with the reason and lists recent switches below.

Switches exist only with fallback models configured. A failure shown to the user without a switch is not recorded here.

## Client

| Aspect | Behaviour |
| --- | --- |
| Component | `apps/web/components/settings/providers/routing-overview.tsx`: two HTML columns, an SVG layer behind them |
| Edges | End points measured from rendered rows after layout and on resize, so names can be any length |
| Changing the window | Cancels the previous request; shown content dims until the new window arrives |
| Motion | Live edges carry a moving dot; hovering a lane or model keeps its edges and dims the rest |
| Below 560 px of pane width | The map is hidden; the pane asks the user to collapse the sidebars or widen the window |
