MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Integration
Orchestrator Hooks

Orchestrator Hooks

The ContextGraphOrchestratorHooks class provides non-invasive middleware hooks that plug into both the BI Analytics Orchestrator and the General Agent Orchestrator. These hooks capture agent thinking steps, LLM calls, and tool executions as AgentThinkingStep records without modifying core orchestrator logic.


Overview

The hooks follow an event-driven middleware pattern: orchestrators call hook methods at key execution points, and the hooks delegate to the AgentThinkingCaptureService for persistence. All hook methods are safe to call even when the thinking service is unavailable (graceful degradation).

Source: data-plane/ai-service/src/context_graph/integration/orchestrator_hooks.py


Integration Points

BI Orchestrator

from context_graph.integration.orchestrator_hooks import get_orchestrator_hooks
 
hooks = get_orchestrator_hooks()
if hooks:
    trace_id = await hooks.on_bi_message_start(session_id, tenant_id, message)
    # ... run BI agents ...
    await hooks.on_bi_message_complete(trace_id, response)

General Orchestrator

hooks = get_orchestrator_hooks()
if hooks:
    trace_id = await hooks.on_agent_start(session_id, tenant_id, goal)
    await hooks.on_agent_step(trace_id, step_type, reasoning, confidence)
    await hooks.on_agent_complete(trace_id, outcome)

Hook Methods

MethodWhen CalledWhat It Captures
on_bi_message_startBI agent begins processing a user messageCreates a new thinking trace
on_bi_message_completeBI agent finishes processingCompletes the trace with outcome and metrics
on_agent_startGeneral agent begins a taskCreates a new thinking trace
on_agent_stepAgent completes a reasoning stepRecords a thinking step
on_llm_callAn LLM API call is madeRecords an API call record
on_tool_callA tool is executedRecords a tool execution step
on_agent_completeAgent finishes the taskCompletes the trace

Graceful Degradation

All hook methods follow the fail-safe pattern:

  1. Check if the thinking service is available
  2. If unavailable, log a debug message and return None
  3. If available, delegate to the service
  4. Catch and log any exceptions without re-raising

This ensures that Context Graph issues never impact the critical agent execution path.


Tenant Configuration

Hooks check the tenant configuration before capturing:

config = get_tenant_context_graph_config(tenant_id)
if not config or not config.thinking_enabled:
    return None

Singleton Access

from context_graph.integration.orchestrator_hooks import (
    get_orchestrator_hooks,
    create_orchestrator_hooks,
)
 
# Create with custom service
hooks = create_orchestrator_hooks(thinking_service=service)
 
# Get singleton
hooks = get_orchestrator_hooks()