MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Agent Thinking
Thinking Traces

Thinking Traces

A thinking trace is a structured record of an agent's complete reasoning process for a single request. It captures the goal, every intermediate thinking step, all LLM API calls made, token usage, cost, and the final outcome. Traces are persisted to Dgraph and streamed to Kafka for downstream analytics.


Trace Lifecycle

  1. Start -- A trace is created when an agent begins processing a request
  2. Record Steps -- Each reasoning step is appended to the trace
  3. Record API Calls -- Each LLM or external API call is logged
  4. Complete -- The trace is finalized with an outcome and total metrics

Trace Structure

FieldTypeDescription
trace_idstringUnique identifier for the trace
parent_trace_idstringParent trace ID for nested agent calls
tenant_idstringTenant scope
session_idstringUser session identifier
actor_urnstringURN of the agent or user
goalstringNatural language description of the task
statusstringactive, completed, failed, cancelled
outcomestringsuccess, partial, failure
total_thinking_tokensintTotal thinking/reasoning tokens consumed
total_input_tokensintTotal input tokens across all steps
total_output_tokensintTotal output tokens across all steps
total_cost_usdfloatTotal estimated cost in USD
total_duration_msfloatTotal execution time in milliseconds
model_ids_usedlistList of model IDs used during execution
path_takenlistOrdered list of agent steps taken

Usage

Start a Thinking Trace

service = AgentThinkingCaptureService(dgraph_store=store)
 
trace_id = await service.start_thinking_trace(
    tenant_id="acme",
    session_id="sess-123",
    actor_urn="urn:matih:agent:acme:bi-agent",
    goal="Show me total sales by region",
)

Record a Thinking Step

await service.record_thinking_step(
    trace_id,
    AgentThinkingStep(
        step_type=ThinkingStepType.INTENT_ANALYSIS,
        reasoning="User wants aggregated sales data grouped by region",
        confidence=0.95,
        model_id="gpt-4",
        token_usage=TokenUsageRecord(
            input_tokens=150,
            output_tokens=50,
            thinking_tokens=200,
        ),
    ),
)

Complete a Thinking Trace

await service.complete_thinking_trace(
    trace_id,
    outcome="success",
)

Thinking Step Fields

FieldTypeDescription
step_idstringUnique step identifier
step_typeenumType of reasoning step
sequence_numberintOrder within the trace
reasoningstringThe agent's reasoning text
reasoning_hashstringSHA-256 hash for deduplication
input_summarystringSummary of input to this step
output_summarystringSummary of output from this step
alternatives_consideredlistOther options the agent considered
selected_alternativestringWhich alternative was chosen
confidencefloatConfidence score (0-1)
duration_msfloatStep execution time
model_idstringLLM model used for this step

API Call Records

Each external API call is recorded with:

FieldTypeDescription
call_idstringUnique call identifier
api_typestringType of API (e.g., llm, database, tool)
endpointstringAPI endpoint called
methodstringHTTP method
status_codeintHTTP response status
latency_msfloatCall latency in milliseconds
request_tokensintTokens in the request
response_tokensintTokens in the response
cost_usdfloatEstimated cost
errorstringError message if the call failed

Querying Traces

traces = await store.query_thinking_traces(
    tenant_id="acme",
    session_id="sess-123",
    status="completed",
    limit=50,
    offset=0,
)