Agent.bx

The advanced option - a class that extends bx-ai's own AiAgent, for definitions that need code.

On this page

Agent.bx

Most agents use Agent.yml instead

The default way to define an agent is a plain Agent.yml. Reach for Agent.bx when the definition itself needs code: hooks, dynamic instructions, tools registered in code, an overridden method, or detectEnvironment(). A folder holds one or the other, never both. The sections on the model slug, environment overrides, merge semantics and the always-on agent route below apply to both.

Agent.bx is the advanced way to define an agent. A BxAgents project needs an Agent.yml or an Agent.bx. It extends bx-ai's own AiAgent, so it is the agent - the build instantiates it rather than rebuilding one from a config struct, so what you write is what runs. Inherit and add whatever you need: private helpers, overridden methods, tools registered in code. Because it's a real class rather than a struct-returning descriptor, an IDE can introspect it like any other BoxLang class - jump to definition, autocomplete on inherited methods, the works.

class extends="bxModules.bxai.models.runnables.AiAgent" {

	function init() {
		super.init(
			name        : "my-agent",
			description : "A helpful assistant",
			instructions: "You are a helpful assistant.",
			model       : aiModel( provider: "openai", params: { model: "gpt-5" } )
		)
		return this
	}

}

bxAgents new --class scaffolds exactly this shape (without --class, new scaffolds an Agent.yml). instructions.md is optional - set instructions directly in super.init(), or drop the file beside Agent.bx and let the build wire it in for you (see the table below).

What the build layers on top of the class

The rule: an explicitly declared convention wins; otherwise the class is in charge.

So an agent that sets everything in its own init() gets nothing imposed on it, while one that sets the bare minimum still picks up the conventions it didn't speak for. Everything below is optional - declare a configure() returning any of these keys to override what the class itself set, or the matching instructions.md/tools//subagents/ convention folder:

You declareThe build emitsIf you don't declare it
instructions.mdwithInstructions( fileRead( ... ) )the class's own instructions stand
model in configure()setModel( aiModel( ... ) )the class's own model stands
name / description in configure()setName() / setDescription()the class's own stand
memory in configure()setMemory( ... )the class's own stands
the tools/ folderwithTools( ... ) with that folder's toolsalways - withTools() appends in bx-ai rather than replacing, so tools your class registered itself are kept and the agent's own tools/ are added
tools / toolOptions in configure()withTools( ... ) with each item@module / *@module listed, built with the module's optionsno module tools; see Tools and Tool options
subAgents on the class, or subagents/ on diskaddSubAgent( ... ) per childappended the same way
checkpointer in configure()withCheckpointer( ... )injected anyway if the class set none - see below
Info

The checkpointer is the one thing the build fills in unasked. An agent reachable from a gateway with no checkpointer has silently broken human-in-the-loop, so a class that set none still gets the cache default. A class that sets its own is left alone.

Warning

Deliberately not implemented by comparing your instance against bx-ai's DEFAULT_AGENT_* values. "Did the author mean this, or is it just the default?" is unanswerable, and an author who genuinely wanted the default name would find it silently replaced. Presence of an external declaration is a fact; intent behind a default value is not.

The class is copied into the generated app at agent/classes/ and instantiated there by its own absolute file path (never a relative lookup that would depend on a registered mapping), exactly as tools/, skills/ and mcp/ are copied - so a packaged .bxa carries it, and chat/invoke/serve all instantiate it the same way whether or not a real ColdBox container is booted.

configure() (optional) - overriding what the class set

A configure() method is entirely optional. Declare it only to override specific fields from outside the class - useful for keeping deployment-specific values (a different model per environment, say) out of the class body itself:

class extends="bxModules.bxai.models.runnables.AiAgent" {

	function init() {
		super.init(
			name  : "with-mcp-servers-agent",
			model : aiModel( provider: "openai", params: { model: "gpt-5" } )
		)
		return this
	}

