The reasoning module operates over facts you supply directly or load from a
ContextGraph. Derived facts are added to working memory and are immediately available for further inference in the same session. To persist derived facts back into the graph, pass them to AgentContext.store().Choosing a reasoning mode
| Mode | Best for | Class |
|---|---|---|
| Forward chaining | Materialise all implied facts from ground truth | Reasoner.forward_chain() |
| Backward chaining | Prove a specific goal; get the minimal evidence chain | Reasoner.backward_chain() |
| Datalog | Recursive traversal of arbitrary depth (supply chains, org graphs) | DatalogReasoner |
| SPARQL | Pattern-matching queries over enriched working memory | SPARQLReasoner |
| RETE | 100+ rule sets — incremental fact propagation via alpha/beta network | ReteEngine |
| Temporal | Allen interval relations between time windows | TemporalReasoningEngine |
| Natural language | LLM-backed freeform queries over graph context | GraphReasoner |
| Explanation | Translate inference results into human-readable justifications | ExplanationGenerator |
Step 1 — Ground facts and working memory
TheReasoner class maintains a set of ground facts and a list of rules. Facts can be added as predicate strings or loaded from a ContextGraph. Start with the explicit knowledge your extraction pipeline produced:
Step 2 — Forward chaining: materialising derived facts
Forward chaining starts from ground facts and applies every matching rule until no new conclusions can be drawn — reaching fixpoint:Rule dataclass:
Step 3 — Backward chaining: proving a specific goal
Backward chaining tests a single hypothesis by working backward through rules — the right tool when you need a yes/no answer and the minimal evidence chain without deriving every other possible fact first:Step 4 — Recursive inference with Datalog
DatalogReasoner handles questions requiring arbitrary-depth traversal — “which actors can transitively reach critical infrastructure?” — using recursive Horn clause rules with semi-naive bottom-up fixpoint evaluation:
add_fact() calls by loading a ContextGraph directly:
Step 5 — SPARQL queries over enriched working memory
After forward chaining has derived new facts,SPARQLReasoner lets you query the enriched working memory using SPARQL triple-pattern matching with optional inference expansion:
Step 6 — RETE engine for large rule sets
ReteEngine implements the RETE algorithm — a network of alpha nodes (single-condition matching) and beta nodes (join operations) that avoids re-evaluating unchanged conditions on every new fact. Use it when you have 100+ rules or need incremental fact propagation in a streaming or event-driven setting:
build_network(). Each subsequent add_fact() call propagates incrementally through only the nodes whose conditions it satisfies — not the full rule set — which keeps evaluation cost proportional to the number of new activations rather than the total rule count.
Step 7 — Temporal interval reasoning
TemporalReasoningEngine computes Allen interval relations between time windows, letting you identify whether two events overlap, one contains the other, they meet at a boundary, and so on across your graph:
| Relation | Meaning |
|---|---|
BEFORE | A ends before B starts |
MEETS | A ends exactly where B starts (no gap, no overlap) |
OVERLAPS | A starts before B and ends during B |
STARTS | A and B start together; A ends first |
DURING | A is fully contained within B |
FINISHES | A and B end together; A starts later |
EQUALS | A and B are identical intervals |
AFTER, MET_BY, OVERLAPPED_BY, STARTED_BY, CONTAINS, FINISHED_BY | Inverses of the above |
OVERLAPS or EQUALS result between two campaigns attributed to different actors is a signal worth flagging for analyst review — a temporal coincidence is a hypothesis, not a conclusion.
Step 8 — LLM-based graph reasoning
GraphReasoner routes freeform natural language queries through an LLM provider, using the graph as grounded context. Use it for exploratory questions that do not map cleanly to a predefined rule set:
GraphReasoner is well suited for early-stage investigation — when the question is exploratory and you have not yet formalised inference rules. For reproducible, auditable decisions, use Reasoner or DatalogReasoner instead.
Step 9 — Explaining inferences in natural language
ExplanationGenerator translates any InferenceResult (from forward or backward chaining) into a human-readable explanation, a step-by-step ReasoningPath, and a Justification with supporting evidence:
"simple" gives a one-line summary, "detailed" lists premises and the rule name, and "verbose" produces a full confidence-annotated narrative.
Putting it together: a complete reasoning pipeline
A pipeline combining forward chaining, Datalog reachability, and natural language explanations for a threat intelligence graph:Domain examples
- Defense — CTI/Threat
- Security — SOC/Incident
- Life Science — Clinical/Pharma
- Banking — Risk/Compliance
Attribution chains in threat intelligence require multi-hop confidence propagation: a TTP match raises the probability of actor attribution, corroborating ASN geolocation raises it further, and a known targeting pattern for the attributed sector raises it to actionable confidence. Each hop is a separate rule with its own confidence weight, and
InferenceResult carries the propagated value through the chain.Related Guides
- Semantic Extraction — extract the entities and relationships that populate the graph facts you reason over
- GraphRAG — retrieve graph-grounded context for LLM responses
- Ontology Management — generate OWL ontologies to give your rules formal semantics
- Decision Intelligence — record and trace inferred decisions through the full causal chain
- Context Graphs — the knowledge graph that reasoning operates over
- MCP Server — expose
run_reasoningas a tool for Claude and other agents
