Skip to content

Trails

Rails for agentic knowledge-graph apps.

One surface. Start with nodes and edges, add types when you want validation, add SHACL when you want closed-world checks, add OWL when you want reasoning. Every feature is additive — code you write on day one keeps working on day 365.

Guides Get Started API Reference


Start with the Guides

The Guides are the fastest way to learn Trails. 32 feature-focused references covering ORM, agents, federation, memory, enrichment, and more — each one self-contained and runnable.


Hello, Trails

from trails import capability, node_type

@node_type("Note", fields={"title": str, "body": str})
class Note: pass

@capability
def create_note(ctx, title: str, body: str) -> dict:
    n = Note(title=title, body=body)
    ctx.kg.add(n)
    return {"id": n.id}

That's a working app. trails server discovers it, exposes it over MCP and HTTP, and records provenance. No config files, no boilerplate.


The Progressive Enhancement Ladder

graph LR
    A["<b>Labels</b><br/>ctx.kg.node / edge"] --> B["<b>Types</b><br/>@node_type + fields"]
    B --> C["<b>Validation</b><br/>@shape + SHACL"]
    C --> D["<b>Reasoning</b><br/>OWL-RL inference"]

    style A fill:#e8def8,color:#1b1b1f,stroke:#7c4dff,stroke-width:2px
    style B fill:#d0bcff,color:#1b1b1f,stroke:#651fff,stroke-width:2px
    style C fill:#bbd0ff,color:#1b1b1f,stroke:#536dfe,stroke-width:2px
    style D fill:#c2e7ff,color:#1b1b1f,stroke:#448aff,stroke-width:2px

Each step is opt-in. You never re-architect — you just add a decorator. See ADR-0021 for the full rationale.


What You Get

  • Knowledge Graph ORM


    Django-parity ActiveGraph ORM. @node_type, Model.where(), Q combinators, property-path traversal, dirty-tracking upserts.

    ORM Guide

  • Agent Runtime


    Three planning strategies — ReAct, Plan-and-Execute, Reflexion — behind one protocol. PROV-O trails and cost envelopes on every step.

    Agent Guide

  • Trust Stack


    Cedar policies, DIDs, verifiable credentials, PROV-O provenance, cost envelopes, consent receipts. All opt-in, all first-class.

    Policy Guide

  • Federation


    SPARQL endpoint, SERVICE queries across peers, MCP capability relay. Standalone by default, federated on demand.

    Federation Guide

  • Data Integration


    PDF/HTML/Markdown ingestion, RML mapping from CSV/JSON/XML/RDBMS, auto-ontology inference, enrichment pipelines.

    Ingestion Guide

  • Progressive AI


    LLM features are optional — core works without any API key. Add AI when it helps: ontology generation, enrichment, agents.

    LLM Guide

  • Temporal KG


    Bitemporal versioning for @node_type instances. as_of() queries, history(), temporal_diff() — time-travel through your graph.

    Temporal Guide

  • Hypothesis Agents


    Scientific reasoning on your KG: observe, hypothesize, test, report. Grounded citations to real nodes, PROV-O lineage, budget-aware.

    Hypothesis Guide

  • Explainable Provenance


    Citation graphs, confidence propagation, counterfactual analysis. Trace exactly how any result was derived — no LLM needed.

    Explainable Guide


Architecture at a Glance

graph TB
    subgraph Python["Python Surface (~24K LLOC)"]
        direction TB
        APP["Your App<br/>@capability @node_type @shape @policy"]
        ORM["ORM &bull; Agents &bull; Ingest &bull; Vector"]
        RT["Runtime: invoke() &bull; ctx.kg &bull; ctx.llm"]
        TR["MCP (stdio + SSE) &bull; HTTP (FastAPI) &bull; CLI"]
    end

    subgraph Bridge["trails._bridge"]
        BR["Auto-detect backend"]
    end

    subgraph Backends["Backends"]
        direction LR
        PY["Python backend<br/>pyoxigraph<br/><i>default</i>"]
        RS["Rust kernel<br/>trails._core<br/><i>optional</i>"]
    end

    subgraph Store["Oxigraph"]
        AD["Adapters: Oxigraph | Fuseki | Qlever"]
    end

    APP --> ORM --> RT --> TR
    TR --> BR
    BR --> PY
    BR --> RS
    PY --> AD
    RS --> AD

    style Python fill:#7c4dff15,stroke:#7c4dff
    style Bridge fill:#ffd74015,stroke:#ffd740
    style PY fill:#448aff15,stroke:#448aff
    style RS fill:#00c85315,stroke:#00c853

The Brain and Hand Pattern

Trails separates reasoning from execution throughout its architecture. Look for these markers in the guides:

Marker Role What it does
🧠 Brain Planning / reasoning Decides what to do — query planning, strategy selection, LLM calls
🤚 Hand Execution / action Does the thing — runs queries, transforms data, calls APIs

This separation makes every subsystem testable (mock the hand, unit-test the brain), pluggable (swap the hand without touching the brain), and observable (track planning cost separately from execution cost).

Deep dive: Brain and Hand in Trails


Resource Description
Handbook 11-chapter learning path from zero to production
Guides 22 feature-focused reference guides
Tutorial Walk the progressive enhancement ladder end-to-end
Architecture Decisions 35 ADRs documenting every major choice
API Reference Auto-generated from source
Examples 9 in-tree example apps
CLI Reference 30+ commands