Skip to content

Reflect API

Analyze behavioral patterns across agent sessions — error patterns, tool sequences, cost analysis, and performance trends.

GET /api/reflect

Run a pattern analysis across sessions and events.

Query Parameters

ParameterTypeDefaultDescription
analysisstring(required)Analysis type (see below)
agentIdstringFilter to a specific agent
fromstringStart of time range (ISO 8601)
tostringEnd of time range (ISO 8601)
limitnumber20Maximum results / patterns

Analysis Types

TypeDescription
error_patternsRecurring error patterns across sessions — groups similar errors, shows frequency and time range
tool_sequencesCommon tool usage patterns — frequently used tool chains, their success rates, and error rates
cost_analysisCost breakdown by model and agent — total spend, per-session averages, trend direction
performance_trendsSuccess rate, duration, and error trends over time — with an overall assessment

Response (200)

json
{
  "analysis": "error_patterns",
  "insights": [
    {
      "type": "error_pattern",
      "summary": "Connection timeout when calling external API",
      "data": {
        "pattern": "Connection timeout",
        "count": 15,
        "firstSeen": "2026-01-15T08:30:00.000Z",
        "lastSeen": "2026-02-07T14:20:00.000Z",
        "affectedSessions": ["ses_001", "ses_002", "ses_003"],
        "precedingTools": [["fetch_api", "process_response"]]
      },
      "confidence": 0.92
    }
  ],
  "metadata": {
    "sessionsAnalyzed": 50,
    "eventsAnalyzed": 1200,
    "timeRange": {
      "from": "2026-01-01T00:00:00.000Z",
      "to": "2026-02-08T00:00:00.000Z"
    }
  }
}

Response Fields

FieldTypeDescription
analysisstringThe analysis type that was run
insightsarrayArray of structured insights
insights[].typestringInsight classification
insights[].summarystringHuman-readable summary
insights[].dataobjectAnalysis-type-specific structured data
insights[].confidencenumberConfidence score (0–1)
metadata.sessionsAnalyzednumberNumber of sessions included
metadata.eventsAnalyzednumberNumber of events processed
metadata.timeRangeobjectActual time range analyzed

Analysis-Specific Data Shapes

error_patterns

json
{
  "pattern": "Connection timeout",
  "count": 15,
  "firstSeen": "2026-01-15T08:30:00.000Z",
  "lastSeen": "2026-02-07T14:20:00.000Z",
  "affectedSessions": ["ses_001", "ses_002"],
  "precedingTools": [["fetch_api", "process_response"]]
}

tool_sequences

json
{
  "tools": ["search_db", "format_result", "respond"],
  "frequency": 25,
  "sessions": 12,
  "errorRate": 0.08
}

cost_analysis

Summary insight:

json
{
  "totalCost": 12.45,
  "avgPerSession": 0.25,
  "totalSessions": 50
}

Model breakdown insight:

json
{
  "model": "claude-opus-4-6",
  "callCount": 200,
  "totalCost": 8.50,
  "avgCostPerCall": 0.0425
}

Current stats insight:

json
{
  "successRate": 0.94,
  "avgDuration": 12500,
  "avgToolCalls": 8.3,
  "avgErrors": 0.4
}

Assessment insight:

json
{
  "assessment": "improving"
}

Errors

StatusCause
400Missing or invalid analysis parameter
401Invalid or missing API key

curl Examples

bash
# Error patterns
curl "http://localhost:3400/api/reflect?analysis=error_patterns" \
  -H "Authorization: Bearer als_your_key"

# Cost analysis for a specific agent
curl "http://localhost:3400/api/reflect?analysis=cost_analysis&agentId=my-agent&from=2026-01-01T00:00:00Z" \
  -H "Authorization: Bearer als_your_key"

# Tool sequences
curl "http://localhost:3400/api/reflect?analysis=tool_sequences&limit=20" \
  -H "Authorization: Bearer als_your_key"

# Performance trends
curl "http://localhost:3400/api/reflect?analysis=performance_trends" \
  -H "Authorization: Bearer als_your_key"

CLI Usage

bash
agentlens reflect error_patterns
agentlens reflect cost_analysis --agent my-agent --from 2026-01-01
agentlens reflect tool_sequences --limit 20
agentlens reflect performance_trends

SDK Example

typescript
import { AgentLensClient } from '@agentlensai/sdk';

const client = new AgentLensClient({
  url: 'http://localhost:3400',
  apiKey: 'als_your_key',
});

const result = await client.reflect({
  analysis: 'error_patterns',
  agentId: 'my-agent',
  from: '2026-01-01T00:00:00Z',
});

for (const insight of result.insights) {
  console.log(`[${insight.type}] ${insight.summary} (confidence: ${insight.confidence})`);
}

console.log(`Analyzed ${result.metadata.sessionsAnalyzed} sessions, ${result.metadata.eventsAnalyzed} events`);

Released under the MIT License.