Running agents

An agent run can take a minute or more, so it runs in the background. Starting one answers at once with the run, and its steps are written as it goes, so a screen or a script follows it step by step. It can be stopped at any time, it can stop to ask the person it acts for a question and wait for the answer, and one agent can run over many objects at once as a batch. What an agent is and how it reasons is on agents overview; this page is about running it

Background202 at once, or the finished run within up to 60 s
Live traceEach step written within a second, and before every model call
Questions1 to 5 at a time; the run waits, holding no worker
BatchesOne run per object, up to 1,000 objects

In the platform today

Built
  • Background runs, stopping, questions, batches and checks on tool results run in the platform service
  • Runs are queued and run in the background
  • Every run is a row in ops.agent_runs from the moment it is queued, and every step a row in ops.agent_steps
  • Evaluations and runs on a data branch run to the end within the request, and are written as they go too

Start a run

POST /api/v2/agents/agt-milk/runs?wait=10
{
  "input": "Silo 3 is at 61 h of its 72 h limit",
  "context": { "siloId": "SIL-3" }
}
FieldWhat it does
inputThe request in words
contextKeys and facts the agent needs, such as the object it is about
modeauto (the default) uses the model and falls back to the playbook; playbook runs the playbook only
parentA run of the same agent this one runs again with changes. GET /api/v2/agents/{id}/runs/{run}/compare?with={other run} puts the two side by side
?wait=Seconds to wait for the run to end, 0 to 60. 0 by default

The call answers 202 with the run while it is queued or running, and 200 with the run once it is past that, when it gets there within wait. The run carries its id, status, answer, proposals, steps, tool calls, tokens and cost

Follow a run

GET /api/v2/agents/agt-milk/runs/run_01JBC4…/trace?after=6

Returns the run with only the steps after step 6, so a client polls with the last step it has. live is true while the run is queued or running. Each step has its kind, tool, input, output, status, time taken, tokens and cost, and a span and parent that give the call tree, as described on reasoning traces

A run writes its new steps at least once a second, after every 10 steps and before every model call, so what happened so far shows while a model call is under way. Instead of polling, the runs and runs:<run id> topics of GET /api/v2/stream send a short event when a run's status or counters change and for each step written. Only the person the run acts for and administrators get them, and the step itself is read from the trace

EndpointDoes
GET /api/v2/agents/{id}/runs/{run}One run without its trace
GET /api/v2/agents/{id}/runs/{run}/trace?after=The run with its steps, or only those after a step number
GET /api/v2/agents/{id}/runsThe agent's recent runs, 50 at most
GET /api/v2/agent-runs?status=&agent=&mine=&limit=Runs across agents, newest first. status takes a comma list, such as queued,running; limit is 50 by default and 500 at most
GET /api/v2/agent-runs/{run}One run by its id alone, without its trace, as a notification links to it

A run's trace holds what tools returned under the access of the person it acts for, so only that person and administrators read it. GET /api/v2/agent-runs lists the caller's own runs, or everyone's for an administrator unless mine=1

Stop a run

POST /api/v2/agents/agt-milk/runs/run_01JBC4…:cancel

Stops a queued, running or waiting run before its next model, tool or playbook step. A call already under way finishes and is counted. Proposals the run already made stay, each waiting for a person as always. Only the person the run acts for or an administrator can stop it, and a run that has already ended answers 409 RUN_FINISHED. A stop sent to one copy of the service reaches a run on another within a second. POST …/runs/{run}/cancel does the same

Run states

StatusMeans
queuedWaiting for a worker
runningA worker is running it
waitingIt asked the person questions and waits for the answers, holding no worker
completedIt finished with an answer
limit_reachedIt used its step limit, or a team its step limit, without finishing
cost_limit_reachedA team reached its costLimitCad
blockedA check at level: block did not pass
refusedThe model declined the request
failedAn error ended it, such as no model and no playbook to fall back on
cancelledSomeone stopped it, named in cancelled_by
interruptedThe copy of the service running it stopped. It is not run again on its own, so nothing it proposed is proposed twice
expiredNo answer came to its questions in the time allowed

A run whose copy of the service stops ends interrupted within a few minutes, and a waiting run past its time ends expired

Questions to the person

An agent with ask: true gets an ask_person tool. With it, the agent asks the person it acts for one to five questions in one call, for what is not in the data: a preference, a judgement, a fact from the site. The run then stops with status waiting and frees its worker. Ashgrove's project assessment asks the superintendent how to optimize before it writes any scenario

agents:
  - id: project-assessment
    ask: true                     # offers ask_person to the model
    askTimeoutHours: 24           # default 24, set per implementation

A playbook asks the same way with an ask step, whose questions can come from a function. Each question has an id (a letter, then letters, digits, - or _), its text, a kind, and optionally options (20 at most), a default and why, shown under the question

KindAnswer
choiceOne of the options
choicesA list of options, which may be empty
textText, 2000 characters at most
numberA number
dateA date, YYYY-MM-DD

While it waits, the run shows its questions and ask_expires_at. Where it stopped, the conversation so far and what tools and team members returned, is kept on its row encrypted, never returned by the API, and dropped when the run goes on, ends or expires. A run not started through the API, such as one an automation or a batch started, notifies the person it acts for in their inbox

