Custom agents
A custom agent is an agent the gateway runs for you. You describe it with instructions, a model, an optional MCP toolset, optional skills and limits, and the gateway runs the tool loop: it calls the model, runs the tools the model asks for through the MCP gateway, feeds the results back and stops when the model answers or a limit is reached.
Custom agents need a Spanway enterprise license that includes the custom_agents feature. Without it every /v1/custom_agents route answers 403 and invoking a custom agent over A2A returns a JSON-RPC error with status 403. Registered A2A agents keep working without a license.
Every custom agent is also an ordinary row in the agents registry, so it shows up next to your other agents, can be called over A2A, has its own spend, and respects key and team agent grants and the kill switch.
To start from ready-made agents for support triage, incident investigation or code review, install a workflow template. It creates ordinary custom agents you can edit afterwards.
Scenario
A release manager spends an hour every Friday turning the week's merged pull requests into release notes. The team builds a custom agent with instructions on the house style, a read only repository toolset and a budget of fifty cents per run. Anyone on the team can now ask it for this week's notes from the Agents page, the run stops on its own at twelve steps or fifty cents, and the full transcript is kept in the run ledger.
Steps
-
In the Admin UI open Agents under Agentic, switch to the Custom agents tab and click New custom agent. Fill Name, Instructions and Model, pick the MCP toolset with the repository read tools, set Max steps to 12, Budget per run (USD) to 0.5 and Time limit (seconds) to 300, choose Who can see and run it, and click Save agent. The same agent can be created from the API.
curl -X POST http://localhost:4000/v1/custom_agents/new \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"name": "Release notes writer","description": "Drafts release notes from merged pull requests","instructions": "Read the merged pull requests and write short release notes grouped by area.","model": "claude-sonnet-5-5","toolset_id": "ts_01J...","limits": {"max_steps": 12, "max_run_budget": 0.5, "max_wall_seconds": 300},"visibility": "private"}' -
Open the agent from the list, type the task in the agent input box and click Run. The timeline streams each step, model answer and tool result. From the API, start the same run with your own key.
curl -X POST http://localhost:4000/v1/custom_agents/run \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"agent_id": "5b7e2c41-9d3a-4f6e-8c12-7a9b0d3e4f51", "input": "Write the notes for this week", "stream": true}' -
When the run stops, click Open in run ledger to see every model request and tool call with its cost, or call
GET /v1/custom_agents/runs/info?run_id=car_01J.... -
To let other teams call the agent from their own tools, point them at
/a2a/<agent name>/message/sendwith a key the agent is shared with.
Expected result
The create call answers 200 with the agent's agent_id, agent_name and revision. A run without stream answers 200 with a run.stopped object once the agent finishes.
{"type": "run.stopped", "run_id": "car_01J...", "stop_reason": "completed", "final_text": "Release notes for the week of ...", "steps": 4, "cost": 0.0312}
stop_reason is completed, max_steps, budget_exhausted, deadline, cancelled, killed or model_error. With stream the response is a stream of events ending in the same run.stopped event, and the Runs table on the agent's page lists the run with its steps, cost and a Run ledger link.
How a run is governed
A run always uses the key of the person who started it. The gateway adds the agent's id to that key for the run, so:
- the caller's model access, rate limits, guardrails and spend logs apply to every model call, and the caller's key, team and user budgets are checked again before every model and tool call of the run,
- every MCP tool call goes through the gateway's own authorized MCP path, so the caller's MCP server and tool grants, BYOK credentials, tool guardrails and MCP spend logs apply, and the agent's toolset narrows them further,
- a run can never do more than the caller could do directly, and never more than the agent allows.
A key that belongs to an agent cannot start a custom agent run. Runs are started by people and teams.
Write tools are off by default
A tool reaches the model only when it is in the agent's toolset and it is read-only. A tool counts as read-only when its MCP annotations say readOnlyHint: true or when the connector catalogue lists it as read-only. Tools without annotations count as write tools.
A write tool is exposed only when a proxy admin lists it in the agent's write_tools and the MCP server's own write tool setting allows it. If anyone other than a proxy admin later changes what the agent does (its instructions, prompt, model, toolset, built-in tools or skills), its write tools are switched off again and the change is in the audit log. Renaming the agent keeps them.
A tool the model asks for that is not exposed is refused before it reaches the gateway, and the refusal is sent back to the model as the tool result.
No code runs on the gateway
Custom agents never execute code, shell commands or file edits on the gateway host. They act only through model calls, MCP tools and server tools, and every one of those goes through the checks on this page. To run an agent that needs a shell or a working tree, run it on your own machine with its model calls sent to the gateway, or use the containers API for the code interpreter tool.
Who can do what
| Action | Who |
|---|---|
| Create a private agent | Any user with a role that can write, using a key that belongs to a user |
| Create a team agent | An admin of that team, or a proxy admin |
Share an agent with an organization (org) | Proxy admins |
| Enable write tools | Proxy admins |
| Change or delete an agent | Its owner, an admin of its team, or a proxy admin |
| See an agent | Its owner, members of its team for team agents, members of its organization for org agents, proxy admins and admin viewers |
| Run an agent | Anyone who can see it and whose key and team agent grants allow it |
An org agent belongs to one organization, which is stored as owner_organization_id and returned with the agent. Members of that organization can see and run it, and people in other organizations or Cloud tenants cannot. Someone counts as a member when their key belongs to the organization or their user is a member of it.
A new agent belongs to the organization of the key that creates it. When the key names no organization, it belongs to the user's organization if the user is in exactly one. A proxy admin who is in no organization, or in several with a key that names none, creates an org agent that belongs to no organization.
An org agent that belongs to no organization is visible only to proxy admins and admin viewers. That includes every org agent created before this change. Such an agent takes the organization of the next person who edits it with a key or user tied to one organization, and an agent that already belongs to an organization keeps it on every edit. The Admin UI marks these agents as visible to proxy admins only.
Proxy admins and admin viewers still see every agent on the gateway. Organization and tenant admins cannot choose org themselves, so they share an agent with their people through a team agent.
Visibility applies everywhere an agent can be reached: /v1/custom_agents/*, /v1/agents, the agent card, A2A and routing by the a2a/<agent name> model name. An agent you cannot see answers as not found, the same as one that does not exist. Admin viewers and internal user viewers can read but not create or change agents unless a custom role grants custom_agents.write.
The agents row behind a custom agent gets a generated unique name, and the fields a custom agent create or update accepts are limited to the ones below. Kill switch webhooks, headers, access groups and identity bindings cannot be set through these endpoints. Anyone who may change an agent can switch off write tools a proxy admin enabled, but only a proxy admin can switch them on.
Create an agent
curl -X POST http://localhost:4000/v1/custom_agents/new \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Release notes writer",
"description": "Drafts release notes from merged pull requests",
"instructions": "Read the merged pull requests and write short release notes grouped by area.",
"model": "claude-sonnet-5-5",
"toolset_id": "ts_01J...",
"limits": {"max_steps": 12, "max_run_budget": 0.5, "max_wall_seconds": 300},
"visibility": "private"
}'
| Field | Meaning |
|---|---|
name, description | Shown in the dashboard and on the agent card |
instructions | The system prompt |
prompt_id, prompt_version | Optional managed prompt to render each model call through, checked when the agent is saved and pinned when a run starts |
model | A model the creator's key can call. Every run checks it again against the caller's key |
toolset_id | An MCP toolset your key holds a grant on. A toolset you cannot use is refused with the same 404 as one that does not exist, and an agent keeps a toolset it already has when someone renames it. It is also written to the agent's own permissions, so the MCP gateway enforces it on every tool call. An agent without a toolset never calls the MCP gateway |
write_tools | Proxy admins only. [{"server_id": "...", "tool_name": "..."}], each one in the toolset |
builtin_tools | lens_recent_traces, lens_read_trace, lens_read_span, see Lens tools, and server_web_fetch, server_datetime, see Server tools |
skill_ids | Skills whose instructions are added to the system prompt. You can only attach skills you can read yourself, and once attached their instructions reach everyone who can run the agent. Skill code execution is not available to custom agents |
limits | max_steps (1 to 50, default 10), max_run_budget in USD (optional), max_wall_seconds (up to 900, default 300) |
visibility, owner_team_id | private, team (with owner_team_id) or org. An org agent belongs to the caller's organization, as described in who can do what |
POST /v1/custom_agents/update takes the same fields plus agent_id and replaces the agent, bumping its revision. POST /v1/custom_agents/delete takes agent_id. GET /v1/custom_agents/list and GET /v1/custom_agents/info?agent_id= return what the caller may see. Creating, changing and deleting agents writes audit rows against Spanway_CustomAgentSpec.
Run an agent
curl -X POST http://localhost:4000/v1/custom_agents/run \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"agent_id": "5b7e2c41-9d3a-4f6e-8c12-7a9b0d3e4f51", "input": "Write the notes for this week", "stream": true}'
With stream: true the response is server-sent events, one event per step: run.started, step.started, model.completed (with tokens and cost), tool.called, tool.denied and finally run.stopped. Closing the connection cancels the run. Without stream the response is the run.stopped event:
{"type": "run.stopped", "run_id": "...", "stop_reason": "completed", "final_text": "...", "steps": 4, "cost": 0.031}
stop_reason is one of completed, max_steps, budget_exhausted, killed, cancelled, deadline or model_error.
Limits
Before every model call the run checks the stop signal, the step count, the budget and the wall clock, and before every tool call it checks the stop signal, the wall clock and the budget. Each model call and tool call is also cut off when the wall clock runs out. The budget counts model spend and the configured per-call price of MCP tools, and it is checked before each call, so a run can go over max_run_budget by at most the cost of the call that crossed it. Before each call the run also reads the gateway's live spend counters for the caller's key, team and user budgets (the user budget only where it applies to the key, as in normal requests) and stops with budget_exhausted once one is used up, so a run with no max_run_budget still cannot spend past the caller's own limits by more than one call. A tool call whose arguments are not a JSON object is refused and the refusal is sent back to the model. A step runs at most 8 tool calls, the rest are refused, and each tool result is cut to 20,000 characters before the model sees it. max_run_budget is also set as the agent's max_budget_per_session, so the session budget hook applies to the run's model calls as well. When a workflow runner step runs a custom agent, the run's budget is the smaller of max_run_budget and what the step and its run may still spend, and the run is listed with the surface workflow.
Runs
GET /v1/custom_agents/runs?agent_id=&page= lists runs newest first. A run's input and transcript hold what the gateway fetched with its caller's credentials and trace scope, so only the person who started a run, proxy admins and admin viewers can list or read it. The agent's owner and team members do not see other people's runs, and this stays true after the agent is deleted. GET /v1/custom_agents/runs/info?run_id= returns a run with its events and transcript, and POST /v1/custom_agents/runs/cancel asks a running run to stop before its next model or tool call, on whichever replica runs it. The agent's owner and the admins of its team may cancel anyone's run of it, and then get back only its status. Runs are written only by the gateway, so nobody can edit a run's record. A run whose caller disconnected keeps its opening messages as the transcript, and its events show what happened after that.
Every model and MCP call of a run carries the run id as its session id, so /run_ledger/{run_id} shows the run's requests, MCP calls, guardrail decisions and spend from the spend logs, and the Lens trace with the same id when tracing is on. A run that exposes at least one write tool also writes an audit row against Spanway_CustomAgentRun.
Zero data retention, retention and data subject requests
When the caller's key, team or organization requires zero data retention through its data policy, when the policy cannot be read, or when the gateway keeps no content because store_prompts_in_spend_logs is off or turn_off_message_logging is on, the run is stored without content. Its input and answer are saved as redacted-by-spanway, its transcript is empty and its stop event keeps no detail. The caller still receives the full answer.
Set maximum_custom_agent_run_retention_period to delete runs, with their events and transcripts, older than that period. Runs never outlive the spend logs, so maximum_spend_logs_retention_period applies when it is shorter. A run deleted while it is still running stops before its next model or tool call. Erasing a user through data subject requests deletes the runs they started, and exports include them.
Over A2A
Each custom agent has an agent card at /a2a/{agent_id}/.well-known/agent-card.json and answers message/send and message/stream at /a2a/{agent_id}. Each message starts a new run with the caller's key, exactly like /v1/custom_agents/run. message/send returns an agent message whose metadata carries run_id and stop_reason when the run completes. A run that stops any other way returns a task in its final state instead, failed for a step limit, budget, deadline or model error and canceled for a cancel or kill switch, with the stop message and the same metadata, and message/stream sends working status updates and a final one with the answer. Both A2A protocol versions are served. Task methods such as tasks/get are not supported for custom agents, and the A2A contextId does not continue an earlier run.
Kill switch
POST /v1/agents/{agent_id}/kill_switch on a custom agent disables it at the gateway and blocks the keys bound to it, like for any agent. Running runs on every replica stop before their next model or tool call with stop_reason: killed, and new runs are refused with 409. A kill switch webhook is optional and is fired too when one is configured, and an audit row kill_switch_fired records the halt. Set enabled back to true with PATCH /v1/agents/{agent_id} to allow runs again.
Lens tools
Any custom agent can turn on the built-in Lens tools (lens_recent_traces, lens_read_trace and lens_read_span) through builtin_tools, for example to build an investigator that reads recent agent traces in Lens, connects failures to evidence and reports one issue worth fixing.
curl -X POST http://localhost:4000/v1/custom_agents/new \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "Lens investigator", "model": "claude-sonnet-5-5", "builtin_tools": ["lens_recent_traces", "lens_read_trace", "lens_read_span"], "limits": {"max_steps": 20, "max_run_budget": 1.0}}'
Only custom agent runs can call these tools. They are read-only and every read uses the trace scope of the person who started the run, the same scope /v1/traces gives them, so the agent never reads a trace its caller could not. They need agent tracing to be enabled. To let the agent read code too, pass a toolset_id with read-only repository tools, for example from a source control connector. The agent does not check its quoted evidence against the spans afterwards, so treat its findings as candidates.
Server tools
A custom agent can also fetch web pages and read the time with the gateway's server tools by listing server_web_fetch or server_datetime in builtin_tools. This needs a license that includes the agent_server_tools feature, and the tool must be enabled under general_settings.server_tools. Creating or changing an agent with a server tool without that feature returns 403, and listing a tool the gateway has not enabled returns 400.
A run is refused with 403 when the license no longer includes the feature or when the key and team of the person who starts it were not granted the tool, so both the agent and the caller have to allow it. Every call runs as the caller's key acting as the agent, under the run's session, with the same address checks, guardrails, spend logs and traces as a server tool call on chat completions. The server tool limits under general_settings.server_tools apply to the whole run, so a run makes at most max_tool_calls server tool calls and web_fetch.max_uses fetches in total however many steps it takes, and a call past a limit is refused and the refusal goes back to the model.
In the dashboard
The Agents page has a Custom agents tab to create agents, run them with a live timeline, stop a run, open its run ledger and see past runs. On a gateway whose license lacks custom_agents the tab shows what the feature is and how to get a license instead of an empty list and failing saves. Admin viewers and internal user viewers see the agents shared with them without the create, edit and delete buttons, and a note says a custom role with custom_agents.write lifts that. The form lists the two server tools next to the Lens tools. Without the agent_server_tools feature they are locked and say which feature unlocks them, and an agent saved earlier can still drop one. You can pick a managed prompt, skills and the built-in Lens tools. Write tool switches only appear for proxy admins, and only for tools the MCP server's own write setting allows.
Promote between environments
To move custom agents from dev to staging to prod through Git review, export them as files and apply them with a stored, approved plan. See environments and Git promotion.
Errors
| Status | What it means | Fix |
|---|---|---|
403 custom_agents license message | The license lacks the custom_agents feature, or agent_server_tools for a server tool. | Add the feature to the license or ask sales@spanway.ai. |
| 403 on create or update | Your role can read custom agents but not change them, or you picked a visibility you may not use. | Use a role with custom_agents.write, or pick Only me or a team you administer. |
| 403 "Only whoever started the run, the agent's managers or a proxy admin can cancel it" | Someone else tried to cancel a run. | Cancel from the key that started it or as a proxy admin. |
| 400 "Model ... does not exist or your key cannot call it" | The agent's model is not on the gateway or your key may not use it. | Pick a model from the Model list, which shows the models your key may call. |
| 400 "Prompt ... is not registered on the gateway" | The managed prompt id or version does not exist. | Pick the prompt again on the form, or register it first. |
| 400 "One or more skill_ids do not exist" | A skill is missing or belongs to someone else. | Attach only skills you can see on the Skills page. |
| 400 "The Lens tools need agent tracing, which is not enabled on this gateway" | The agent lists Lens tools but tracing is off. | Turn on agent tracing, or remove the Lens tools. |
| 400 "Server tools not enabled on this gateway: [...]" | A listed server tool is off in general_settings.server_tools. | Enable the tool there, or remove it from the agent. |
| 400 "Write tools need a toolset that contains them" or "Write tools must be in the agent's toolset: [...]" | A write tool was switched on without its toolset, or for a tool outside the toolset. | Pick the toolset that holds the tool, or switch the tool off. |
| 404 "Toolset ... does not exist" | The toolset is gone or not granted to your key or team. | Pick a toolset from the MCP toolset list. |
| 409 "Custom agent ... was changed by someone else; reload it and try again" | Two people saved the same agent at once. | Reload the agent and save your change again. |
| 400 "An agent's own key cannot run a custom agent; use a user or team key" | The run was started with a key bound to an agent. | Start the run with a user or team key. |
| 403 "Agent ... is not allowed for your key or team" | The key or team is not granted the agent. | Ask an admin to grant the agent, see default deny. |
| 403 "Your key cannot call the agent's model ..." | The key may not use the agent's model. | Grant the model to the key or team. |
| 403 "Server tools not enabled for your key or team: [...]" | The agent uses a server tool your key or team was not granted. | Ask an admin to grant the tool. |
| 409 "Custom agent ... is disabled" | The agent was disabled, for example by the kill switch. | Restore the agent on its page. |
| 409 "Prompt ... is not registered on the gateway" or "Toolset ... no longer exists" | Something the agent depends on was removed after it was saved. | Edit the agent and pick a prompt or toolset that exists. |
| 404 "Custom agent ... not found" | The agent does not exist or is not visible to the caller. | Check the id on the Custom agents tab and the agent's sharing. |
| 404 "Run ... not found" | The run id is wrong or the run belongs to someone else. | Read runs with the key that started them, or as an admin viewer. |
| 409 "Run ... already stopped" | The run finished before the cancel arrived. | Nothing to do, read the run's result instead. |
stop_reason max_steps, budget_exhausted or deadline | The run hit one of its limits. | Raise Max steps, Budget per run (USD) or Time limit (seconds) on the agent if the limit was too tight. |
stop_reason killed | The kill switch fired for the agent. | Restore the agent on its page once the incident is over. |
| Tool shown in amber in the timeline | The model asked for a tool that is not available to the agent, with bad arguments, or past the step's tool call limit. | Add the tool to the toolset, or tighten the instructions. |
Logs
Custom agent routes write no operator log lines of their own. A run's model and tool calls are logged like any other gateway request, and a failed save is answered with the messages above.
Audit
Every change writes an audit row. Creating, updating and deleting an agent write created, updated and deleted on Spanway_CustomAgentSpec with the agent before and after. The instructions are not copied into the row, it keeps their SHA-256 and length instead, so you can see that the system prompt changed without the audit log holding it. Starting a run writes created on Spanway_CustomAgentRun with the agent, its revision and its write tools. Cancelling a run writes updated on the run with the status going from running to cancel_requested, whoever cancels it. When someone other than the person who started a run reads its input and transcript, for example an admin viewer, it writes viewed on the run, without the content.
curl "http://localhost:4000/audit?table_name=Spanway_CustomAgentRun&action=viewed" -H "Authorization: Bearer $SPANWAY_ADMIN_KEY"
Related
- Agents page describes every control of the Custom agents tab.
- Workflow runner chains custom agents into multi step runs.
- Workflow templates install ready made custom agents.
- Run ledger shows what a run did and cost.
- Agent governance covers the kill switch and session limits.
- MCP and tool policies decide which tools an agent may call.
- Environments promote agents between gateways.