Skip to main content

Build your controller

The controller is one Python file, supervaizer_control.py. It tells Studio what your agent can show and do, and it connects Studio's calls to your agent's code.

This page builds a complete controller in six steps. Decide your missions, jobs, cases and steps first.

Let a coding agent do it

Install the Supervaizer integration skill in your coding agent, open your agent project and ask:

Integrate this agent with Supervaize.

The skill asks you the design questions, then follows this guide and the Hello World example, with tests. The rest of this page is what it reads.

1. Create the file​

pip install supervaizer
supervaizer scaffold

supervaizer scaffold writes a working supervaizer_control.py: one agent, one start form, one start action. The next steps explain each part and extend it.

2. Register the agent​

The registration declares the surfaces (screens) and the actions (commands) of your agent.

import os
from typing import Any

from supervaizer import (
Account,
Agent,
Server,
V2ActionRequest,
V2SurfaceRequest,
build_v2_agent_registration,
)

AGENT_NAME = "My Agent"

agent = Agent(
name=AGENT_NAME,
version="0.1.0",
description="Describe what your agent does.",
supervaizer_v2_registration=build_v2_agent_registration(
agent_slug="my-agent", # must equal the slugified agent name
display_name=AGENT_NAME,
a2ui_catalog_version="my-agent-ui.1", # change it when your surfaces change
surfaces=["job.start"],
actions=["job.start", "job.sync", "job.stop"],
),
)

account = Account(
workspace_id=os.getenv("SUPERVAIZE_WORKSPACE_ID") or "dummy_workspace_id",
api_key=os.getenv("SUPERVAIZE_API_KEY") or "dummy_api_key",
api_url=os.getenv("SUPERVAIZE_API_URL") or "https://app.supervaize.com",
)

sv_server = Server(agents=[agent], supervisor_account=account)

An action is job-scoped and changes state unless you say otherwise. Declare a read-only action with V2ActionDefinition(id="...", mutating=False). Studio uses this to decide who may call it.

Add a label and a description to an action or a surface when Studio shows it as a button. Both are plain text, at most 300 characters. To describe a surface, pass {"id": "job.analytics", "label": "Analytics", "description": "Charts of the job."} in surfaces.

Describe the agent​

Pass introduction and full_description to Agent, next to description:

agent = Agent(
name=AGENT_NAME,
version="0.1.0",
description="Describe what your agent does.",
introduction={
"tagline": "Turns a question into a sourced brief",
"summary": "Collects sources, drafts a synthesis and asks for review.",
"example_goals": ["Brief me on a market"],
},
full_description={"format": "markdown", "content": "# My Agent\n\nWhat it does and its limits."},
supervaizer_v2_registration=registration,
)

Studio shows introduction before any job exists. Studio shows full_description on the agent page, as markdown or html, and cleans it before display.

Allow scheduled starts​

Add scheduled_start=True to build_v2_agent_registration so that Studio can start the job on a schedule. The registration must declare job.start. Studio calls job.start with the same input as a manual start, and a stable idempotency_key for each occurrence. See Scheduled start.

3. Describe the start form​

When an operator starts a job, Studio loads the job.start surface and draws the form that your agent returns.

The start form of the Hello World agent

@sv_server.v2_surface("job.start", agent_slug=agent.slug)
def job_start_form(request: V2SurfaceRequest) -> dict[str, Any]:
return {
"surface": request.surface,
"document": {
"type": "Form",
"id": "my_agent.job.start",
"title": "Start job",
"fields": [
{"id": "goal", "label": "Goal", "type": "text", "multiline": True, "required": True},
{
"id": "mode",
"label": "Mode",
"type": "select",
"default": "fast",
"options": [{"value": "fast", "label": "Fast"}, {"value": "deep", "label": "Deep"}],
},
{
"id": "max_sources",
"label": "Maximum sources",
"type": "integer",
"default": 10,
"depends_on": {"field": "mode", "in": ["deep"]},
"validation": {"min": 1, "max": 50, "message": "Choose 1 to 50 sources"},
},
{"id": "review", "label": "Review before sending", "type": "boolean", "default": False},
],
"submit": {"action": "job.start", "label": "Start"},
},
}

