Models¶
The model gateway routes completion requests to a configured provider adapter.
arcana.models.gateway.ModelGateway
¶
ModelGateway(
connections,
*,
providers=None,
retry=None,
pricing=None,
on_cost=None,
unhealthy_cooldown=_UNHEALTHY_COOLDOWN,
)
Single entry point the Agent uses to talk to any model.
Routes provider/model_id strings to the correct adapter, pools
adapter instances per connection, retries transient failures with
exponential backoff, and emits a CostEvent per completed call.
Token usage is recorded on the session regardless. Per-call cost is
emitted only if on_cost is provided at construction; without it,
no CostEvent fires.
Usage::
async with ModelGateway(connections=ConnectionStore()) as gw:
response = await gw.complete("ollama/hermes-3", request)
# Or with a cost sink:
def record(event: CostEvent) -> None:
...
gw = ModelGateway(connections=store, on_cost=record)
Source code in packages/arcana-core/arcana/models/gateway.py
complete
async
¶
Dispatch a completion request, retrying transient errors with backoff.
Source code in packages/arcana-core/arcana/models/gateway.py
stream
async
¶
Stream a response as ModelChunk deltas.
Retry applies only before the first token arrives — mid-stream failures
are surfaced immediately since output cannot be cleanly replayed.
Emits one CostEvent after the stream completes.
Source code in packages/arcana-core/arcana/models/gateway.py
health
async
¶
Check health for one model string or all cached adapters.
A successful check resets the unhealthy flag so the connection is allowed back into the request path without waiting for cooldown.
Source code in packages/arcana-core/arcana/models/gateway.py
resolve
¶
Parse provider/model_id or provider:connection_name/model_id and return a ModelConnection.
When a connection name is given, looks it up by name in ConnectionStore and raises
ValueError if it doesn't exist. Without a name, checks by provider first then falls
back to ProviderRegistry defaults so out-of-the-box usage requires no config file.
Source code in packages/arcana-core/arcana/models/gateway.py
aclose
async
¶
Close all cached adapters. Called automatically by the context manager.
Source code in packages/arcana-core/arcana/models/gateway.py
arcana.models.connection_store.ConnectionStore
¶
Reads ModelConnection records from disk and credentials from the OS keyring.
Connections are loaded lazily on first access; call reload() to invalidate
the cache if the file changes at runtime.
Usage::
store = ConnectionStore()
conn = store.get_by_provider(ModelProvider.ANTHROPIC)
key = store.get_api_key(conn.id)
Source code in packages/arcana-core/arcana/models/connection_store.py
upsert
¶
Insert conn, or replace the existing connection with the same name.