Before you start
You needcurl, jq, uuidgen, and an API credential with permission to manage agents, conversations, and runs, plus project.runtime.manage to register the worker. Set PROJECT to your project reference and PROJECT_ID to the project’s UUID.
Create the agent definition in the project first. Set AGENT_ID to its ID. The definition names the agent; your worker executes it.
1. Register the worker
CallPOST /agent-workers/projects/{project_reference}/workers. Choose an implementation_kind that names your runtime and a stable worker_key for this process:
register-worker.sh
2. Store the user message
A worker run starts from a persisted user message. Create a conversation with one. Omittype: the API creates a chat conversation, which is the type for top-level runs.
create-conversation.sh
POST /conversations/{conversation_id}/messages before you start the run.
3. Start the run on your worker
CallPOST /runs with conversation_id and config.agent_admission. Set mode to worker and implementation_kind to the value your worker registered with:
start-run.sh
202 and holds it for a matching worker. Add worker_key to agent_admission to pin the run to one worker.
execution.runtime overrides don’t apply to agents. Use agent_admission instead.
4. Claim the run
PollPOST /agent-workers/workers/{worker_id}/claim with the worker token:
claim.sh
run is null when nothing is waiting, so the loop polls until a run arrives. An HTTP error stops the loop and prints the response body. The claimed run includes the conversation and message IDs, so your code can read the user message. Renew the lease with POST /agent-workers/workers/{worker_id}/runs/{run_id}/lease while the run takes longer than the lease. That path takes the worker run ID (WORKER_RUN_ID), not canonical_run_id; the Runs endpoints below use RUN_ID. A renewal that returns run: null means the lease is gone.
5. Append events
Append events withPOST /runs/{run_id}/events and the run_event_token. API keys can’t append events.
append-events.sh
RUN_STARTED and RUN_FINISHED itself.
6. Finish the run
CallPOST /runs/{run_id}/succeed with the run credentials:
succeed.sh
Idempotency-Key header; reuse the same key when you retry. Call POST /runs/{run_id}/fail instead when your code fails.
7. Verify the result
Read the run withGET /runs/{run_id} and its events with GET /runs/{run_id}/events:
verify.sh
status is completed and output holds the result you sent. Studio shows the run and its events like any hosted run.
Python example
The same loop with the OpenAI SDK, calling the AI gateway: Install the dependencies and export the worker credentials from step 1 and a model ID:setup.sh
python worker.py:
worker.py
LEASE_SECONDS. For longer work, renew the lease on a timer until the run ends. If the lease expires, another worker can claim the run and your succeed call fails.
The AI gateway doesn’t accept the run’s auth_token, so the example calls it with VERYFRONT_API_KEY. Gateway usage is billed to the API key’s account and isn’t attributed to the run.
Read the user message from the conversation in the claimed run instead of PROMPT in production code.
API references
Next steps
- Inspect logs and traces for the recorded run.
- Run an agent on the hosted runtime instead.