Field definitions​

The same field definition is used in every form: the start form, a human-in-the-loop form, a Configure form and a resource form.

TypeInput
string, textOne line of text. With multiline: true, a text area.
integer, numberA number.
booleanA checkbox.
date, emailA date picker, an e-mail input.
select, multi_selectOne or several choices from options.
resource_refA choice from one of the agent's resources, with options_source.
fileA file upload. See Human in the loop.
listA repeatable group of item_fields.
KeyUse
id, labelRequired. The id is the key of the value that your action receives.
requiredStudio blocks the submit while the field is empty.
default, placeholderThe start value, and the hint in an empty input.
descriptionHelp text, at most 300 characters.
multiline, min_rowsA text area and its height.
read_onlyShows the value and locks it.
optionsChoices as {"value", "label"} objects. A required select has no empty choice.
options_sourceChoices loaded from a resource: {"type": "resource", "resource": "contacts", "value_field": "id", "label_field": "email"}.
depends_onShows the field only when another field has one of the listed values: {"field": "mode", "in": ["deep"]}.
validationmin_length, max_length, pattern for text; min, max for numbers; min_items, max_items for lists; message for the error text.

The SDK rejects a bad field definition in setup plans, waiting steps and resources:

  • A validation rule must fit the type: min, max for integer, number; min_length, max_length, pattern for string, text; min_items, max_items for multi_select, list.
  • min_rows needs multiline: true.
  • A list needs item_fields. An item field cannot be a list.
  • options are {"value", "label"} objects. A bare string is refused.
  • The values in depends_on are strings. Parent fields can chain.

Studio checks the fields for the operator's comfort. Your action must check the values again.

4. Start the job and return the job state​

The job.start action receives the form values in request.input. It runs your agent and returns the job state: the job, its cases and their steps.

@sv_server.v2_action("job.start", agent_slug=agent.slug)
def start_job(request: V2ActionRequest) -> dict[str, Any]:
job_id = request.job_id or "local-job"
if job_id in JOBS: # Studio can send the same start again: never run it twice
return started(JOBS[job_id])

result = run_my_agent(goal=request.input["goal"], mode=request.input.get("mode", "fast"))

JOBS[job_id] = {
"job": {
"id": job_id,
"agent_slug": request.agent_slug,
"mission_id": request.mission_id,
"status": "completed",
"source": {"type": "fresh_start"},
"information": {"cost": {"total_cost": result.cost}},
},
"cases": [
{
"id": f"{job_id}-case-1",
"lane": "work",
"title": request.input["goal"],
"status": "completed",
"steps": [
{
"id": f"{job_id}-case-1-research",
"activity": "operation",
"status": "completed",
"title": "Research",
"cost": result.cost,
}
],
}
],
}
return started(JOBS[job_id])


def started(job_state: dict[str, Any]) -> dict[str, Any]:
job = job_state["job"]
return {
"status": "ok",
"effects": [{"type": "job.started", "job_id": job["id"], "status": job["status"]}],
"job_state": job_state, # Studio reads cases and steps only from here
}

JOBS is a store that you own. Use a dictionary to begin, and your database in production.

Three rules:

  • Return the job state at the top level of the result. Studio does not read cases from an effect.
  • Make the start safe to repeat. Studio sends the same job_id and idempotency_key again after an uncertain answer. A scheduled run has a new key for each occurrence. Return the first result for a repeated key.
  • Refuse a reused key with other input. Return status: "error" with errors[0].code set to idempotency_conflict.
  • Long work does not block the answer. Return the cases as pending or active, run the work in the background, and let job.sync report the progress.

5. Sync and stop​

Studio calls job.sync to get the current state, and job.stop when an operator stops or completes the job.

