コンテキスト可観測性
コンテキストを観測・制御可能な資産に変換します。
コンテキストSLOs
コンテキストメトリクスにSLOを定義します。
設定
.lean-ctxにslos.tomlを作成します。
# .lean-ctx/slos.toml
[[slo]]
name = "context_budget"
metric = "session_context_tokens"
threshold = 50000
action = "warn" # warn | throttle | block
[[slo]]
name = "cost_per_session"
metric = "session_cost_usd"
threshold = 1.00
action = "throttle"
[[slo]]
name = "compression_efficiency"
metric = "compression_ratio"
threshold = 0.3
direction = "min"
action = "warn" 利用可能なメトリクス
session_context_tokens 現在のセッションの合計トークン数
session_cost_usd セッションの推定USDコスト
compression_ratio 圧縮比率
shell_invocations シェルコマンド数
tool_calls_total MCPツール呼び出し総数
SLOアクション
| アクション | 動作 |
|---|---|
warn | 出力に警告を追加 |
throttle | 警告し密度を削減 |
block | 非必須の呼び出しをブロック |
MCPコマンド
ctx_session slo # Show current SLO status
ctx_session slo reload # Reload SLOs from disk
ctx_session slo history # Show violation history
ctx_session slo clear # Clear violation log 異常検知
異常なトークン消費パターンを自動検出。
仕組み
Welfordアルゴリズムを使用:
- 50回のスライディングウィンドウEMA
- >3x標準偏差でアラート
- TUIとダッシュボードで表示
追跡メトリクス
tokens_per_callを追跡中。
Prometheusメトリクス
Prometheus形式でエクスポート。
エンドポイント
# Start dashboard with metrics endpoint
lean-ctx dashboard
# Scrape metrics
curl http://localhost:3000/metrics 利用可能なメトリクス
lean_ctx_tokens_saved_total
lean_ctx_compression_ratio
lean_ctx_session_cost_usd
lean_ctx_tool_calls_total
lean_ctx_slo_violations_total
lean_ctx_cache_hit_rate
lean_ctx_anomalies_total
lean_ctx_session_uptime_seconds Grafana統合
/metricsへスクレイパーを向けてください。
PIIゼロ保証
カウンター、ゲージ、比率のみエクスポート。
セッション差分
2つのセッションを並べて比較。
使用方法
# Compare two sessions (MCP)
ctx_session diff <session_a> <session_b>
ctx_session diff <session_a> <session_b> json
# List available sessions
ctx_session list 比較内容
完全なセッション構造を比較:
- ファイル: 追加、削除、モード変更
- 発見: 新しい観察vs前回
- 決定: アーキテクチャ選択
- 統計: ツール呼び出し、節約トークン、コマンド
ダッシュボードAPI
| エンドポイント | 説明 |
|---|---|
/api/slos | SLOステータスと違反履歴 |
/api/anomaly | 異常検知サマリー |
/metrics | Prometheus形式エクスポート |
/api/events | イベントログ(直近200件) |