Skip to main content

Call the agent from another system

A script, a scheduled job or a workflow product can hand work to the agent the way a person does in the web app: start a task, give it files, ask for the result, wait, and collect the files it wrote. This page shows that flow with curl and jq; any HTTP client works the same way. Your first API call explains the basics (the token, the address, Idempotency-Key and events) and is the place to start.

Not yet run on a real install. The commands on this page are written from the API's description (openapi.json) and have not been run against an installation yet. Each block says so until it has been.

The calls act as the person whose token they use: the task is that person's, it appears in their sidebar, and it runs with their role, their keys and the installation's limits. Use an account made for the system if the work should not be one person's.

# Not yet run on a real install.
export TQA_URL=https://agent.example.com
export TQA_TOKEN=tqa_pat_... # from Settings › API tokens; keep it in the system's secret store
auth=(-H "Authorization: Bearer $TQA_TOKEN")

1. Choose the agent profile​

An agent profile fixes what the task is: its instructions, model route, environment, tools, skills and limits. A system that does the same kind of work every time should use one, so that the work is set up once, in the web app, and not in the script. List the profiles the token's owner can use, with whether each is usable on this installation now:

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/profiles" "${auth[@]}" | jq -r '.items[] | "\(.id) \(.name) usable=\(.usable)"'
PROFILE=<the profile's id>

2. Start a task from it​

With a profile that sets a model route, the task needs only a title. Without one, add "model_route" as in your first API call. A task gets a new empty workspace unless you name one with "workspace_id".

# Not yet run on a real install.
TASK=$(curl -sS -X POST "$TQA_URL/api/v1/conversations" "${auth[@]}" \
-H "Content-Type: application/json" -H "Idempotency-Key: $(uuidgen)" \
-d "{\"title\": \"Monthly sales report\", \"profile\": \"$PROFILE\"}" | jq -r .id)

A profile that names a route, environment or connector the installation no longer offers is refused with 409 profile_unusable, which names the field.

3. Upload the input files​

Each file is the body of one request, with its name in name. It is stored in the workspace's uploads/ folder under a safe version of that name (an existing name gets -2, -3; nothing is overwritten), and the answer's path is what the message names. A file may be up to 25 MiB by default.

# Not yet run on a real install.
INPUT=$(curl -sS -X POST "$TQA_URL/api/v1/conversations/$TASK/uploads?name=sales.csv" "${auth[@]}" \
-H "Content-Type: application/octet-stream" --data-binary @sales.csv | jq -r .path)
echo "$INPUT" # uploads/sales.csv

4. Send the request​

Say what you want and where the result should go, and list the uploaded files in attachments: each one's path as the upload answered it (uploads/…), at most 32, each once.

# Not yet run on a real install.
jq -n --arg input "$INPUT" '{
text: "Make the monthly sales report from the attached CSV. Write it to report/summary.md and the totals to report/totals.csv.",
attachments: [$input]
}' | curl -sS -X POST "$TQA_URL/api/v1/conversations/$TASK/messages" "${auth[@]}" \
-H "Content-Type: application/json" -H "Idempotency-Key: $(uuidgen)" -d @- | jq

5. Wait for the run's end​

Read the task's events until the run ends. run.started carries the run's id, which step 6 uses; the run ends with run.completed, run.failed (with a code and a reason) or run.cancelled. A task may wait for a free sandbox before its run starts.

# Not yet run on a real install.
AFTER=0; RUN=""; END=""
while [ -z "$END" ]; do
PAGE=$(curl -sS "$TQA_URL/api/v1/conversations/$TASK/events?after=$AFTER&limit=500" "${auth[@]}")
EVENTS=$(echo "$PAGE" | jq -c '.items[] | select(.type == "event") | .event')
RUN=${RUN:-$(echo "$EVENTS" | jq -r 'select(.type == "run.started") | .run_id' | head -n 1)}
END=$(echo "$EVENTS" | jq -r 'select(.type | IN("run.completed", "run.failed", "run.cancelled")) | .type' | head -n 1)
AFTER=$(echo "$PAGE" | jq -r --arg after "$AFTER" '.next_after // ($after | tonumber)')
[ -z "$END" ] && sleep 5
done
echo "$RUN ended: $END"

A system that keeps a connection open can stream the events instead (?stream=1&protocol=1, server-sent events, see your first API call). Set a deadline of your own as well: stop waiting after it, and stop the run with POST /api/v1/conversations/$TASK/runs/$RUN/cancel if the result is no longer needed.

6. Collect the result files​

The files the run added, changed or deleted, compared with the workspace before the message (uploads are left out):

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/conversations/$TASK/runs/$RUN/changes" "${auth[@]}" \
| jq -r '.files[] | "\(.status) \(.path)"'

Or list a folder of the workspace (path relative to its root; leave it out for the root):

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/conversations/$TASK/files?path=report" "${auth[@]}" \
| jq -r '.items[] | "\(.kind) \(.name) \(.size)"'

Download each file you need:

# Not yet run on a real install.
curl -sS -f "$TQA_URL/api/v1/conversations/$TASK/files/download?path=report/summary.md" "${auth[@]}" -o summary.md

The agent's own answer, the text it wrote in the task, is its text.delta events in order, as in your first API call.

After the run​

  • Ask for more: send another message to the same task; it starts a new run on the same files.
  • Keep or delete: the task and its workspace stay until someone deletes them. Deleting the task (DELETE /api/v1/conversations/$TASK) removes it and its history but keeps the workspace and its files; delete the workspace (DELETE /api/v1/workspaces/{id}) to remove those (Deleting tasks and workspaces).
  • The next job on the same files: start the next task with "workspace_id" set to this task's workspace (its workspace_id in GET /api/v1/conversations/$TASK).

Every operation, with its fields and refusals, is in the API reference.