@sv_server.v2_action("job.sync", agent_slug=agent.slug)
def sync_job(request: V2ActionRequest) -> dict[str, Any]:
job_state = JOBS.get(request.job_id)
result: dict[str, Any] = {"status": "ok", "effects": [{"type": "job.synced", "job_id": request.job_id}]}
if job_state:
result["job_state"] = job_state
return result


@sv_server.v2_action("job.stop", agent_slug=agent.slug)
def stop_job(request: V2ActionRequest) -> dict[str, Any]:
cancel_my_agent(request.job_id)
JOBS.pop(request.job_id, None)
return {"status": "ok", "effects": [{"type": "job.stopped", "job_id": request.job_id}]}

Studio does not poll your agent. When the work makes progress, return the new job state from the next action, or ask Studio for a sync. The Hello World file agent_rules.py shows the sync request.

6. Run and test​

Run the controller without Studio:

supervaizer start --local

Then load the start form:

curl -s http://127.0.0.1:8000/a2a \
-H "X-API-Key: local-dev" -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "1", "method": "supervaizer/surface.load",
"params": {"request_id": "r1", "actor": {"user_id": "me"}, "workspace": {"id": "local"},
"mission_id": "local-mission", "agent_slug": "my-agent",
"surface": "job.start", "input": {}}}'

To start a job, change the method to supervaizer/action.invoke and add "action": "job.start", a "job_id" and your form values in "input".

For automatic tests, set SUPERVAIZER_LOCAL_MODE=true before you import the control file. Then post the same requests to sv_server.app with FastAPI's TestClient. Local mode skips the workspace token. Without it, the request needs a signed workspace authorization header. The Hello World tests/ folder has the helpers.

Test that the start is safe to repeat. The helper calls your handler twice and fails if the second call changes anything:

from supervaizer import assert_confirm_idempotent

def test_job_start_is_idempotent(start_request): # a V2ActionRequest with an idempotency_key
assert_confirm_idempotent(start_job, start_request)

To connect the controller to your workspace, set the credentials as the Quick Start shows. On the first use in a workspace, an admin opens the agent page in Studio, gives an agent workspace ID (the name of this workspace on the agent side) and accepts the agent grant.

Guided setup​

Studio can set up a job in a chat panel. Your agent describes the setup as a plan. The operator fills in the steps, reads the cost estimate and confirms. Then Studio calls job.start.

from supervaizer import V2CostEstimate, V2SetupFieldDefinition, V2SetupPlan, V2SetupStep

plan = V2SetupPlan(
steps=[
V2SetupStep(
id="scope",
title="Scope",
fields=[V2SetupFieldDefinition(id="goal", label="Goal", type="text", multiline=True, required=True)],
help_actions=["goal.suggest"],
)
],
cost_estimate=V2CostEstimate(status="estimated", unit="USD", min=1, max=3, basis="for 10 sources"),
)

registration = build_v2_agent_registration(
agent_slug="my-agent",
display_name="My Agent",
a2ui_catalog_version="my-agent-ui.1",
surfaces=["job.start"],
actions=[
"job.start",
{"id": "goal.suggest", "kind": "help", "mutating": False, "target_field": "goal"},
{"id": "job.test", "dry_run": True, "label": "Test run"},
],
job_policy={"setup": {"plan": plan}},
)
  • job_policy.setup adds the read-only job.start.preview action. It returns the plan in setup_plan.
  • A help action has kind="help" and mutating=False. It proposes a value for the field in target_field. It receives {"field": ..., "values": {...}} and returns draft.
  • A dry-run action has dry_run=True. It can create temporary data and use model calls. It never sends external messages.
  • A cost estimate is a range or status="unknown". See Cost reporting.

The full contract is in Guided setup contract.

Add what your agent needs​

Your agentAdd
needs settings or secrets from the workspaceAgent parameters
must wait for a personHuman in the loop
has data that jobs share or operators editShared resources
costs money to runCost reporting
has effects that mission rules must controlGuardrail controls
needs setup on the job pageJob Configurator
has data worth chartingJob and mission analytics

Reference​