	function configure() {
		return {
			mcpServers : [
				"https://example.com/mcp",
				{ url : "https://other.com/mcp", name : "other" }
			]
		};
	}

}
FieldTypeNotes
namestringAlso becomes this agent's config/WireBox.bx binding key at build time (getInstance( name ), or agent( name ) in a scheduler) - see schedules/ - so it must be unique across the whole project (root + every subagent); build fails if two agents share a name.
modelstringA provider/model slug or a bare provider name. A profile (see Models and profiles) can stand in for it. See below.
descriptionstringOptional.
subAgentsarray of stringsNames of sibling folders under the root project's subagents/. See subagents/.
mcpServersarrayMCP servers - each entry a URL string, or a mapping with url (or command), name and the credentials the server wants. See mcp/.
securitystructForwarded verbatim into the generated app's bxai module settings; bx-ai's own SecurityDirector turns it into guardrail middleware. Passthrough only - BxAgents has no own guardrails convention.
memorystring or structThe agent's conversation memory. A bare string is shorthand for the type ("cache"); a struct is { type, ...config } and is passed through to aiMemory() verbatim - e.g. { type: "cache", maxMessages: 50 }, or with summaryProvider/summaryModel/summaryThreshold to make the web UI's /compact functional. Applies per node, so a subagent can declare its own.
checkpointerstruct{ type: "cache"\|"file"\|"jdbc", ...config }. Defaults to { type: "cache" } if omitted. Always applied - without one, human-in-the-loop approval flows through any gateway other than cli fail outright.
gatewaySessionstruct{ policy, maxQueueDepth, attachments, commands }, all optional (default "queue" / 50 / true / a safe set of slash commands). Only meaningful if the project has at least one push-style gateway entry - controls the generated GatewaySession's policy for a second inbound message arriving on a thread that already has a turn in flight. policy must be one of reject/queue/steer/interrupt.
agentApistruct{ enabled, access, tokenEnvVar, tokens, profiles, path, allowedIPs, rateLimit, manageTokens, embeds }, all optional. Controls the always-on agent route, at /__bxagents unless path moves it. See The always-on agent route below.
tools, toolOptionsarray, structModule tools and their options. See Tools and Tool options.
provider, params, options, providers, profiles, defaultProfile, profile, contextWindow, vision, embeddings, speech, transcription, imagevariousThe model settings, with the same shape and checks as in Agent.yml. A subagent may not set params, options, providers, profiles, defaultProfile or the four capabilities: they apply to the whole app and belong to the root. See Models and profiles.
notesstructShort notes the agent keeps. See Notes.
usagebooleanRecords what each model call used. Root only. See usage.
tuistructHow the terminal chat looks. Root only. See tui.
extensionsstructValues for the project's extensions. Root only.
any other keyanyMerged and available in the resolved config struct, but not interpreted by BxAgents itself.

The model slug

model is BxAgents' own convention - bx-ai itself takes provider and model as two separate arguments to aiModel(). BxAgents splits the slug on the first / only, so a provider that itself contains a slash (like OpenRouter's openrouter/anthropic/claude-x) still parses correctly:

model valueprovidermodel
openai/gpt-5openaigpt-5
openrouter/anthropic/claude-xopenrouteranthropic/claude-x
mock/mock-modelmockmock-model

If model has no / at all, it must be a known core provider name (the provider's default model) - validation rejects anything else. The recognized core providers are: bedrock, cartesia, claude, cloudflare, cohere, deepseek, docker, elevenlabs, gemini, grok, groq, huggingface, minimax, mistral, mock, ollama, openai, openai-compatible, openrouter, perplexity, voyage (kept in sync with bx-ai's own CORE_PROVIDERS). mock is a real provider, useful for tests and CI - it never makes a network call.

This slug-splitting convention is what a configure()-declared model string goes through - super.init()'s own model argument instead takes a real AiModel instance directly (aiModel( provider: "...", params: { model: "..." } )), since the class already speaks bx-ai's own API.

Environment overrides

Agent.bx may declare a method named after an environment (e.g. production(), development(), or any custom name) returning a struct of overrides - this works whether or not the class also declares a configure():

class extends="bxModules.bxai.models.runnables.AiAgent" {

	function init() {
		super.init(
			name        : "override-agent",
			description : "An agent with an environment override",
			model       : aiModel( provider: "openai", params: { model: "gpt-5" } )
		)
		return this
	}

	function production() {
		return {
			model : "openai/gpt-5-mini"
		};
	}

}

