๐Ÿ”Engram

Engram Documentation

Causal memory for AI agents. Not just what happened โ€” why.

Quickstart

BYOK (open-source, free)

# Install
pip install engram-causal

# Set your Anthropic key (only needed for free-text extraction)
export ANTHROPIC_API_KEY=sk-ant-...

# Install hooks for Claude Code
engram install

# Extract a causal triple from free text
engram extract "WeatherAPI timed out because the server was overloaded"

# Query root cause
engram why "WeatherAPI"
Structured JSON events and why-queries are always zero LLM โ€” no Anthropic key needed for those.

How it works

Three-track pipeline. LLM only called once per unique text. Everything else is local.

input โ†’ structured JSON event?

YES โ†’ rule_extractor() โ†’ CausalTriple (confidence=EXTRACTED, $0.00)

NO โ†’ SHA256 cache hit?

YES โ†’ cached triples ($0.00)

NO โ†’ claude_extractor() โ†’ CausalTriple + cache result

Confidence tagging โ€” every triple is tagged:

  • EXTRACTED โ€” found directly in structured event fields (confidence 1.0)
  • INFERRED โ€” extracted by Claude from free-text (confidence 0.0โ€“1.0)
  • AMBIGUOUS โ€” below threshold, kept for exploration

CLI reference

# Extract causal triples from free text
engram extract "WeatherAPI timed out because server was overloaded"

# Ingest structured event log (zero LLM)
engram ingest ./logs/session.json

# Query root cause (backward DFS, zero LLM)
engram why "WeatherAPI"

# Point-in-time causal query
engram why "PaymentService" --before 2024-03-01

# Start local API server
engram serve

# Start MCP stdio server
engram serve --mcp

# Install hooks for your platform
engram install                        # Claude Code (PreToolUse + PostToolUse)
engram install --platform cursor      # Cursor rules
engram install --platform codex       # Codex AGENTS.md + hook

SDK reference

Basic client

import engram

# BYOK โ€” bring your own Anthropic key
client = engram.Client(anthropic_api_key="sk-ant-...")

# Extract and store
triples = await client.extract("WeatherAPI timed out because server overloaded")
await client.store(triples)

# Why-query
result = await client.why("WeatherAPI")
print(result.explanation)
print(result.top_causes)

Agent decorator

import engram

@engram.wrap(entity="PaymentService")
async def my_agent(task: str) -> str:
    # Engram auto-injects causal context before execution
    # Auto-ingests outcome as structured event after
    ...

Hook setup

Hooks are the core of the agent performance loop. PreToolUse injects causal context before every tool call. PostToolUse ingests the outcome back into the graph.

Claude Code

engram install
# Writes .claude/settings.json hooks + CLAUDE.md section
# PreToolUse: fires before Bash/Glob/Read โ†’ injects top causal chain (โ‰ค500 tokens)
# PostToolUse: fires after tool completes โ†’ ingests outcome as structured event

Cursor

engram install --platform cursor
# Writes .cursor/rules/engram.mdc with alwaysApply: true

Codex

engram install --platform codex
# Writes AGENTS.md + .codex/hooks.json PreToolUse hook
Token budget: hook injects max 500 tokens by default. Configure with ENGRAM_HOOK_BUDGET=300.

MCP server

# Start MCP stdio server
engram serve --mcp

# .mcp.json
{
  "mcpServers": {
    "engram": {
      "type": "stdio",
      "command": "engram",
      "args": ["serve", "--mcp"]
    }
  }
}

MCP tools exposed:

  • why_query(entity, max_depth, min_confidence) โ€” backward DFS causal chain
  • store_triple(cause, effect, mechanism, confidence) โ€” persist a causal triple
  • ingest_events(events) โ€” batch ingest structured event log

API reference

MethodEndpointDescription
GET/v1/healthService health + graph stats
POST/v1/extractExtract triples without storing
POST/v1/storePersist triples to graph
GET/v1/why/{entity}Why-query for entity (DFS)
POST/v1/extract-and-storeExtract + persist in one call
POST/v1/eventsIngest structured event log
GET/v1/why/session/{id}Explain session failure chain

Token costs

Event typeLLM costReason
Structured JSON event$0.00Rule-based extractor
Repeated free-text log$0.00SHA256 cache hit
New free-text (first time)~$0.0003/1K tokensClaude Haiku โ†’ cached
Why-query$0.00Graph DFS, no LLM
Explanation synthesis$0.00Template engine

Changelog

Canonical release history: CHANGELOG.md ยท GitHub release bodies: RELEASE_NOTES.md

v0.1.4 โ€” Entity normalization improvements

  • 40eefcc โ€” normalization entity added with fallback

v0.1.3 โ€” Codex integration fix

  • 195e91d โ€” version updated
  • 2bc1afc โ€” fix: codex network access to engram with rule

v0.1.2 โ€” CI release pipeline update

  • ac54283 โ€” fix: published version updated with CI

v0.1.1 โ€” PyPI metadata improvements

  • b5586f7 โ€” Add PyPI project description metadata

v0.1.0 โ€” Initial release

  • af3ca1b โ€” Use PyPI API token for publishing
  • 6bf0122 โ€” causal graph in CLI mode activated
  • 4dbba73 โ€” fix: cache import changed with hosted fastapi app for hosted users
  • a2fbf5f โ€” per org kuzu graph in s3
  • f86f7a7 โ€” engram install hooks for claude code