Skip to main content

Build an agent in code

Preview

The Autessa CDK is in preview and not yet released. Chatting with Autessa's models from your machine needs a platform update that is still rolling out. Until it reaches your company, chat says so and you can use --stub.

Write an agent in a few lines: its instructions, its model, its tools and its firewall. Test it offline, chat with it on your machine, then deploy the same code to Autessa unchanged.

The loop​

  1. init agent <name> writes a working project.
  2. test runs its test cases offline, with the model stubbed.
  3. chat talks to the agent on your machine. Autessa's models answer, and the project's tools run locally.
  4. diff shows what a deploy would change in a stage.
  5. deploy puts it on Autessa.
npx autessa-cdk init agent order-helper
cd order-helper
npx autessa-cdk test
npx autessa-cdk chat OrderHelper
npx autessa-cdk diff --stage dev
npx autessa-cdk deploy --stage dev

To install the CDK, see Autessa CDK.

What init writes​

init agent <name> makes a folder named <name> with:

  • the project config with one Agent, one Tool (tools/lookup.as) and a FirewallPolicy;
  • autessa.stages.yaml with a dev stage;
  • tests/agent.test.yaml, whose cases stub the model so they run offline;
  • a README with the next steps: test, chat, diff, deploy.

It never overwrites a file that is already there. The agent's logical ID comes from the name: order-helper becomes OrderHelper (OrderHelperAgent in Python).

The agent in a few lines​

An Agent takes four settings at the top level:

SettingWhat it is
instructionsWhat the agent does, in plain words.
modelsmall, large, or a model from your company's catalog.
toolsThe project's Tools the agent may call.
firewallA FirewallPolicy, or its settings. Settings make a policy named <agent>Firewall for you.
// autessa.config.ts
import { Agent, Project, Tool } from "@autessa/cdk";

const project = new Project({ name: "order-helper" });

const lookup = new Tool(project, "Lookup", {
source: "tools/lookup.as",
description: "Look up an order by id",
inputSchema: { type: "object", properties: { orderId: { type: "string" } } },
});

new Agent(project, "OrderHelper", {
instructions: "You help customers with their orders. Look the order up before you answer.",
model: "small",
tools: [lookup],
firewall: { policy: { mode: "ENFORCE", blockPromptInjection: true } },
});

export default project;

Any other agent setting goes in the agent passthrough (agent: { ... } in TypeScript, agent={...} in Python, .property(key, value) in Java). A key set there wins over the shorthand. memory turns on conversation memory.

Test it offline​

test runs tests/**/*.test.yaml against a local fake Autessa: no account, no network. An agent case stubs each model turn, and a stubbed reply can call tools:

- name: the agent looks the order up, then answers
run: OrderHelper
inputs: { message: "Where is my order A1001?" }
stubs:
agents:
- when: { agent: OrderHelper, turn: 1 }
reply: { toolCalls: [{ name: Lookup, arguments: { orderId: A1001 } }] }
- when: { agent: OrderHelper, turn: 2 }
reply: Order A1001 has shipped.
expect:
responseText: { $contains: shipped }
calls: { tool: { contains: [{ tool: Lookup, inputs: { orderId: A1001 } }] } }

Tests stay stubbed, so they are repeatable. Add models: autessa to a case or a file to run it on Autessa's models instead. test --before-deploy runs the checks a deploy runs first.

Chat with it on your machine​

chat <AgentLogicalId> runs the agent locally:

  • Each model turn goes to Autessa's models, with the agent's instructions, model, the conversation so far and its tools' schemas.
  • Each tool call runs on your machine, against the project's tools, and is printed as it runs.
  • The loop ends at a reply with no tool calls, or at the agent's maxIterations.

It needs one of these, set in your environment and never in project files:

  • AUTESSA_DEPLOY_KEY: a deploy key with Run agents locally (MODEL_TURN).
  • AUTESSA_API_KEY: an API key with the Run agents locally permission (AGENT_MODEL_TURN). Only a company admin can give a key this permission.

See Keys.

Turns are billed to your company like an agent run, and your company's firewall policy checks each message and reply.

OptionTypeScriptPythonMaven / Gradle
Stubbed replies, no key--stub--stubautessa.models=stub
Send one message and exit--message "text"-m "text"
Chat in a stage's tenant instead--stage dev--stage devautessa.stage=dev

If your company doesn't have Autessa's model endpoint yet, chat stops with: Autessa's model endpoint isn't available on this server yet. Use --stub to run with stubbed replies. It never falls back to stubs on its own.

Some agents only run in a tenant: ones that search a knowledge table, use an MCP server, or call built-in tools such as email or the phone keypad. For those, chat stops with: This agent uses features that run only in Autessa. Run it with --stage <name> to chat with it in that stage's tenant.

Chat with it in a stage's tenant​

chat <AgentLogicalId> --stage dev chats with the agent in that stage's tenant. Its tools, knowledge table and MCP server are the ones you deployed there, and they run in Autessa. Each tool call is printed as on your machine.

  • Deploy first. The agent's tools, table and MCP server must already be deployed to the stage. If one isn't, chat names it and stops.
  • The stage's deploy key. chat uses the stage's deploy key (its deployKeyEnv, then AUTESSA_DEPLOY_KEY). The key needs the Chat with agents from your machine permission (HARNESS_CHAT). On its own, that permission can't deploy, pull or roll back anything.
  • One agent for chats. The first time, chat creates an agent named local-harness-agent in the tenant. After that, it gives that agent the settings of whichever agent you chat with, including its firewall policy and the stage's variable values. If the tenant already has an agent with that name that chat didn't create, chat leaves it alone and asks you to rename it.
  • Nothing is deployed. Deploy history, rollback and sharing don't change.
  • Billing. Conversations are billed to the tenant like any agent conversation.

Diff and deploy​

diff --stage dev asks Autessa what the deploy would create, update or delete, and marks any change that widens access. deploy --stage dev applies it. The agent runs on Autessa exactly as written, with its tools and its firewall policy. See Commands for every flag, and Watch deployments to follow a deploy in Autessa.

To call the deployed agent from your app, use the agents package.