What Is The Thread Class?
TheThread class is your application’s extension point for the underlying Durable Object that manages each conversation. By extending DurableThread, you can add custom RPC methods that are accessible from your API endpoints, tools, hooks, and other parts of your application.
The
Thread class lives in agents/Thread.ts and is automatically exported as the DurableThread binding for Cloudflare Workers.Default Implementation
When you scaffold a project, you get a minimal Thread class:DurableThread.
Built-in Methods
Your Thread class inherits these methods fromDurableThread. All methods are available as RPC calls.
Message Methods
| Method | Description |
|---|---|
getMessages(limit?, offset?, order?, includeSilent?, maxDepth?) | Retrieve message history with pagination |
sendMessage(threadId, content, role?) | Send a new message and trigger execution |
deleteMessage(messageId) | Delete a specific message |
updateMessageContent(messageId, content) | Update a message’s content |
Execution Methods
| Method | Description |
|---|---|
execute(threadId, agentId, messages?, data?) | Start agent execution with optional initial messages |
stop() | Stop current execution and mark pending messages as failed |
shouldStop() | Check if execution stop flag is set |
Log Methods
| Method | Description |
|---|---|
getLogs(limit?, offset?, order?) | Retrieve execution logs with pagination |
getLogDetails(logId) | Get detailed information for a specific log |
Thread Management
| Method | Description |
|---|---|
getThreadMeta(threadId) | Get thread metadata including agent info and stats |
updateThreadMeta(threadId, params) | Update thread metadata (agent, user_id, tags) |
deleteThread() | Permanently delete thread and all its data |
Configuration Loaders
| Method | Description |
|---|---|
loadAgent(name) | Load an agent definition by name |
loadPrompt(name) | Load a prompt definition by name |
loadModel(name) | Load a model definition by name |
getAgentNames() | List all available agent names |
getPromptNames() | List all available prompt names |
getModelNames() | List all available model names |
Registry Methods
| Method | Description |
|---|---|
tools() | Get the tools registry (auto-implemented by virtual module) |
hooks() | Get the hooks registry (auto-implemented by virtual module) |
models() | Get the models registry (auto-implemented by virtual module) |
prompts() | Get the prompts registry (auto-implemented by virtual module) |
agents() | Get the agents registry (auto-implemented by virtual module) |
Thread extends Cloudflare’s DurableObject, you also have access to this.ctx and this.env along with all other built-in Durable Object features like alarms, WebSockets, and transactional storage.
Adding Custom Methods
Extend the Thread class to add application-specific functionality:Custom methods are automatically available as RPC calls on the Durable Object instance.
Accessing Custom Methods
From API Endpoints
UsedefineThreadEndpoint to access your custom methods:
From Tools
Access the thread instance via FlowState:Available Context
Inside your Thread methods, you have access to:this.ctx
The Durable Object state, including SQLite storage:
this.env
Environment bindings configured in wrangler.jsonc:
Database Schema
The Thread’s SQLite database includes these tables:messages
| Column | Type | Description |
|---|---|---|
id | TEXT | Primary key (UUID) |
role | TEXT | ’system’, ‘user’, ‘assistant’, or ‘tool’ |
content | TEXT | Message content |
tool_calls | TEXT | JSON array of tool calls |
tool_call_id | TEXT | For tool response messages |
tool_status | TEXT | ’success’ or ‘error’ |
created_at | INTEGER | Microseconds since epoch |
status | TEXT | ’pending’, ‘completed’, or ‘failed’ |
depth | INTEGER | Nesting depth for sub-prompts |
logs
| Column | Type | Description |
|---|---|---|
id | TEXT | Primary key (UUID) |
message_id | TEXT | Associated message |
provider | TEXT | ’openai’, ‘anthropic’, etc. |
model | TEXT | Model identifier |
input_tokens | INTEGER | Prompt tokens used |
output_tokens | INTEGER | Completion tokens used |
total_tokens | INTEGER | Total tokens |
cost_total | REAL | Estimated cost |
latency_ms | INTEGER | Request latency |
created_at | INTEGER | Microseconds since epoch |
Best Practices
Keep Methods Fast
Keep Methods Fast
Durable Object methods should complete quickly. For long-running operations, consider using alarms:
Use Typed Queries
Use Typed Queries
Always type your SQL query results:
Handle Errors Gracefully
Handle Errors Gracefully
Wrap database operations in try/catch:
Document Your Methods
Document Your Methods
Add JSDoc comments for better IDE support:
Server Entry Point
Your Thread class must be exported asDurableThread from your server entry point. This is a hard requirement for the framework to function: