A coding agent is the hungriest client of an LLM fleet: long sessions, tools, background grading. Here is how I connect agents (using the pi harness as an example) to a hybrid fleet through any-router — so that one address serves chat, tools and decision calls.
The guiding principle from practice: the agent stack has one LLM config
point — a host + an API token. No separate addresses per slot, no private
proxies. All the variety is expressed by the provider/model form in requests.
A provider in models.json#
pi is configured with models.json (the agent directory): the provider
points at the router, apiKey comes from an environment variable (no literal
in the file):
{
"$schema": "https://pi.dev/schemas/models.schema.json",
"providers": {
"my-router": {
"baseUrl": "$YOUR_HOST/v1",
"api": "openai-completions",
"apiKey": "${MY_ROUTER_KEY}",
"models": [
{ "id": "ollama/glm-5.3-flash:cloud", "reasoning": true },
{ "id": "llamacpp/Qwen3.8-27B", "input": ["text", "image"] }
]
}
}
}
What matters:
provider/modelis copied straight from the router'sGET /v1/models— it is the aggregated list of all providers with ready-made ids;- the model entries in
models.jsondescribe capabilities (reasoning, image input): the router reports caps in the list as well, but for the agent an explicit record is a reliable contract; - apiKey with
${VAR}interpolation — pi substitutes the value at request time, and the file is fit to be committed to private storage without a secret inside.
The agent's decision channel#
Besides the main chat model, agents need background gradings: «is the task
done», «which type of task», «rewrite the review politely». In pi these are
available as decision models (the classifier type) and are called from
codemode scripts:
// state — the agent's current task state, questions — typed
const gate = await models.getModelOfType("classifier", "my-router", "relay/nimble:9b");
const result = await models.classify(gate, {
state: { task: "fix flaky test", progress: "2/3 green" },
questions: {
approved: {
type: "bool",
instructions: "Is the work accepted?",
criteria: { true: "All green and no regressions", false: "There are problems" },
},
},
});
return result.answers; // + probabilities and usage
From the client's point of view it is the same Bearer token and the same
/v1: models.classify lands on POST /v1/systemone, and the router
decides itself — whether the model is natively a decision model or a chat
model with a fallback via structured output (see
Decision models and embeddings).
The right move is to pick small fast models for the decision channel:
there are many gates per session, and both cost and latency grow linearly.
Tokens: least privileges per agent#
I issue one API token per stack (agent, web chat, benchmark), with a scope as a model list:
tokens:
- id: agent-main
token: "<secret>"
scope: models
models: ["ollama/*", "llamacpp/*"] # the whole fleet except the relay
- id: agent-gate
token: "<secret>"
scope: models
models: ["relay/nimble:9b"] # the decision channel only
Practice:
- a separate token per role — revoking one compromise does not drop the rest;
- a scoped token does not reveal the provider list (an unknown one gets an
honest 403), and its
/v1/modelsis filtered by permissions — the agent sees nothing extra; - the admin token is never handed out at all: config mutations happen locally, the admin panel is not published;
- agent secrets — in environment variables or a secret store, not in repo files.
The local part: a llama.cpp router#
For the local model pi talks to llama.cpp (the stand is built per the
local inference article): an
OpenAI-compatible /v1, vision via the mmproj projector, thinking
controlled per-request. The awkward gguf-path-in-model-id moment is solved
at the router level — auto-aliases look human-readable like
llamacpp/Qwen3.8-27B.
When the local stand and the cloud are needed simultaneously — point the agent's provider not at llama.cpp directly but at a router balance alias (a bare name): round-robin with conversation stickiness and failover walks the session between members itself.
More practices#
- Long sessions on cloud relays — cap the context window at the agent itself: a 1M model context does not mean all of it is useful (or cheap); in practice — cache and trim history.
X-Session-Idon balance-alias requests — stickiness works even for «lonely» first turns, and the upstream prompt cache is reused.- Timeouts for a cold start: llama.cpp loads 13 GiB of weights for minutes — client retries must survive that (nginx timeouts too).
- Observability: in Langfuse an agent session trace filters by
key:<agent token>andmodel:<alias>— «where the context went» is visible before analyzing request bodies.
Where to continue#
- Hybrid inference: a single endpoint via any-router — the fleet and token config;
- Decision models and embeddings via any-router — non-chat calls and their contract;
- Local inference: Qwen3.8-27B on llama.cpp — the local model stand.
- promo and binaries — any-router.e7.by.