Building a decision engine
A decision engine is a service a task consults while it works. Before each step it may choose the model route,
narrow the tools, run one call itself, ask for compaction or stop the run; after the model answers, it may choose
which of the model's tool calls run. Your own engine speaks this product's decision/v1 protocol: plain HTTP with
JSON, in any language. The gateway relays each question to it with the engine's key, so the agent never holds the
key or the address. This page is for whoever writes one. Setting one up is in
Decision engines, and its endpoints are in the
decision engine API.
Not yet run on a real install. The commands on this page are written from the code and the guides and have not been run against an installation yet. Each block says so until it has been.
What an engine can and cannot do
- Narrowing only. Every answer is checked against the step's limits before it takes effect: a route from the step's list, tools from the step's tools, arguments that fit the tool's schema. An engine can make a step more restricted, never wider, and an answer that breaks a limit is not partly applied: the step goes on as if there were no engine, and the task records why.
- It never sees credentials, and it cannot read the conversation unless an administrator raises its exposure (below).
- It cannot declare a run completed. It can only stop one as failed. Completion stays the model's own answer.
- After a response, it selects and does not reorder. The calls it keeps run in the model's order.
- It never stops a task by failing. An engine that is down, late or wrong costs the step nothing but the wait: the agent decides alone for that step.
- No engine ships hosted. There is a reference engine to copy (below), and a built-in adapter for a
multiple-choice service (
typed_questions_v1), which an administrator configures rather than builds.
The two consultation points
The runtime posts to the gateway, which forwards to your engine:
| Point | Path on your engine | When | Your answer |
|---|---|---|---|
before_step | POST <url>/v1/before_step | Once per step, before its first model call. The step's retries and a switch to a fallback route reuse the decision | One decision: call_model, act, compact or stop |
after_response | POST <url>/v1/after_response | After a complete model response with tool calls | select: the calls to run, by id |
An engine speaking decision/v1 is asked at both points. A call your select leaves out gets an error result saying
it was omitted, so the model sees what happened.
The request
One JSON object. Every request has:
point,run_id,step(from 1) andattempt;routes: the routes acall_modelmay name, the task's own first, then its declared fallbacks, each withname,native_tools,context_windowandmax_output;tools: the step's tools, each withname,descriptionandparameters(its JSON Schema);budget:steps_used,max_steps,output_tokens_usedandmax_output_tokens;previous_calls: the previous step's calls, each withnameand itsstatus(ok,error,cancelledorinterrupted).
An after_response request also has response_calls: the model's calls, by id and name. The exact schema is
crates/tqa-types/schema/decision-v1.json, exported from tqa_types::decision::v1.
Exposure: what your engine may see
An administrator sets each engine's exposure; every change is audited.
| Exposure | Adds to the request |
|---|---|
none (the default; Tool names only) | Nothing more: calls by id, name and status, never their arguments |
calls | The calls' arguments |
full | Also the response's visible text (response_text) and the conversation as the model sees it (transcript, at most its last 64 KiB) |
The gateway refuses a request that carries more than the engine's exposure allows, so a sandbox cannot send your
engine more than the administrator chose. The tools' descriptions and parameter schemas reach the engine under
every exposure. Design for none if you can: an engine that needs more fails its check at the lower levels.
The answer
Exactly one decision object, parsed strictly: an unknown field or value makes the answer invalid.
| Decision | Fields | What happens |
|---|---|---|
call_model | route, tools | The step calls route with only these tools. route must be one of routes; tools a subset of tools |
act | tool, arguments | The step runs this call without a model call, through the same checks as a model's call. It counts as a step. After three act steps in a row, the next is refused (act_loop) and that step calls the model. The model then sees the action as a note, never as a call it made |
compact | none | Older turns are summarised now, then the engine is asked again. A second compact in the step, or one with nothing old enough, is refused |
stop | reason (1 to 200 characters) | The run ends as failed, with the code decision_stop and your reason |
select | call_ids (at most 64) | After a response: only these calls run |
{"decision": "call_model", "route": "cheap", "tools": ["read_file", "glob", "grep"]}
Bounds and failure
- Size: each request and each answer is at most 64 KiB.
- Time: your engine has its
timeout_msto answer (2,000 by default, at most 10,000), and it counts inside the run's time. There are no retries within a step. - Circuit breaker: after three failures in a row in one run, the run stops asking the engine and records one
circuit_open. - What a task records instead of your answer (a
decision.fallbackevent, with the reason):unreachable,timeout,invalid,route_not_allowed,tool_not_allowed,schema,repeated_compact,compact_nothing,act_loop,circuit_openorbudget_spent. An applied answer is recorded as adecisionevent, with your choice and its latency. On resume, a step uses its recorded decision and does not ask again. - Budgets: an administrator may give an engine a budget of calls per total or per calendar month (UTC). Once it
is spent, the gateway refuses the consultation before it reaches your engine, and the run decides alone for the rest
of the run. Each consultation also counts against the task owner's relay rate and is recorded as usage under
decision:<name>(Cost and limits).
The reference engine
examples/decision-reference/engine.py is one Python file, standard library only, Apache-2.0, to read, run and copy.
It answers before_step with the cheaper route named by --cheap-route after a step whose calls all only read and
all succeeded, and the task's own route otherwise. It keeps every call after a response, and needs only exposure
none. Its policy is one function, choose_route; the rest is the protocol.
# Not yet run on a real install.
python3 examples/decision-reference/engine.py --port 8700 --cheap-route cheap
python3 examples/decision-reference/engine.py --self-test
--bearer-from-env ENGINE_KEY makes it require that variable's value as a bearer token. --read-only mcp.docs.search
counts another tool as read-only.
Check it with ext-check
tools/ext-check decision asks your engine what a task would ask, at both points and at each exposure level, and
checks every answer with the agent's own rules:
# Not yet run on a real install.
tools/ext-check decision http://127.0.0.1:8700 --timeout-ms 2000 --json engine-report.json
- What a result means: a pass means the agent would apply the answer. A failure names what the agent would
record instead (
route_not_allowed,tool_not_allowed,schema,invalid,unreachableortimeout). - Latency: the report gives p50 and p95, and the first consultation's on its own.
- Where the engine may run: on this machine unless you add
--network; a remote engine needshttps. - Keys:
--bearer-from-env ENGINE_KEYsends a bearer token, never printed. An engine whose key goes in another header is checked without it. - Exit status: 0 when everything passed, 1 otherwise.
Register it
An administrator adds the engine on the Decision engines settings page, or in gateway.toml, then names it in the
agent profiles that should use it (a task uses an engine only through its profile):
# Not yet run on a real install.
[[decision_engines]]
name = "reference"
display_name = "Cheaper routine steps"
url = "http://127.0.0.1:8700"
insecure_http = true # plain http, for an engine on the gateway's own machine; file only
auth = { scheme = "none" }
exposure = "none"
- Addresses and keys: anywhere but the gateway's own machine, put the engine behind
https. Its key is a shared key the engine must be allowed to use, like a connector's. - Tasks need a recent runtime: a task whose profile names an engine needs this version's runtime image (protocol revision 9); an older runtime is refused.
- An engine that goes away: while an engine is turned off or deleted, messages to the tasks that name it are refused. An engine in use cannot be deleted: turn it off instead.