Skip to main content

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:
Reference: backend/core/threads/repo.py:21-46

Thread-Project-Sandbox Relationship

Threads are organized in a hierarchy:
When listing threads, the system performs an efficient LEFT JOIN to include project and sandbox information:
Reference: 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:
Reference: backend/core/threads/repo.py:174-198

2. Message Exchange

As the conversation progresses, messages are added:
  1. User sends a message
  2. Agent run is created
  3. Agent processes the message and responds
  4. Tool calls are executed (if needed)
  5. 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:
Reference: backend/core/threads/repo.py:137-156 The platform includes semantic search capabilities for finding relevant threads and messages: Messages are embedded and stored in a vector database for similarity search:
Reference: 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:
  1. Recent messages: Most recent N messages are included
  2. Token limits: Automatically truncate to fit model’s context window
  3. 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

  1. Stream created when agent run starts
  2. Events written as they occur
  3. TTL of 3600 seconds (configurable)
  4. Stream closed when run completes
Reference: backend/core/agents/runner/executor.py:48-189

Performance Optimizations

Pagination

Thread lists support efficient pagination:
The query uses 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:
Reference: 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)
This eliminates N+1 query problems when loading thread lists.

Thread Metadata

Threads support flexible metadata storage:
Metadata is stored as JSONB for efficient querying and filtering.

Access Control

Thread Ownership

Threads are owned by accounts:
Reference: backend/core/threads/repo.py:125-128

Public Threads

Threads can be marked as public for sharing:

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