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:
| Field | Description |
|---|---|
sequence | Ordered list of action types (e.g., [INTENT, SQL_GEN, VALIDATE]) |
support | Number of traces containing this pattern |
support_ratio | Fraction of all traces that contain this pattern |
confidence | Statistical confidence of the pattern |
Anomaly Scores
Each trace receives an anomaly score based on deviation from established patterns:
| Field | Description |
|---|---|
trace_urn | The trace being scored |
score | Anomaly score from 0 (normal) to 1 (highly anomalous) |
reasons | Human-readable explanation of why it is anomalous |
unusual_transitions | Unexpected action-to-action transitions |
missing_expected | Expected actions that did not occur |
Pattern Clusters
Groups of similar patterns identified by clustering algorithms:
| Field | Description |
|---|---|
cluster_id | Unique cluster identifier |
representative_pattern_id | The most central pattern in the cluster |
member_pattern_ids | All patterns in the cluster |
centroid_sequence | The representative action sequence |
avg_similarity | Average 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
| Endpoint | Method | Description |
|---|---|---|
/api/v1/context-graph/patterns/detect | POST | Run pattern detection on recent traces |
/api/v1/context-graph/patterns/statistics | GET | Get pattern frequency statistics |
Use Cases
| Use Case | Description |
|---|---|
| Agent optimization | Identify the most efficient reasoning paths for common queries |
| Anomaly detection | Flag unusual agent behavior for human review |
| Template creation | Convert common patterns into reusable agent templates |
| Performance monitoring | Track pattern evolution over time |