What are Episodes?
Episodes are the fundamental units of information in Graphiti. An episode is any piece of input data—structured or unstructured—that you want to integrate into your knowledge graph. Think of episodes as the raw material that Graphiti processes to extract entities and relationships.- Extracts entities (Alice, Acme Corp, Senior Engineer)
- Identifies relationships (Alice —[works at]—> Acme Corp)
- Creates an episodic node to preserve the original context
- Links entities to the episode via MENTIONS edges
Episode Types
Graphiti supports three episode types defined inEpisodeType:
EpisodeType.message
For conversational data with actor-content format:For
EpisodeType.message, format content as "actor: content" on each line. Example: "user: Hello\nassistant: Hi there"EpisodeType.json
For structured data objects:EpisodeType.text
For unstructured text documents:EpisodicNode Schema
When you add an episode, Graphiti creates anEpisodicNode in the graph:
Key Fields
name: A human-readable identifier for the episodestore_raw_episode_content=False)
EntityEdge UUIDs extracted from this episode
The Episode Processing Pipeline
When you calladd_episode(), Graphiti executes a multi-step pipeline:
1. Episode Creation
2. Context Retrieval
Graphiti retrieves recent episodes for context:3. Entity Extraction
LLM analyzes the episode to extract entities:- EntityNode(name=“Alice”, labels=[“Person”])
- EntityNode(name=“Acme Corp”, labels=[“Organization”])
4. Node Deduplication
Resolves extracted nodes against existing graph entities:uuid_map maps the new extraction to the existing UUID.
5. Edge Extraction
LLM identifies relationships between entities:- EntityEdge( source_node_uuid=alice_uuid, target_node_uuid=acme_uuid, name=“works_at”, fact=“Alice started working at Acme Corp as a Senior Engineer” )
6. Edge Resolution and Invalidation
Checks for duplicate or contradictory edges:7. Attribute Extraction
Enriches entity nodes with summaries:summary field to each EntityNode based on its edges.
8. Graph Persistence
Saves everything to the graph database:Episodic Edges (MENTIONS)
Episodic edges connect episodes to the entities they mention:Example Query
Reference Time vs Created Time
Understanding the temporal distinction is crucial:Graphiti uses
reference_time to set the valid_at timestamp on extracted edges, ensuring facts are temporally grounded to when they occurred, not when they were processed.Episode Retrieval
Graphiti provides methods to retrieve episodes:Get Recent Episodes
Get by UUID
Get by Group IDs
Bulk Episode Processing
For efficient batch ingestion, useadd_episode_bulk():
add_episode_bulk() processes multiple episodes in parallel for better performance but does not perform edge invalidation. Use add_episode() for incremental updates with contradiction handling.Sagas: Organizing Episode Sequences
Sagas group related episodes into a narrative sequence:- Creates a
SagaNode(or reuses existing) - Creates
HAS_EPISODEedges from saga to episodes - Creates
NEXT_EPISODEedges to chain episodes in order
Querying Saga Episodes
Custom Extraction Instructions
You can guide the LLM’s extraction process:Storing Raw Content
Control whether to preserve episode content:Episode Best Practices
1. Use Descriptive Names
2. Set Accurate Reference Times
3. Include Source Context
4. Choose the Right Episode Type
5. Use Group IDs for Multi-Tenancy
Episode Lifecycle
Next Steps
Nodes and Edges
Learn about the graph elements extracted from episodes
Add Episodes
Detailed guide on adding episodes to your graph
Custom Entity Types
Define custom entity and edge types for extraction
Search
Search episodes and extracted facts