MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Analytics & Ranking
Pattern Mining

Pattern Mining

The PatternMiningService discovers common execution patterns, anomalous traces, and pattern clusters from agent trace data. It implements sequential pattern mining (PrefixSpan-style), graph-based pattern discovery, and anomaly detection algorithms.


Overview

Pattern mining enables the platform to learn from agent behavior over time. By identifying common successful patterns, unusual execution sequences, and clusters of similar behavior, the system can optimize agent routing, detect problems early, and provide pattern-based recommendations.

Source: data-plane/ai-service/src/context_graph/services/pattern_mining_service.py


Pattern Types

Sequential Patterns

Discovered from the ordered sequence of action types within traces:

FieldDescription
sequenceOrdered list of action types (e.g., [INTENT, SQL_GEN, VALIDATE])
supportNumber of traces containing this pattern
support_ratioFraction of all traces that contain this pattern
confidenceStatistical confidence of the pattern

Anomaly Scores

Each trace receives an anomaly score based on deviation from established patterns:

FieldDescription
trace_urnThe trace being scored
scoreAnomaly score from 0 (normal) to 1 (highly anomalous)
reasonsHuman-readable explanation of why it is anomalous
unusual_transitionsUnexpected action-to-action transitions
missing_expectedExpected actions that did not occur

Pattern Clusters

Groups of similar patterns identified by clustering algorithms:

FieldDescription
cluster_idUnique cluster identifier
representative_pattern_idThe most central pattern in the cluster
member_pattern_idsAll patterns in the cluster
centroid_sequenceThe representative action sequence
avg_similarityAverage pairwise similarity within the cluster

Mining Configuration

from context_graph.models.process import PatternMiningConfig
 
config = PatternMiningConfig(
    min_support=5,            # Minimum traces for a valid pattern
    min_confidence=0.3,       # Minimum pattern confidence
    max_pattern_length=20,    # Maximum steps in a pattern
    anomaly_threshold=0.7,    # Score above which a trace is anomalous
)

Mining Operations

Run Pattern Mining

result = await service.mine_patterns(
    tenant_id="acme",
    traces=traces,
    config=config,
)
# MiningResult(
#     patterns_discovered=15,
#     traces_analyzed=500,
#     anomalies_detected=12,
#     mining_duration_ms=1234.5,
# )

Detect Anomalies

anomalies = await service.detect_anomalies(
    tenant_id="acme",
    traces=traces,
    threshold=0.7,
)

REST API

EndpointMethodDescription
/api/v1/context-graph/patterns/detectPOSTRun pattern detection on recent traces
/api/v1/context-graph/patterns/statisticsGETGet pattern frequency statistics

Use Cases

Use CaseDescription
Agent optimizationIdentify the most efficient reasoning paths for common queries
Anomaly detectionFlag unusual agent behavior for human review
Template creationConvert common patterns into reusable agent templates
Performance monitoringTrack pattern evolution over time