Skip to main content

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.
When you add an episode, Graphiti:
  1. Extracts entities (Alice, Acme Corp, Senior Engineer)
  2. Identifies relationships (Alice —[works at]—> Acme Corp)
  3. Creates an episodic node to preserve the original context
  4. Links entities to the episode via MENTIONS edges

Episode Types

Graphiti supports three episode types defined in EpisodeType:

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:
Graphiti’s LLM extracts entities and relationships from the structured fields.

EpisodeType.text

For unstructured text documents:

EpisodicNode Schema

When you add an episode, Graphiti creates an EpisodicNode in the graph:

Key Fields

name: A human-readable identifier for the episode
content: The raw episode body (can be empty if store_raw_episode_content=False)
source: The episode type
source_description: Metadata about where the episode came from
valid_at: When the episode content was created or occurred in the real world
created_at: When the episode was ingested into Graphiti
entity_edges: List of EntityEdge UUIDs extracted from this episode

The Episode Processing Pipeline

When you call add_episode(), Graphiti executes a multi-step pipeline:

1. Episode Creation

2. Context Retrieval

Graphiti retrieves recent episodes for context:
Previous episodes provide context for entity resolution. If “Alice” was mentioned in a prior episode, Graphiti recognizes it’s the same person.

3. Entity Extraction

LLM analyzes the episode to extract entities:
From the episode “Alice started working at Acme Corp”, this extracts:
  • EntityNode(name=“Alice”, labels=[“Person”])
  • EntityNode(name=“Acme Corp”, labels=[“Organization”])

4. Node Deduplication

Resolves extracted nodes against existing graph entities:
If “Alice” already exists in the graph, uuid_map maps the new extraction to the existing UUID.

5. Edge Extraction

LLM identifies relationships between entities:
Extracts:
  • 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:
If Alice previously worked somewhere else, the old edge is temporally invalidated.

7. Attribute Extraction

Enriches entity nodes with summaries:
Adds a 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:
These edges preserve provenance—you can always trace back which episodes mentioned which entities.

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, use add_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:
Graphiti automatically:
  1. Creates a SagaNode (or reuses existing)
  2. Creates HAS_EPISODE edges from saga to episodes
  3. Creates NEXT_EPISODE edges 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:
Set store_raw_episode_content=False to save database storage when you don’t need to retrieve the original episode text later.

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