Skip to main content

Overview

The Graph Expander is a long-running background worker that autonomously discovers and adds related historical events to the knowledge graph. It uses an LLM to suggest contextually relevant events from “frontier nodes” (sparsely connected events).
The Expander must be explicitly enabled via the EXPANSION_ENABLED feature flag and requires a valid OPENROUTER_API_KEY.

Configuration

Parameters

Environment Variables

Worker Lifecycle

The Expander runs continuously, executing expansion cycles at regular intervals:

Startup

In app/main.py, the Expander is initialized during application startup:

Shutdown

Expansion Algorithm

Each expansion cycle follows this workflow:

1. Find Frontier Nodes

Frontier nodes are events with fewer than 3 connections, indicating they’re under-explored in the graph. The Expander uses an LLM to suggest 3-5 related historical events:

3. Add Events to Graph

Each suggested event is added as a new node:
The _add_event method:
  • Constructs a URL path for the event
  • Checks if the event already exists
  • Creates the node with metadata
  • Adds an edge from the source node

Edge Types

The Expander creates edges with semantic relationships:

Example Response Parsing

The Expander handles markdown-wrapped JSON responses:
This ensures the LLM’s output is correctly parsed even if wrapped in code fences.

Node Metadata

Expanded nodes are tagged with provenance:
Expanded events start with flash_timepoint_id=None. The Daily worker may later generate scenes for popular events.

Error Handling

The Expander gracefully handles API failures:
Key failure modes:
  • LLM timeout: 120-second timeout on OpenRouter requests
  • Invalid JSON: Malformed LLM responses are logged and skipped
  • Duplicate events: Existing events are silently ignored
  • Edge creation errors: Caught with ValueError and passed

Monitoring

Watch expansion activity via logs:
Track graph growth with the /health endpoint: