Agents API

Agents are project-scoped, versioned resources that package a system prompt, model, tools, fragments, and guardrails. Updating an agent creates a new immutable revision; the agent slug stays stable while current_revision advances.

Agents can be executed directly with the convenience endpoints below or embedded into workflows via agent.run (see Agents Guide).

Quick Reference

GET    /api/v1/projects/:id/agents              List agents
POST   /api/v1/projects/:id/agents              Create an agent
GET    /api/v1/projects/:id/agents/:slug        Get an agent
PATCH  /api/v1/projects/:id/agents/:slug        Update an agent
DELETE /api/v1/projects/:id/agents/:slug        Delete an agent
POST   /api/v1/projects/:id/agents/:slug/run    Run an agent
POST   /api/v1/projects/:id/agents/:slug/test   Test an agent
POST   /api/v1/projects/:id/agents/:slug/replay Replay an agent

Authentication

  • Secret key (mr_sk_*) required for CRUD, test, and replay.
  • Run accepts either a secret key or a customer bearer token.
  • When using a secret key, you can attribute usage to a customer by setting X-ModelRelay-Customer-Id or the customer_id field in the run request.

Agent Resource

Responses include an agent metadata object plus the latest version snapshot:

Field Type Description
agent.id uuid Agent ID
agent.slug string Stable agent identifier
agent.current_revision integer Latest revision number
version.revision integer Revision for this config snapshot
version.tools array Tool references (hooks, definitions, fragments)
version.fragments object Named workflow fragments (optional)

Create Agent

POST /api/v1/projects/:id/agents

Request Body

Field Type Required Description
name string Yes Display name
slug string Yes URL-safe identifier
description string No Description
system string No System prompt
model string No Default model
tools array No Tool refs (see Tool Refs)
fragments object No Fragment definitions by name
max_steps integer No Max tool steps (default 10)
max_duration_sec integer No Max run duration (default 300)
step_timeout_sec integer No Timeout per step (default 60)
tool_failure_policy string No stop, continue, or retry
tool_retry_count integer No Retries when policy is retry

Example

curl -X POST https://api.modelrelay.ai/api/v1/projects/123/agents \
  -H "Authorization: Bearer mr_sk_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Researcher",
    "slug": "researcher",
    "description": "Finds sources and summarizes them",
    "system": "You are a careful researcher.",
    "model": "claude-opus-4-5",
    "tools": [
      {"definition": {"type": "function", "function": {"name": "search", "parameters": {"type": "object", "properties": {"q": {"type": "string"}}}}}}
    ],
    "max_steps": 6,
    "tool_failure_policy": "retry",
    "tool_retry_count": 2
  }'

Update Agent

PATCH /api/v1/projects/:id/agents/:slug

Updates agent metadata and/or configuration. If configuration changes, a new immutable revision is created.

Tool Refs

Each entry in tools is one of:

  • Tool definition: Provide a definition (function, image_generation, web, code_execution, etc.).
  • Tool hook: Provide hook_id plus a definition to route execution through a tool hook.
  • Fragment tool: Provide fragment.ref that matches a fragment name in fragments. Optionally map fragment inputs with fragment.inputs using JSON pointers into the tool arguments.

Example fragment tool:

{
  "fragment": {
    "ref": "research-pipeline",
    "inputs": {
      "query": "/q",
      "filters": ""
    }
  }
}

An empty pointer ("") passes the full tool arguments JSON payload to the fragment input. If no mapping is provided, inputs default to /<inputName>.

Run Agent

POST /api/v1/projects/:id/agents/:slug/run

Runs the agent with the provided input. This endpoint is a convenience wrapper around a workflow containing a single agent.run node.

Request Body

Field Type Required Description
input array Yes Input items (messages, tool results, etc.)
options object No Runtime overrides (see Run Options)
customer_id string No Customer external ID (secret key only)

Run Options

Field Type Description
model string Model override
max_steps integer Override tool step limit
max_duration_sec integer Override max duration
step_timeout_sec integer Override step timeout
tool_failure_policy string stop, continue, or retry
tool_retry_count integer Retry count when policy is retry
stream boolean Stream run events (SSE or NDJSON)
capture_tool_io boolean Include step logs in response
dry_run boolean Error if a tool call is not mocked

Example

curl -X POST https://api.modelrelay.ai/api/v1/projects/123/agents/researcher/run \
  -H "Authorization: Bearer mr_sk_..." \
  -H "Content-Type: application/json" \
  -d '{
    "input": [
      {"type": "message", "role": "user", "content": [{"type": "text", "text": "Summarize Q4 sales."}]}
    ],
    "options": {"max_steps": 4, "capture_tool_io": true}
  }'

Response

{
  "run_id": "550e8400-e29b-41d4-a716-446655440000",
  "output": [{"type": "message", "role": "assistant", "content": [{"type": "text", "text": "..."}]}],
  "usage": {
    "total_steps": 2,
    "total_llm_calls": 2,
    "total_tool_calls": 1,
    "total_input_tokens": 120,
    "total_output_tokens": 260,
    "total_cost_cents": 5
  },
  "steps": []
}

Test Agent

POST /api/v1/projects/:id/agents/:slug/test

Runs the agent with mocked tool outputs. Useful for CI and prompt iteration.

Request Body

Field Type Required Description
input array Yes Input items
mock_tools object No Tool mock outputs by tool name
options object No Same as run options

mock_tools values can be:

  • an array (returned sequentially),
  • an object keyed by id or key in tool arguments, or
  • a single JSON value (returned once).

Replay Agent

POST /api/v1/projects/:id/agents/:slug/replay

Replays an agent using the same request format as test. Intended for deterministic reruns with mocked tool output.