Threads
Manage conversation history and context
Threads in Marvin provide a powerful mechanism for managing conversation history and context across your AI workflows. Unlike ControlFlow’s flows, threads in Marvin are focused on conversation management and persistence rather than workflow orchestration.
Understanding Threads
A thread in Marvin represents a conversation context that can span multiple interactions. Each thread:
- Maintains its own conversation history
- Can be persisted to a database
- Can be referenced across different parts of your application
- Supports attaching memories for long-term information storage
Creating and Using Threads
Basic Thread Creation
The simplest way to create a thread is to instantiate the Thread
class:
Specifying a Thread ID
You can create a thread with a specific ID, which is useful for resuming conversations:
Using Threads with Tasks
Threads can be passed to tasks to maintain conversation context:
Adding Messages
You can manually add messages to a thread:
Retrieving History
Getting Messages
You can retrieve messages from a thread with various filters:
Tracking LLM Usage
Threads also track LLM API usage:
Thread Context Management
Marvin provides context management for threads, making it easy to set the current thread:
You can also get the current thread:
Parent-Child Relationships
Threads can have parent-child relationships:
Database Integration
Threads are automatically persisted to a database when configured:
Threads and Memory
While threads provide short-term conversation context within a session, Memory offers long-term persistence across multiple sessions. The two can work together:
Threads maintain the flow of conversation, while Memory stores specific information that should persist across different threads and sessions. See the Memory documentation for more details on working with persistent memory.