Skip to main content
The Plan class provides a fluent interface for constructing prompts. You obtain a Plan by calling agent.think() or session.think(), then chain methods to build up your prompt content before executing with .run().
Plan was previously named ThinkBuilder. The old name is still available as a deprecated type alias.

Basic Usage

Content Methods

These methods add content to your prompt.

.text(content)

Adds literal text to the prompt.

.textln(content)

Adds text with a trailing newline. Useful when building prompts incrementally.

.quote(content, label?)

Adds content wrapped in XML-style tags. Use this for user input, documents, or any content that should be clearly delimited from instructions.
The label helps the AI understand what the quoted content represents.

.code(content, language?)

Adds content as a fenced Markdown code block. Use this when including source code in your prompt.

Tool Methods

Tools allow the AI agent to call back into your code during prompt execution.

.tool(name, description, handler) - Simple Form

Register a tool that takes no input parameters.

.tool(name, description, inputSchema, handler) - With Input Schema

Register a tool with typed, validated input. Define the input shape using an interface with @JSONSchema:
The schema serves three purposes:
  1. Tells the AI how to format tool calls
  2. Validates incoming calls at runtime
  3. Provides TypeScript typing for your handler

Skill Methods

Skills let you give the agent reusable, self-contained capabilities following the Agent Skills standard. Skills support progressive disclosure — only metadata is loaded initially, with full instructions loaded on demand when the agent activates a skill.

.skill(path) - Stored Skill

Load a skill from a SKILL.md file on disk.
The file is parsed at run() time. The skill directory must contain a SKILL.md with YAML frontmatter:
Reference files and assets in the skill directory can be accessed via the read_skill_file tool.

.skill(definition) - Virtual Skill

Define a skill programmatically without filesystem artifacts.
Virtual skills can include handler functions via the tools array. These are invoked through the call_skill_tool dispatcher — they don’t appear as individual MCP tools, preserving progressive disclosure.

Skill Name Rules

Skill names must follow the Agent Skills spec:
  • 1-64 characters
  • Lowercase alphanumeric plus hyphens
  • No leading, trailing, or consecutive hyphens

Multiple Skills

You can attach multiple skills to a single prompt:

Configuration Methods

.cwd(path)

Sets the working directory for the session. This affects where the agent looks for files when using its built-in tools.

Execution

.run()

Executes the prompt and returns the typed result. This is always the final method in the chain.

.stream()

Executes the prompt and returns a ThoughtStream — a handle providing both the final typed result and an async iterable of intermediate progress events.
Execution begins eagerly when stream() is called — you don’t need to iterate to start the operation.

ThoughtStream

The ThoughtStream<Output> class provides access to streaming events during prompt execution.

Properties

.result

A Promise<Output> that resolves with the final typed result.

Async Iteration

ThoughtStream implements AsyncIterable<ThoughtEvent>, allowing you to iterate over events with for await:

Execution Semantics

The async iterator and result promise have independent lifecycles:
  • Iterate without awaiting .result — the result resolves in the background
  • Await .result without iterating — events buffer internally
  • Do both concurrently — iterate and await simultaneously
  • Early termination — break out of the loop and still await .result

ThoughtEvent Types

Events emitted during streaming are represented as a discriminated union:

thought

Streaming internal reasoning / chain-of-thought from the agent.

message

Streaming visible response text from the agent.

tool_start

Emitted when the agent starts using a tool.
The kind field indicates the category of tool: "read", "edit", "delete", "move", "search", "execute", "think", "fetch", "switch_mode", or "other".

tool_update

Emitted during tool execution with progress or intermediate content.
The content array may include:
  • { type: "content"; content: ContentBlock } — text, image, or resource link
  • { type: "diff"; path: string; oldText: string; newText: string } — file diff
  • { type: "terminal"; terminalId: string } — terminal output reference

tool_done

Emitted when a tool completes.

plan

Emitted when the agent shares its execution plan.
Each PlanEntry has:
  • content: string — description of the plan step
  • status: "pending" | "in_progress" | "completed"
  • priority: "high" | "medium" | "low"

Complete Example

Here’s a complete example showing multiple Plan features:

Method Chaining Order

While most methods can be called in any order, we recommend this sequence for readability:
  1. .think(schema) - Always first (starts the builder)
  2. .cwd(path) - Configuration
  3. .skill() - Skill attachments
  4. .text() / .textln() / .quote() / .code() - Main prompt instructions
  5. .tool() - Tool definitions
  6. .run() or .stream() - Always last (executes the prompt)