MATIH Platform is in active MVP development. Documentation reflects current implementation status.
12. AI Service
Context Graph
Agent Thinking Traces

Agent Thinking Traces

Production - Dgraph-backed agent reasoning capture with Kafka streaming

Agent Thinking Traces capture the full reasoning process of every agent interaction, storing each step in Dgraph for analysis and replay. This enables debugging, quality assurance, and continuous improvement of agent behaviors.


12.5.4.1Thinking Capture

The AgentThinkingCaptureService records thinking traces via orchestrator hooks:

# OrchestratorHooks integration (src/context_graph/integration/orchestrator_hooks.py)
class OrchestratorHooks:
    async def on_agent_message_start(self, session_id, tenant_id, message, agent_id):
        """Start a new thinking trace."""
        trace_id = await self._thinking_service.start_trace(
            session_id=session_id,
            tenant_id=tenant_id,
            input_message=message,
            agent_id=agent_id,
        )
        return trace_id
 
    async def on_bi_agent_step(self, trace_id, agent_name, state, duration_ms):
        """Record an agent step within a thinking trace."""
        await self._thinking_service.add_step(
            trace_id=trace_id,
            agent_name=agent_name,
            state_snapshot=state.to_dict(),
            duration_ms=duration_ms,
        )
 
    async def on_agent_message_complete(self, trace_id, success):
        """Complete a thinking trace."""
        await self._thinking_service.complete_trace(trace_id, success=success)

Storage Pipeline

Agent Execution
    |
    v
OrchestratorHooks (capture events)
    |
    +-- DgraphContextStore (persistent graph storage)
    +-- KafkaProducer (event streaming)
    +-- ThinkingEmbeddings (vector indexing)

12.5.4.2API Endpoints

# Get thinking trace for a session
curl "http://localhost:8000/api/v1/context-graph/thinking/traces?session_id=sess-123&tenant_id=acme-corp"
 
# Get trace details
curl "http://localhost:8000/api/v1/context-graph/thinking/traces/{trace_id}?tenant_id=acme-corp"
 
# Search thinking traces by content
curl -X POST http://localhost:8000/api/v1/context-graph/thinking/search \
  -H "Content-Type: application/json" \
  -H "X-Tenant-ID: acme-corp" \
  -d '{"query": "SQL generation errors", "limit": 20}'
{
  "trace_id": "trace-uuid-123",
  "session_id": "sess-123",
  "agent_id": "bi-orchestrator",
  "status": "completed",
  "steps": [
    {"agent": "router", "action": "classify", "result": "query_data", "duration_ms": 120},
    {"agent": "sql", "action": "generate", "result": "SELECT...", "duration_ms": 1850},
    {"agent": "visualization", "action": "generate", "result": {"type": "bar"}, "duration_ms": 95}
  ],
  "total_duration_ms": 2340,
  "success": true
}