Skip to content

History: decouple from cache, add per-module + per-LM tracking #70

Open

Description

@darinkishore

Context

DSPy Python has 3-level history tracking (global, per-LM, per-module), fully independent of caching. Each history entry includes: messages, kwargs, response, outputs, usage, cost, timestamp, uuid, model. inspect_history(n) works at all three levels.

dspy-rs currently couples history entirely to the cache layer:

  • inspect_history(n) reads from ResponseCache.history_window (a fixed-size Vec<CacheEntry>)
  • If cache: false, there's no history at all
  • CacheEntry stores { prompt: String, prediction: Prediction } — minimal info

Meanwhile, the trace/ module (task-local DAG recorder) was an earlier attempt at runtime execution recording, but it uses Prediction/Example types and is currently unwired (see #68).

What's needed

Separate history from cache

  • History should record every LM call regardless of cache settings
  • Rich entries: messages sent, LM params, raw response, parsed output, usage, timestamp

Per-LM history

  • lm.history: Vec<HistoryEntry> with configurable max size
  • lm.inspect_history(n) returns last n entries

Per-module history

  • When a module calls an LM, the entry is also appended to module.history
  • Requires knowing which module is the caller (DSPy uses settings.caller_modules stack)

Global history

  • dspy_rs::inspect_history(n) for all LM calls across the process

Modernize trace/ module

Related

Activity

  1. darinkishore commented on Feb 12, 2026

    @darinkishore
    CollaboratorAuthor

    Codebase audit — current history coupling

    How history works today:

    LM::call() → sends CacheEntry to ResponseCache via mpsc channel
    ResponseCache::insert() → stores in foyer + appends to history_window (Vec<CacheEntry>, max 100)
    LM::inspect_history(n) → reads from cache_handler.lock().get_history(n)
    

    If cache: false on LM → cache_handler is still created (it's always Some), but the semantics are confusing. History is an accident of caching, not a deliberate feature.

    What DSPy Python does:

    • lm.history — list of dict with messages, response, usage, cost, timestamp, uuid, model
    • dspy.inspect_history(n) — global, reads from all LMs
    • History is independent of caching — every call is recorded regardless
    • Per-module: settings.caller_modules stack tracks which module triggered which LM call

    Dependency chain:

    1. DummyLM should share a trait with LM #77 first — LanguageModel trait, so history can be trait-level
    2. proper error handling #67 partially — HistoryEntry should use typed errors, not just success cases
    3. Cache: fix key to be request-based, not Example-based #71 partially — separating history from cache means CacheEntry no longer serves double duty

    Proposed HistoryEntry:

    pub struct HistoryEntry {
        pub id: Uuid,
        pub timestamp: DateTime<Utc>,
        pub model: String,
        pub messages: Vec<Message>,  // what was sent
        pub raw_response: String,
        pub parsed_output: Option<BamlValue>,
        pub usage: LmUsage,
        pub latency_ms: u64,
        pub module_path: Option<String>,  // which Predict/module triggered this
        pub error: Option<String>,  // if the call failed
    }

    Storage: Vec<HistoryEntry> with configurable max size per-LM instance. Global history = aggregation across all LM instances. Per-module = filtered by module_path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions