Agent
The core entry point of Agent Express.
An Agent wraps a language model with middleware-based lifecycle hooks.
Create an agent, add middleware with .use(), and run it with .run().
Example
Section titled “Example”const agent = new Agent({ name: "assistant", model: "anthropic/claude-sonnet-4-6", instructions: "You are a helpful assistant.",})
// Multi-turnawait agent.init()const session = agent.session()const r = await session.run("Hello!").resultawait session.close()await agent.dispose()
// Convenience one-linerconst { text } = await agent.run("Hello!").resultConstructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Agent(
def):Agent
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Agent
Properties
Section titled “Properties”
readonlyname:string
Agent name used for debugging and tracing.
Methods
Section titled “Methods”[asyncDispose]()
Section titled “[asyncDispose]()”[asyncDispose]():
Promise<void>
Alias for dispose() — enables await using agent = new Agent(...).
Returns
Section titled “Returns”Promise<void>
dispose()
Section titled “dispose()”dispose():
Promise<void>
Dispose the agent: auto-closes open sessions, then unwinds the agent onion triggering cleanup in all middleware (reverse registration order). Idempotent — safe to call on an uninitialized agent.
Returns
Section titled “Returns”Promise<void>
init()
Section titled “init()”init():
Promise<void>
Explicitly initialize the agent: resolve model, run agent middleware (connect MCP servers, register tools, etc.). Idempotent.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”await agent.init() // MCP servers connect, tools registerrun(
input,opts?):AgentRun
Convenience: auto-init + create session + single turn + close session.
Parameters
Section titled “Parameters”string
User message text
Optional run options (output schema)
Returns
Section titled “Returns”AgentRun (dual interface: streaming + result promise)
Example
Section titled “Example”const { text } = await agent.run("Hello!").resultsession()
Section titled “session()”session(
opts?):Session
Create a new session for multi-turn conversation. Auto-initializes the agent if not already initialized.
Parameters
Section titled “Parameters”Optional session configuration (custom ID for persistence)
Returns
Section titled “Returns”A Session object for executing turns
Call Signature
Section titled “Call Signature”use(
middleware):this
Register middleware on this agent. Chainable.
Accepts a Middleware object, an array of middleware, a plain function
(treated as a turn hook), or a scope + function pair.
Parameters
Section titled “Parameters”middleware
Section titled “middleware”Returns
Section titled “Returns”this
this agent (for chaining)
Call Signature
Section titled “Call Signature”use(
middlewares):this
Register middleware on this agent. Chainable.
Accepts a Middleware object, an array of middleware, a plain function
(treated as a turn hook), or a scope + function pair.
Parameters
Section titled “Parameters”middlewares
Section titled “middlewares”Returns
Section titled “Returns”this
this agent (for chaining)
Call Signature
Section titled “Call Signature”use(
fn):this
Register middleware on this agent. Chainable.
Accepts a Middleware object, an array of middleware, a plain function
(treated as a turn hook), or a scope + function pair.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”this
this agent (for chaining)
Call Signature
Section titled “Call Signature”use<
S>(scope,fn):this
Register middleware on this agent. Chainable.
Accepts a Middleware object, an array of middleware, a plain function
(treated as a turn hook), or a scope + function pair.
Type Parameters
Section titled “Type Parameters”S extends HookScope
Parameters
Section titled “Parameters”S
ScopeHookFn[S]
Returns
Section titled “Returns”this
this agent (for chaining)