Upstash Documentation

How to Add a Custom Agent

2 min read

Custom agents let you bring your own agent process to an Upstash Box. The box still provides the same sandbox, filesystem, shell, git, logs, streaming, and console experience, but your code decides how to call the model and how to produce output.

Use a custom agent when the built-in Claude Code, Codex, OpenCode, or Cursor agents do not fit your workflow.

Create a Custom Agent Box#

Create the box with agent.harness: Agent.Custom and provide a customHarness command. The command runs inside the box for every box.agent.run() or box.agent.stream() call.

Agent Contract#

For each run, Box executes your command and appends these arguments:

--session is only included when a prior session exists.

Your process must write Server-Sent Events to stdout using the box-sse-v1 protocol:

Supported event names:

EventDescription
textAdds text to the visible response
thinkingEmits reasoning/thinking text
toolShows a tool call in logs
tool_resultShows a tool result in logs
doneFinishes the run successfully
errorFinishes the run with an error

SDK Helper#

If your custom agent process can import @upstash/box, use runCustomHarness() to parse Box arguments and emit the protocol events:

Minimal Anthropic Agent#

This custom agent calls Anthropic directly and streams text back through Box.

custom-anthropic-agent.mjs

Write the custom agent into the box before the first run:

Update an Existing Custom Agent#

You can update the custom agent command for an existing custom box:

This only works for boxes created with agent.harness: Agent.Custom.

Custom Harness Examples#

Ready-to-run examples for popular open-source agents:

  • Pi — multi-provider coding agent
  • Gemini — Google Gemini via @google/genai
  • Aider — git-aware code editor
  • Goose — autonomous coding agent
  • CrewAI — multi-agent orchestration framework
  • Pydantic AI — type-safe agent framework

Notes#

  • Custom agents do not use managed provider keys.
  • Pass secrets through env on Box.create() or configure them inside the box.
  • The command must be a binary name from PATH or an absolute path under /workspace/home/ or /home/boxuser/.
  • The command runs as boxuser inside the existing box sandbox.