POST /api/v2/agents/project-assessment/runs/run_01JBC7…:answer
{ "answers": { "optimize": "schedule" }, "note": "The owner wants the milestone held" }
  • The person the run acts for or an administrator answers. The answers are checked against the questions, a missing one takes its default, and a wrong one is refused (400 INVALID_ANSWERS)
  • The call answers 202 and the run goes on in the background from where it stopped. The model gets the answers as the result of its ask_person call; a playbook reads them as {{answers.<id>}}
  • Two answers at the same moment can't both resume it: the second gets 409 RUN_NOT_WAITING. If the run can't be handed to a worker after an answer is accepted, it goes back to waiting
  • Unanswered, it ends expired after askTimeoutHours. :cancel ends a waiting run at once
  • In an evaluation no one can answer: the model is told so and goes on with its best assumption, and a playbook ends at its ask step

Checks on tool results

An agent's checks judge a tool's result as the run goes. A check counts a list in the result, or reads a number in it, and compares it with a minimum. Ashgrove's project assessment wants at least two lessons from past projects before it relies on its guidance

checks:
  - id: precedent
    name: Enough precedent
    after: find_guidance                # the tool whose result it checks
    # a list (its length) or a number; by default the result itself
    count: lessons
    min: 2
    level: warn                         # warn (the default) or block
    call:
      agent: precedent-search
      input: "Past projects with a problem like this one: {{input.text}}"
  • Each check is a step of its own, kind check, with its score (found over needed, at most 1), its rationale and its attempts. where counts only list items with given values
  • One that falls short at level: warn marks the step with a warning and the run goes on. At level: block it stops the run as blocked
  • call runs a second agent first, as a run of its own linked to this one, and the check keeps its answer. Its templates read {{input.*}}, {{args.*}} (the tool's input) and {{output.*}}
  • The model gets the tool's result as {result, checks} when a check applied

Batches: one run per object

A batch runs one agent over many objects, one run each. The cold chain agent can look at every piece of equipment whose health score is under 60

POST /api/v2/agents/agt-cold/batches
{
  "type": "equipment",
  "where": { "health": { "$lt": 60 } },
  "input": "Check {{object.name}}: health {{object.health}}",
  "context": { "equipmentId": "{{object._key}}" },
  "parallel": 2,
  "costLimitCad": 1.50
}
FieldWhat it doesDefault
typeThe object type, which the person must be able to seeRequired
whereA filter in the syntax of object setsEvery object
keysA list of object keys, instead of or with where
inputThe request for each object, with {{object.<property>}}Required
contextContext for each run, with the same templates. Each run also gets objectType and objectKey
modeauto or playbookauto
parallelRuns at once, at most half of the runs working at once on a copy4, so 2 with the default workers
costLimitCadStops the batch once its runs have spent this much on modelsNone
  • The batch runs over the objects the person can see, read as them, and each run acts for them
  • The call answers 202 at once with the batch, or 200 when no object matched and the batch is already completed
  • All batches on a copy share at most half of its workers, so people's own runs always get one
  • A batch's runs have ids starting brun_ and source batch, and a question one of them asks reaches the person's inbox

Follow, stop and review a batch

EndpointDoes
GET /api/v2/agent-batches/{batch}Progress: runs by status and not started, done, cost so far, proposals waiting and the time left at the pace so far
GET /api/v2/agent-batches/{batch}/runs?status=&limit=&offset=Each object's run with its answer and proposals, 200 by default and 1,000 at most per page
GET /api/v2/agent-batches?status=&mine=&limit=Batches, newest first: the caller's own, or everyone's for an administrator unless mine=1
POST /api/v2/agent-batches/{batch}:cancelRuns not started never start, runs going stop before their next step, and waiting runs end

A batch ends completed, cancelled, cost_limit_reached, failed, or interrupted when its copy of the service stopped, in which case the objects not started are not run. While running it is running, or cancelling once stopped

A batch's proposals are grouped rather than listed one by one, so hundreds of them don't bury the rest. GET /api/v2/proposals returns batched, how many each batch has for the reader; ?batch={batch} lists one batch's and includeBatches=1 lists everything. Who may read or decide a proposal does not change

From an automation

An automation with an agent effect queues one run as the automation's owner, with the triggering object in its context, and does not wait for it. When the queue is full it waits up to 5 minutes for room, then counts as a failed attempt. Automation runs have their own share of the queue, the per-person limit per owner, so they never use up the owner's own runs. With forEach, the effect starts a batch instead

effect:
  kind: agent
  agent: agt-cold
  forEach:
    type: equipment
    where: { health: { $lt: 60 } }
    parallel: 2
    costLimitCad: 1.50
  input: "Check {{object.name}}: health {{object.health}}"
  context: { equipmentId: "{{object._key}}" }

forEach takes type, where, parallel and costLimitCad. The batch runs in the background without holding the automation timer, and the automation run records the batch id and the number of objects. Each run's context also names the automation

Limits

Limits apply per copy of the service, and each default is set per implementation. Past them the request is refused with 429 and a reason, safe to retry. The full list is on limits and quotas

LimitDefaultRefusal
Runs working at once4
Runs waiting for a worker50AGENT_QUEUE_FULL
Runs queued or running for one person3AGENT_PER_PERSON
How long a run waits for answers, unless the agent sets askTimeoutHours24Ends expired
Batches running10BATCHES_FULL
Batches running for one person3BATCHES_PER_PERSON
Objects in one batch1,000BATCH_TOO_LARGE (400)

A run waiting for answers holds no worker and does not count against the queue. The step limit is set per agent, 16 by default and 64 at most, as on agents overview

Limitations

  • A run interrupted by a stopped copy of the service is not resumed; a person sees what it did and starts it again
  • A step already under way when a run is stopped finishes and is counted
  • A single agent or a team's lead can ask questions; one ask_person call per turn holds every question
  • A batch's cost limit is checked before each run is handed out, so runs already going can take it a little past the limit before they stop

Related