Overview
Threads are conversation containers that maintain the context and history of interactions with agents. Each thread belongs to a project and contains an ordered sequence of messages exchanged between users and agents.Thread Architecture
Data Model
Threads are lightweight containers with the following structure:backend/core/threads/repo.py:21-46
Thread-Project-Sandbox Relationship
Threads are organized in a hierarchy:backend/core/threads/repo.py:21-46
Message System
Threads contain messages that represent the conversation flow:Message Types
- User messages: Input from human users
- Assistant messages: Responses from the AI agent
- Tool messages: Results from tool executions
- System messages: Internal status and control messages
Message Storage
Messages are stored separately from threads for efficient querying:Thread Lifecycle
1. Creation
Threads are created when a user starts a new conversation:backend/core/threads/repo.py:174-198
2. Message Exchange
As the conversation progresses, messages are added:- User sends a message
- Agent run is created
- Agent processes the message and responds
- Tool calls are executed (if needed)
- Final response is sent
3. Updates
Thread metadata can be updated:- Rename thread
- Update visibility (public/private)
- Add custom metadata
4. Deletion
Deleting a thread cascades to remove all associated data:backend/core/threads/repo.py:137-156
Thread Search
The platform includes semantic search capabilities for finding relevant threads and messages:Vector Search
Messages are embedded and stored in a vector database for similarity search:backend/core/threads/thread_search.py
Search Features
- Semantic search: Find messages by meaning, not just keywords
- Hybrid search: Combine vector similarity with filters
- Metadata filtering: Search within specific projects or date ranges
- Ranked results: Most relevant conversations first
Thread Context Management
Context Window
Threads maintain conversation history for the LLM’s context window:- Recent messages: Most recent N messages are included
- Token limits: Automatically truncate to fit model’s context window
- Summarization: Long threads can be summarized to preserve context
Memory Systems
Threads integrate with memory systems:- Short-term: Recent messages in current thread
- Long-term: Knowledge base entries extracted from conversations
- Project memory: Shared context across all threads in a project
Thread Streaming
Thread responses are streamed in real-time using Redis Streams:Stream Key Format
Stream Events
Clients subscribe to the stream and receive events:Stream Lifecycle
- Stream created when agent run starts
- Events written as they occur
- TTL of 3600 seconds (configurable)
- Stream closed when run completes
backend/core/agents/runner/executor.py:48-189
Performance Optimizations
Pagination
Thread lists support efficient pagination:COUNT(*) OVER() to get total count without an extra query.
Reference: backend/core/threads/repo.py:16-94
Null Byte Sanitization
Thread data is sanitized to prevent PostgreSQL null byte errors:backend/core/threads/repo.py:7-14
Efficient Joins
Thread queries use LEFT JOINs to include related data in a single query:- Project information
- Sandbox details
- Total count (using window functions)
Thread Metadata
Threads support flexible metadata storage:Access Control
Thread Ownership
Threads are owned by accounts:backend/core/threads/repo.py:125-128
Public Threads
Threads can be marked as public for sharing:Related Concepts
Agents
Learn about agents that execute within threads
Tools
Understand tools that agents can use in threads
Sandboxes
Explore the execution environment for threads
MCP
See how MCP tools integrate with threads