The active environment is resolved with this precedence (highest wins):

  1. --environment CLI flag (bxAgents build --environment=production)
  2. Agent.bx's own detectEnvironment() method, if it declares one
  3. BX_AGENTS_ENV environment variable
  4. "development" (default)

detectEnvironment()

For anything the first and third tiers can't express, Agent.bx may declare a detectEnvironment() method and decide for itself. Whatever it returns is taken as the environment name verbatim:

class extends="bxModules.bxai.models.runnables.AiAgent" {

	function init() {
		super.init( name : "my-agent", model : aiModel( provider : "openai", params : { model : "gpt-5" } ) )
		return this
	}

	function detectEnvironment() {
		return fileExists( expandPath( "/.production-marker" ) ) ? "production" : "development";
	}

}

This mirrors ColdBox's own tier-1 convention (a config declaring detectEnvironment() has its return value taken verbatim), with two deliberate differences, both because this runs at build time:

  • An explicit --environment flag outranks detectEnvironment() here, where ColdBox puts its own detect method first. A flag someone typed is the most specific statement of intent available.
  • ColdBox's other detection tier - an environments struct of regexes matched against CGI.HTTP_HOST - is not supported. It cannot work here: the build runs in a CLI process with no HTTP host, so it would only ever describe the machine doing the building (a CI runner would resolve development for a production build).

Returning a blank string, or anything that isn't a string, means "no opinion" and falls through to the next tier - so a project computing its environment from something absent on this machine degrades to the default rather than failing the build. A detectEnvironment() that throws is a different matter: that's a real bug in the project's own code, and build fails and says so.

This is a build-time decision only, distinct from ColdBox's own runtime environment detection (the generated app reads getSetting("environment") on its own, per ColdBox's environments convention) - this precedence only decides which environment() override method on Agent.bx, and which boxlang-{env}.json/miniserver-{env}.json files, the build pipeline applies.

If no method matching the active environment exists, no override is applied.

Merge semantics

The full resolution order (lowest to highest precedence) is:

  1. configure() (optional)
  2. the matching environment-override method, if any
  3. boxlang.json
  4. boxlang-{environment}.json
  5. miniserver.json
  6. miniserver-{environment}.json

Struct keys are merged recursively - a nested struct in a higher-precedence source only overrides the keys it actually sets, leaving sibling keys from a lower-precedence source intact. Arrays and all scalar values are replaced whole, never appended or concatenated.

boxlang.json/boxlang-{env}.json/miniserver.json/miniserver-{env}.json are all optional project-root JSON files, useful for config that's easier to express as data than as BoxLang code (e.g. model defaults):

// boxlang.json
{
	"modelDefaults": { "temperature": 0.7, "maxTokens": 1000 }
}
// boxlang-production.json
{
	"modelDefaults": { "temperature": 0.2 }
}

Building with --environment=production here yields modelDefaults: { temperature: 0.2, maxTokens: 1000 } - the recursive merge kept maxTokens from the base file since boxlang-production.json never mentioned it.

Warning

Secrets (API keys, tokens) are never read or merged by BxAgents at build time - they stay external (an OS environment variable, .env, a platform secret manager) and are resolved live by bx-ai itself at runtime. See Deployment & Secrets.

The always-on agent route

Every built project exposes its root agent over HTTP at a fixed reserved path, whether or not it declares any gateways/ entries:

POST /__bxagents/invoke
POST /__bxagents/stream    # SSE
POST /__bxagents/batch
GET  /__bxagents/info

