Agent class is the main entry point for Thinkwell. It represents a connection to an AI agent (like Claude Code) and provides a fluent API for blending deterministic code with LLM-powered reasoning.
Connecting to an Agent
UseAgent.connect() to establish a connection to an AI agent. The method accepts a command string that spawns the agent process.
Using Built-in Connectors
Thinkwell provides pre-configured connector strings for popular AI agents:Using Custom Commands
You can also connect using any custom command that implements the Agent Client Protocol:Connection Options
Theconnect() method accepts optional configuration:
Ephemeral vs Persistent Sessions
Thinkwell supports two patterns for interacting with agents:Ephemeral Sessions (Single-Turn)
Useagent.think() for one-off prompts that don’t need conversation history. Each call creates an ephemeral session that is automatically closed when the prompt completes.
- Independent, self-contained tasks
- Stateless operations that don’t need context
- Parallel processing of multiple prompts
Persistent Sessions (Multi-Turn)
Useagent.createSession() for multi-turn conversations where the agent needs to remember previous interactions.
- Multi-step workflows where context matters
- Iterative refinement of results
- Conversations that build on previous responses
Code Examples
Basic Prompt with Structured Output
Using Custom Tools
Multi-Turn Session with Working Directory
API Reference
Agent.connect(command, options?)
Connects to an agent by spawning a subprocess.
Parameters:
| Parameter | Type | Description |
|---|---|---|
command | string | The command to spawn the agent process |
options | ConnectOptions | Optional connection configuration |
Promise<Agent>
ConnectOptions:
| Property | Type | Description |
|---|---|---|
env | Record<string, string> | Environment variables for the agent process |
timeout | number | Connection timeout in milliseconds |
agent.think(schema)
Creates a ThinkBuilder for constructing a single-turn prompt. Each call creates an ephemeral session that is automatically closed when the prompt completes.
Parameters:
| Parameter | Type | Description |
|---|---|---|
schema | SchemaProvider<Output> | Defines the expected output structure |
ThinkBuilder<Output>
agent.createSession(options?)
Creates a persistent session for multi-turn conversations.
Parameters:
| Parameter | Type | Description |
|---|---|---|
options | SessionOptions | Optional session configuration |
Promise<Session>
SessionOptions:
| Property | Type | Description |
|---|---|---|
cwd | string | Working directory for the session (defaults to process.cwd()) |
systemPrompt | string | System prompt for the session |
agent.close()
Closes the connection to the agent. This shuts down the conductor and invalidates any active sessions.
Returns: void
See Also
- API Overview - ThinkBuilder methods and schema providers
- Sessions - Multi-turn conversation sessions
- Connectors - Available agent connectors