They answer in the shape ColdBox's toAi() terminator gives, but they are routes of the agent API's own handler, so the same access setting covers them and every other route. (ColdBox route middleware does not reach toAi()'s routes, which is why they are not used.) The route exists so this module's own tooling always has an address to talk to - it's the service entrance, not your project's public API. A project's own public surface belongs in gateways/, at whatever paths you choose.

A route reached with a verb it does not take (a GET to /invoke) answers 405 with the usual JSON body, { "success": false, "error": "...", "code": "method_not_allowed" }, not an error page.

Run control

Beside those four, a generated handler (handlers/AgentApi.bx) serves what a chat client needs to control a run. It is what chat --server and chat --connect use:

POST /__bxagents/events    # SSE: like /stream, plus tool and suspended frames. Body may carry
                           # attachments: [ { name, mime, data (base64) } ], at most 4 pictures of 5 MB
POST /__bxagents/cancel    # { threadId, reason? }
POST /__bxagents/steer     # { threadId, input }
GET  /__bxagents/pending   # ?threadId=
POST /__bxagents/resume    # { threadId, decision } -> SSE, like /events
GET  /__bxagents/tools
GET  /__bxagents/profiles   # the profiles a client may ask for
GET  /__bxagents/agent    # name, model, what it can do
GET  /__bxagents/sessions   # the caller's recent conversations (?source=terminal for those that started there; ?all=1 lists everyone's, with whose each is, for an admin)
POST /__bxagents/sessions   # { title? } start one: { conversationId }
POST /__bxagents/sessions/rename   # { conversationId, title } name one
POST /__bxagents/sessions/fork     # { conversationId, title? } copy one, messages and all
POST /__bxagents/sessions/delete   # { conversationId } its row and the agent's messages for it
GET  /__bxagents/export    # ?conversationId=&format=md|html the conversation as a file to save
GET  /__bxagents/whoami    # who the server takes the caller to be: { id, name, scope, source, agent }
GET  /__bxagents/volumes   # the folders the agent may use: { volumes: [ { name, mode, description } ] }, never a path on the machine
GET  /__bxagents/logs      # the agent's logs, admin only: which there are, or ?area=&tail=&level=&q= for the last lines
GET  /__bxagents/gateways  # the gateways and their state, admin only: { gateways: [ { name, type, state, credentials, lastActiveAt, lastError } ] }
GET  /__bxagents/skills    # the skills the agent can load: { skills: [ { name, description, origin } ] }, origin is installed, project or agent
GET  /__bxagents/data      # where the agent keeps everything and where each volume is (admin only)
GET  /__bxagents/users     # the sign-in accounts and whether each is an admin (admin only)
POST /__bxagents/users/admin  # { username, admin }: make an account an admin or take that away (admin only)
GET  /__bxagents/preferences          # what the caller has set: { preferences : { key : value } }
POST /__bxagents/preferences/set      # { key, value }
POST /__bxagents/preferences/delete   # { key }
GET  /__bxagents/history    # ?conversationId= what was said in one
POST /__bxagents/clear      # { conversationId } forget its messages
POST /__bxagents/compact    # { conversationId } summarize the older messages
POST /__bxagents/ping       # { profile? } test a model with one tiny question: { ok, model, ms, reply } or { ok : false, model, reason }
GET  /__bxagents/commands   # the slash commands this caller may run, [ { name, hint } ]
GET  /__bxagents/context    # ?conversationId= estimated size of the context: parts, largest tools, total, share of the window
GET  /__bxagents/status     # ?conversationId= what the extensions' status items say: { items : [ { name, text } ] }
GET  /__bxagents/slots      # ?conversationId= what the extensions' slots hold: { items : [ { id, extension, place, html, text } ] }
POST /__bxagents/command    # { line : "/info", conversationId? } run one, answers { blocks, effects, conversationId }
GET  /__bxagents/notes    # the caller's notes and the agent's, with how full they are
POST /__bxagents/notes    # { scope : "user" | "agent", text }
POST /__bxagents/notes/update   # { scope, id, text }
POST /__bxagents/notes/remove   # { scope, id }

That list is the run-control and conversation core. Routines, the vault, notes and memory, skills, logs, gateways, backups, extensions, accounts and tokens have routes too: every one of the 88 is in Every route.

/events sends the frames of /stream (thread first, chunk for each piece of the answer, done last) and adds:

FrameData
tool{ id, name, arguments, status, ms }, once with status: "running" and once with "ok" and the time it took
suspended{ threadId, pendingActions : [ { toolName, toolArgs } ] } when the run waits for a human decision
error{ message }

cancel stops the run at its next checkpoint (before the next model call or tool call, not in the middle of a token). steer splices a message into a run in flight. resume takes approve, approve_session or reject and streams the rest of the turn. A run belongs to whoever started it: pending answers "nothing pending" and resume answers 403 to anyone else.

Send a conversationId with /events to keep the turn in a conversation: the server lists it under the caller (the name of the token used, or local on an open route) and the agent's memory keeps its messages under the same id. sessions, history, clear and compact work on the caller's own conversations only; someone else's id is a 404, the same as one that does not exist. They answer 404 when the server has no shared chat database, and compact reports available: false when the agent's memory has no summary model.

The notes routes answer 404 for an agent that has no notes block. Each caller reads and changes their own user notes (the owner is the name of the token they used, or local on an open route) and the agent's notes. Any caller can add to either scope or edit a note; removing a note of the agent needs an admin token (a 403 otherwise), because it changes what every user's agent knows.

These routes sit behind the same x-bxagents-token gate as the other four (a missing or wrong token is a 401), and agentApi.enabled: false removes all of them.

Answers, errors and who is calling

Every answer is JSON with success. A failure is { "success": false, "error": "text for people", "code": "machine_code" } with the status the code means:

StatusCodes
400bad_request, bad_profile, bad_attachment
401unauthorized
403forbidden, csrf, not_your_run
404not_found, no_notes
409busy, conflict
429rate_limited
500internal

On a route that is open, everyone is the one user local, on every surface, so the terminal chat, the web page and a script share conversations and notes. A caller can say which surface it is with the x-bxagents-source header (web, terminal, or any plain lower case name; api when it sends none). The conversation list records it: GET /__bxagents/sessions returns a source for each conversation, and ?source=terminal lists only those. Conversations that were made before this was recorded have a blank source.

Because the path is reserved, build rejects any gateways/ entry whose path is /__bxagents or sits underneath it. Two routes on one mount is a genuine conflict: one would silently shadow the other, and yours would be the one that never answers.

agentApi

configure() may return an agentApi struct to control it:

KeyTypeNotes
enabledbooleanDefault true. Set false to omit the route entirely - for a build going somewhere it has no business existing.
accessopen or tokenWhether callers must prove who they are. Left out, it follows the next two keys: a tokenEnvVar or tokens: true means token, nothing means open. open with either of those set, or token with neither, is a build error.
tokenEnvVarstringThe name of an environment variable holding a shared secret. When set, every route requires a matching x-bxagents-token request header.
tokensbooleanDefault false. true also accepts the access tokens made with bxAgents token create.
pathstringDefault /__bxagents. Where the whole API is served, e.g. /agent-api: a rooted path of plain segments (letters, digits, _, -, ~). Everything on this page that says /__bxagents means this path. A gateways/ entry may not sit on it. The web page, serve and chat --server follow it by themselves; chat --connect to a server that moved it needs --api-path=/agent-api.
allowedIPslistDefault none (anyone). The addresses and networks that may call at all, e.g. [ "10.0.0.0/8", "203.0.113.7", "::1" ]. It is cbSecurity's own AllowedIPs route middleware in front of every route, so anyone else gets 403 ({ "error": true, "messages": [ "Your IP address is not allowed" ] }) before any token is looked at. It checks the address of the connection, never a X-Forwarded-For header the client chooses; behind a reverse proxy every caller has the proxy's address unless you set cbSecurity's middleware.trustedProxies. /health stays open.
rateLimitstructDefault none. { requests, perSeconds }: at most requests calls from one address in each window of perSeconds seconds. It is cbSecurity's own Throttle route middleware, one counter for the whole API, kept in CacheBox. Over the limit answers 429 with a Retry-After header; every answer carries X-RateLimit-Limit and X-RateLimit-Remaining.
manageTokensbooleanDefault false. true lets an admin make, list and revoke access tokens over the API (GET/POST /__bxagents/tokens, POST /__bxagents/tokens/revoke) and shows the Tokens tab of the web app's Access page (<path>/access/tokens). Needs tokens: true and a token API; a chat token gets 403. The secret of a new token is in the answer to the create call and nowhere else. Off, the routes do not exist and tokens are made only with bxAgents token create.
embedsbooleanDefault true. false is the master switch for embeds: no embed routes, public or admin, and the Embeds page of the web app is hidden.
profileslistDefault none. The profiles a client may ask for with options.profile on /events, and what GET /__bxagents/profiles lists (never a key). Any other profile is a 400, so a caller cannot pick a model you did not offer. chat --connect uses this list for /profile. Each name must be declared in Agent.yml, or default.
function configure() {
	return {
		name     : "my-agent",
		model    : "openai/gpt-5",
		agentApi : {
			tokenEnvVar : "BXAGENTS_API_TOKEN"
		}
	};
}

Because this comes from configure(), it scopes per environment with an environment override like any other key - so a project can leave the route open locally and require a token in production:

function production() {
	return {
		agentApi : { tokenEnvVar : "BXAGENTS_API_TOKEN" }
	};
}

Stopping and steering a run

POST /cancel and POST /steer take the threadId from the first frame of the run's stream. A run belongs to the caller that started it: the server hands bx-ai an id scoped to the caller (u:<caller>:<threadId>), so another caller's threadId reaches nothing, and a thread id that is not running for you gets cancelled: false (or steered: false). There is no admin override. Resuming a paused run is different: only the caller who started it may (POST /resume), anyone else gets 403 with code: "not_your_run".

Open or token

access: open (the default when nothing else is set) asks nobody anything: everyone is the one local user. It is meant for your own machine. When serve is told to listen on an address other machines can reach (--host=0.0.0.0, say) and the API is open, it prints a warning at start:

WARNING: the agent API is open and this server listens on every network interface (port 8080). Anyone who can reach it can chat with the agent, use its tools and read its conversations. Set agentApi.access to token ...

access: token puts every route of the API behind one check, made before any action runs, by ColdBox route middleware. That includes invoke, stream, batch and info, and the same four routes of any gateway that exposes the agent (exposes: "agent"), which follow this one switch. A request that carries neither the shared secret nor an issued token is answered 401 and never reaches the agent.

The gate fails closed: if the named variable is unset or blank at runtime, nothing matches - it never degenerates into comparing "" to "" and letting every caller through. A request without a valid token gets a 401 with a small JSON body (code: unauthorized), and never reaches the agent.

Warning

tokenEnvVar names a variable, never the secret itself. build rejects a value that isn't a valid environment-variable name, precisely so a literal token can't be committed here by mistake. Only the variable's name is ever written into generated source.

The Access page in the web app: the token list and the form that makes a token

What else a client can fetch

Only the built app's public/ folder is a web root (see what is in .build). The app's own files are not under it, so the agent's settings, Agent.yml, instructions.md, the generated config and the libraries cannot be downloaded by address, with or without a token, and a class under the app is not callable by address: before the app and the web root were split, /config/agent-settings.json, /project/Agent.yml, /agent/instructions.md and a listing of /config/ answered 200 to anyone who could reach the port, token or not. What is served from public/ is the template's static files, the web page's built scripts and styles under /includes/, and the front controller index.bxm that every other address is rewritten to; an address nothing routes is a JSON 404.

Access tokens for a deployed agent

One shared secret (tokenEnvVar) is enough for a single caller. To let several people or machines in, each with a name you can revoke, turn on issued tokens:

# Agent.yml
agentApi:
  tokens: true

Make them where the agent's database is: on the server, or in the project before you pack it.

bxAgents token create --name=laptop --expires=90d
bxAgents token create --name=ci --scope=admin
bxAgents token list
bxAgents token revoke --name=laptop
  • A token is shown once, when it is made. Only its SHA-256 is stored, so a stolen database file cannot be used to call the agent.
  • The caller sends it in the x-bxagents-token header, or bxAgents chat --connect=<url> --token=<token> (or BXAGENTS_TOKEN) does.
  • The name is who is calling. A run belongs to the token that started it, and one token cannot answer another's pending approval. The scope is chat or admin.
  • Revoking takes effect on the next request, without a restart. A token can also expire (--expires=90d, 12h, 2w or a date).
  • With tokens: true and no tokenEnvVar, a server with no tokens yet refuses everyone. With both, the shared secret works as an admin token, so a fresh deployment can be reached before the first token exists.
  • The tokens live in the shared chat database (chat.db in the data home). Run token in the project, or on a deployed server with BXAGENTS_DB pointing at the file the app uses.

Put HTTPS in front of any server a token travels to: a token on plain HTTP can be read on the wire.

Info

The route is bound to whatever host the miniserver is listening on - 127.0.0.1 by default, so nothing is reachable off the machine unless you deliberately expose it. tokenEnvVar is what matters once you do, and for any built .bxa artifact you deploy.

Edit this page Download Markdown Last updated Nov 26, 2026, 8:45:51 AM Report an issue