toad-eye 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +290 -109
  2. package/dist/agent.d.ts +41 -0
  3. package/dist/agent.js +91 -0
  4. package/dist/budget/error.d.ts +10 -0
  5. package/dist/budget/error.js +22 -0
  6. package/dist/budget/index.d.ts +3 -0
  7. package/dist/budget/index.js +2 -0
  8. package/dist/budget/tracker.d.ts +35 -0
  9. package/dist/budget/tracker.js +121 -0
  10. package/dist/budget/types.d.ts +34 -0
  11. package/dist/budget/types.js +1 -0
  12. package/dist/cli.js +66 -28
  13. package/dist/core/metrics.d.ts +14 -0
  14. package/dist/core/metrics.js +119 -0
  15. package/dist/{spans.d.ts → core/spans.d.ts} +3 -1
  16. package/dist/core/spans.js +143 -0
  17. package/dist/core/tracer.d.ts +9 -0
  18. package/dist/core/tracer.js +93 -0
  19. package/dist/drift/baseline.d.ts +16 -0
  20. package/dist/drift/baseline.js +16 -0
  21. package/dist/drift/cosine.d.ts +13 -0
  22. package/dist/drift/cosine.js +39 -0
  23. package/dist/drift/embedding-provider.d.ts +21 -0
  24. package/dist/drift/embedding-provider.js +8 -0
  25. package/dist/drift/index.d.ts +9 -0
  26. package/dist/drift/index.js +8 -0
  27. package/dist/drift/monitor.d.ts +29 -0
  28. package/dist/drift/monitor.js +47 -0
  29. package/dist/drift/openai-embeddings.d.ts +6 -0
  30. package/dist/drift/openai-embeddings.js +26 -0
  31. package/dist/export.d.ts +46 -0
  32. package/dist/export.js +108 -0
  33. package/dist/guard.d.ts +26 -0
  34. package/dist/guard.js +45 -0
  35. package/dist/index.d.ts +14 -6
  36. package/dist/index.js +8 -3
  37. package/dist/instrumentations/anthropic.js +20 -1
  38. package/dist/instrumentations/create.js +130 -32
  39. package/dist/instrumentations/openai.js +38 -2
  40. package/dist/instrumentations/types.d.ts +9 -1
  41. package/dist/types/attributes.d.ts +12 -0
  42. package/dist/types/attributes.js +15 -0
  43. package/dist/types/config.d.ts +14 -0
  44. package/dist/types/index.d.ts +1 -1
  45. package/dist/types/metrics.d.ts +8 -0
  46. package/dist/types/metrics.js +12 -0
  47. package/dist/types/spans.d.ts +21 -0
  48. package/package.json +28 -5
  49. package/templates/grafana/dashboards/finops-attribution.json +278 -0
  50. package/dist/metrics.d.ts +0 -6
  51. package/dist/metrics.js +0 -61
  52. package/dist/spans.js +0 -87
  53. package/dist/tracer.d.ts +0 -7
  54. package/dist/tracer.js +0 -55
  55. package/dist/types.d.ts +0 -104
  56. package/dist/types.js +0 -58
  57. /package/dist/{pricing.d.ts → core/pricing.d.ts} +0 -0
  58. /package/dist/{pricing.js → core/pricing.js} +0 -0
@@ -0,0 +1,35 @@
1
+ import type { BudgetConfig, BudgetExceededMode, BudgetState, BudgetExceededInfo, DowngradeCallback } from "./types.js";
2
+ export declare class BudgetTracker {
3
+ private readonly config;
4
+ private readonly mode;
5
+ private readonly downgrade?;
6
+ private state;
7
+ constructor(config: BudgetConfig, mode: BudgetExceededMode, downgrade?: DowngradeCallback);
8
+ /** Reset counters if the day has changed. */
9
+ private resetIfNewDay;
10
+ /**
11
+ * Check budget BEFORE making an LLM call.
12
+ * Returns modified provider/model if downgrade mode triggers,
13
+ * throws ToadBudgetExceededError if block mode triggers,
14
+ * returns null if no action needed.
15
+ */
16
+ checkBefore(provider: string, model: string, userId?: string): {
17
+ provider: string;
18
+ model: string;
19
+ } | null;
20
+ /** Record cost AFTER a successful LLM call. Returns exceeded info if budget was just crossed. */
21
+ recordCost(cost: number, model: string, userId?: string): BudgetExceededInfo | null;
22
+ /** Find which budget (if any) is exceeded. */
23
+ private findExceeded;
24
+ /** Get current budget usage as percentage (0-100+). */
25
+ getUsagePercent(): number;
26
+ /** Get current state snapshot (for persistence or debugging). */
27
+ getState(): Readonly<BudgetState>;
28
+ /** Restore state from persistence (e.g., on startup). */
29
+ restoreState(saved: {
30
+ date: string;
31
+ totalCost: number;
32
+ perUser: Record<string, number>;
33
+ perModel: Record<string, number>;
34
+ }): void;
35
+ }
@@ -0,0 +1,121 @@
1
+ import { ToadBudgetExceededError } from "./error.js";
2
+ function todayUTC() {
3
+ return new Date().toISOString().slice(0, 10);
4
+ }
5
+ export class BudgetTracker {
6
+ config;
7
+ mode;
8
+ downgrade;
9
+ state;
10
+ constructor(config, mode, downgrade) {
11
+ this.config = config;
12
+ this.mode = mode;
13
+ this.downgrade = downgrade;
14
+ this.state = {
15
+ date: todayUTC(),
16
+ totalCost: 0,
17
+ perUser: new Map(),
18
+ perModel: new Map(),
19
+ };
20
+ }
21
+ /** Reset counters if the day has changed. */
22
+ resetIfNewDay() {
23
+ const today = todayUTC();
24
+ if (this.state.date !== today) {
25
+ this.state = {
26
+ date: today,
27
+ totalCost: 0,
28
+ perUser: new Map(),
29
+ perModel: new Map(),
30
+ };
31
+ }
32
+ }
33
+ /**
34
+ * Check budget BEFORE making an LLM call.
35
+ * Returns modified provider/model if downgrade mode triggers,
36
+ * throws ToadBudgetExceededError if block mode triggers,
37
+ * returns null if no action needed.
38
+ */
39
+ checkBefore(provider, model, userId) {
40
+ this.resetIfNewDay();
41
+ const exceeded = this.findExceeded(model, userId);
42
+ if (!exceeded)
43
+ return null;
44
+ if (this.mode === "block") {
45
+ throw new ToadBudgetExceededError(exceeded);
46
+ }
47
+ if (this.mode === "downgrade" && this.downgrade) {
48
+ return this.downgrade({ provider, model });
49
+ }
50
+ // 'warn' mode — continue, warning emitted after call
51
+ return null;
52
+ }
53
+ /** Record cost AFTER a successful LLM call. Returns exceeded info if budget was just crossed. */
54
+ recordCost(cost, model, userId) {
55
+ this.resetIfNewDay();
56
+ this.state.totalCost += cost;
57
+ this.state.perModel.set(model, (this.state.perModel.get(model) ?? 0) + cost);
58
+ if (userId) {
59
+ this.state.perUser.set(userId, (this.state.perUser.get(userId) ?? 0) + cost);
60
+ }
61
+ return this.findExceeded(model, userId);
62
+ }
63
+ /** Find which budget (if any) is exceeded. */
64
+ findExceeded(model, userId) {
65
+ if (this.config.daily !== undefined &&
66
+ this.state.totalCost >= this.config.daily) {
67
+ return {
68
+ budget: "daily",
69
+ limit: this.config.daily,
70
+ current: this.state.totalCost,
71
+ };
72
+ }
73
+ const modelLimit = this.config.perModel?.[model];
74
+ if (modelLimit !== undefined) {
75
+ const modelCost = this.state.perModel.get(model) ?? 0;
76
+ if (modelCost >= modelLimit) {
77
+ return {
78
+ budget: "perModel",
79
+ limit: modelLimit,
80
+ current: modelCost,
81
+ model,
82
+ };
83
+ }
84
+ }
85
+ if (this.config.perUser !== undefined && userId) {
86
+ const userCost = this.state.perUser.get(userId) ?? 0;
87
+ if (userCost >= this.config.perUser) {
88
+ return {
89
+ budget: "perUser",
90
+ limit: this.config.perUser,
91
+ current: userCost,
92
+ userId,
93
+ };
94
+ }
95
+ }
96
+ return null;
97
+ }
98
+ /** Get current budget usage as percentage (0-100+). */
99
+ getUsagePercent() {
100
+ if (this.config.daily === undefined || this.config.daily === 0)
101
+ return 0;
102
+ this.resetIfNewDay();
103
+ return (this.state.totalCost / this.config.daily) * 100;
104
+ }
105
+ /** Get current state snapshot (for persistence or debugging). */
106
+ getState() {
107
+ this.resetIfNewDay();
108
+ return this.state;
109
+ }
110
+ /** Restore state from persistence (e.g., on startup). */
111
+ restoreState(saved) {
112
+ if (saved.date !== todayUTC())
113
+ return; // stale state, ignore
114
+ this.state = {
115
+ date: saved.date,
116
+ totalCost: saved.totalCost,
117
+ perUser: new Map(Object.entries(saved.perUser)),
118
+ perModel: new Map(Object.entries(saved.perModel)),
119
+ };
120
+ }
121
+ }
@@ -0,0 +1,34 @@
1
+ /** Budget configuration for initObservability(). */
2
+ export interface BudgetConfig {
3
+ /** Max daily spend in USD for the entire application. */
4
+ readonly daily?: number | undefined;
5
+ /** Max daily spend in USD per user. Requires userId in traceLLMCall attributes. */
6
+ readonly perUser?: number | undefined;
7
+ /** Max daily spend in USD per model. */
8
+ readonly perModel?: Readonly<Record<string, number>> | undefined;
9
+ }
10
+ /** What happens when a budget is exceeded. */
11
+ export type BudgetExceededMode = "warn" | "block" | "downgrade";
12
+ /** Callback for 'downgrade' mode — receives original input, returns modified input. */
13
+ export type DowngradeCallback = (original: {
14
+ readonly provider: string;
15
+ readonly model: string;
16
+ }) => {
17
+ readonly provider: string;
18
+ readonly model: string;
19
+ };
20
+ /** Internal state tracking daily spend. */
21
+ export interface BudgetState {
22
+ date: string;
23
+ totalCost: number;
24
+ perUser: Map<string, number>;
25
+ perModel: Map<string, number>;
26
+ }
27
+ /** Info about which budget was exceeded. */
28
+ export interface BudgetExceededInfo {
29
+ readonly budget: "daily" | "perUser" | "perModel";
30
+ readonly limit: number;
31
+ readonly current: number;
32
+ readonly model?: string | undefined;
33
+ readonly userId?: string | undefined;
34
+ }
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js CHANGED
@@ -1,10 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import { execSync, spawn } from "node:child_process";
3
- import { cpSync, existsSync, mkdirSync } from "node:fs";
3
+ import { cpSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
4
4
  import { join, resolve } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  import { initObservability, traceLLMCall, shutdown, calculateCost, } from "./index.js";
7
7
  const INFRA_DIR = "infra/toad-eye";
8
+ const DEMO_DELAY_MIN_MS = 200;
9
+ const DEMO_DELAY_MAX_MS = 2000;
10
+ const DEMO_ERROR_RATE = 0.1;
11
+ const DEMO_TICK_INTERVAL_MS = 2000;
12
+ const DEMO_TOKEN_INPUT_RANGE = [50, 500];
13
+ const DEMO_TOKEN_OUTPUT_RANGE = [20, 300];
8
14
  const SERVICES = [
9
15
  { name: "Grafana", url: "http://localhost:3100", login: "admin / admin" },
10
16
  { name: "Jaeger UI", url: "http://localhost:16686" },
@@ -19,6 +25,14 @@ function getTemplatesDir() {
19
25
  function getComposeFile() {
20
26
  return join(process.cwd(), INFRA_DIR, "docker-compose.yml");
21
27
  }
28
+ function requireInfra(message = `Run \`npx toad-eye init\` first.`) {
29
+ const composeFile = getComposeFile();
30
+ if (!existsSync(composeFile)) {
31
+ console.error(`\u274c ${INFRA_DIR}/ not found. ${message}`);
32
+ process.exit(1);
33
+ }
34
+ return composeFile;
35
+ }
22
36
  function init() {
23
37
  const dest = join(process.cwd(), INFRA_DIR);
24
38
  if (existsSync(dest)) {
@@ -39,32 +53,20 @@ function init() {
39
53
  console.log(" npx toad-eye status Check running services");
40
54
  }
41
55
  function up() {
42
- const composeFile = getComposeFile();
43
- if (!existsSync(composeFile)) {
44
- console.error(`\u274c ${INFRA_DIR}/ not found. Run \`npx toad-eye init\` first.`);
45
- process.exit(1);
46
- }
56
+ const composeFile = requireInfra();
47
57
  console.log("\u{1f438} Starting observability stack...");
48
58
  execSync(`docker compose -f ${composeFile} up -d`, { stdio: "inherit" });
49
59
  console.log();
50
60
  status();
51
61
  }
52
62
  function down() {
53
- const composeFile = getComposeFile();
54
- if (!existsSync(composeFile)) {
55
- console.error(`\u274c ${INFRA_DIR}/ not found. Nothing to stop.`);
56
- process.exit(1);
57
- }
63
+ const composeFile = requireInfra("Nothing to stop.");
58
64
  console.log("\u{1f44b} Stopping observability stack...");
59
65
  execSync(`docker compose -f ${composeFile} down`, { stdio: "inherit" });
60
66
  console.log("\u2705 Stack stopped.");
61
67
  }
62
68
  function status() {
63
- const composeFile = getComposeFile();
64
- if (!existsSync(composeFile)) {
65
- console.error(`\u274c ${INFRA_DIR}/ not found. Run \`npx toad-eye init\` first.`);
66
- process.exit(1);
67
- }
69
+ const composeFile = requireInfra();
68
70
  try {
69
71
  const output = execSync(`docker compose -f ${composeFile} ps --format json`, {
70
72
  encoding: "utf-8",
@@ -104,12 +106,12 @@ function pickRandom(arr) {
104
106
  return arr[randomBetween(0, arr.length - 1)];
105
107
  }
106
108
  async function simulateLLMCall(provider, model, prompt) {
107
- await new Promise((r) => setTimeout(r, randomBetween(200, 2000)));
108
- if (Math.random() < 0.1) {
109
+ await new Promise((r) => setTimeout(r, randomBetween(DEMO_DELAY_MIN_MS, DEMO_DELAY_MAX_MS)));
110
+ if (Math.random() < DEMO_ERROR_RATE) {
109
111
  throw new Error(`${provider} API error: rate limit exceeded`);
110
112
  }
111
- const inputTokens = randomBetween(50, 500);
112
- const outputTokens = randomBetween(20, 300);
113
+ const inputTokens = randomBetween(...DEMO_TOKEN_INPUT_RANGE);
114
+ const outputTokens = randomBetween(...DEMO_TOKEN_OUTPUT_RANGE);
113
115
  return {
114
116
  completion: `Mock response for: "${prompt}"`,
115
117
  inputTokens,
@@ -118,11 +120,7 @@ async function simulateLLMCall(provider, model, prompt) {
118
120
  };
119
121
  }
120
122
  async function demo() {
121
- const composeFile = getComposeFile();
122
- if (!existsSync(composeFile)) {
123
- console.error(`\u274c ${INFRA_DIR}/ not found. Run \`npx toad-eye init\` first.`);
124
- process.exit(1);
125
- }
123
+ requireInfra();
126
124
  initObservability({
127
125
  serviceName: "toad-eye-demo",
128
126
  endpoint: "http://localhost:4318",
@@ -147,10 +145,46 @@ async function demo() {
147
145
  }
148
146
  };
149
147
  const run = () => {
150
- tick().then(() => setTimeout(run, 2000));
148
+ tick().then(() => setTimeout(run, DEMO_TICK_INTERVAL_MS));
151
149
  };
152
150
  run();
153
151
  }
152
+ async function exportTraceCommand() {
153
+ const traceId = process.argv[3];
154
+ if (!traceId) {
155
+ console.error("✗ Usage: npx toad-eye export-trace <trace_id> [--output dir] [--jaeger-url url]");
156
+ process.exit(1);
157
+ }
158
+ const args = process.argv.slice(4);
159
+ let outputDir = process.cwd();
160
+ let jaegerUrl;
161
+ for (let i = 0; i < args.length; i++) {
162
+ if (args[i] === "--output" && args[i + 1]) {
163
+ outputDir = resolve(args[i + 1]);
164
+ i++;
165
+ }
166
+ else if (args[i] === "--jaeger-url" && args[i + 1]) {
167
+ jaegerUrl = args[i + 1];
168
+ i++;
169
+ }
170
+ }
171
+ const { exportTrace } = await import("./export.js");
172
+ try {
173
+ const yaml = await exportTrace(traceId, { jaegerUrl });
174
+ const filename = `trace-${traceId.slice(0, 8)}.eval.yaml`;
175
+ const filepath = join(outputDir, filename);
176
+ if (!existsSync(outputDir)) {
177
+ mkdirSync(outputDir, { recursive: true });
178
+ }
179
+ writeFileSync(filepath, yaml, "utf-8");
180
+ console.log(`✅ Exported trace ${traceId} → ${filepath}`);
181
+ }
182
+ catch (error) {
183
+ const msg = error instanceof Error ? error.message : String(error);
184
+ console.error(`✗ ${msg}`);
185
+ process.exit(1);
186
+ }
187
+ }
154
188
  function help() {
155
189
  console.log(`
156
190
  \u{1f438} toad-eye CLI — observability stack for LLM services
@@ -160,8 +194,9 @@ Commands:
160
194
  up Start the stack (OTel Collector + Prometheus + Jaeger + Grafana)
161
195
  down Stop the stack
162
196
  status Show running services and URLs
163
- demo Send mock LLM traffic to see data in Grafana
164
- help Show this message
197
+ demo Send mock LLM traffic to see data in Grafana
198
+ export-trace Export a Jaeger trace to toad-eval YAML
199
+ help Show this message
165
200
  `);
166
201
  }
167
202
  const command = process.argv[2];
@@ -181,6 +216,9 @@ switch (command) {
181
216
  case "demo":
182
217
  demo();
183
218
  break;
219
+ case "export-trace":
220
+ exportTraceCommand();
221
+ break;
184
222
  case "help":
185
223
  case "--help":
186
224
  case "-h":
@@ -0,0 +1,14 @@
1
+ export declare function initMetrics(): void;
2
+ export declare function recordRequestDuration(ms: number, provider: string, model: string, attrs?: Record<string, string>): void;
3
+ export declare function recordRequestCost(usd: number, provider: string, model: string, attrs?: Record<string, string>): void;
4
+ export declare function recordTokens(count: number, provider: string, model: string, attrs?: Record<string, string>): void;
5
+ export declare function recordRequest(provider: string, model: string, attrs?: Record<string, string>): void;
6
+ export declare function recordError(provider: string, model: string, attrs?: Record<string, string>): void;
7
+ export declare function recordAgentSteps(stepCount: number): void;
8
+ export declare function recordAgentToolUsage(toolName: string): void;
9
+ export declare function recordGuardEvaluation(ruleName: string): void;
10
+ export declare function recordGuardWouldBlock(ruleName: string): void;
11
+ export declare function recordSemanticDrift(drift: number, provider: string, model: string): void;
12
+ export declare function recordBudgetExceeded(budgetType: string): void;
13
+ export declare function recordBudgetBlocked(budgetType: string): void;
14
+ export declare function recordBudgetDowngraded(budgetType: string): void;
@@ -0,0 +1,119 @@
1
+ import { metrics } from "@opentelemetry/api";
2
+ import { GEN_AI_ATTRS, GEN_AI_METRICS, INSTRUMENTATION_NAME, } from "../types/index.js";
3
+ let requestDuration;
4
+ let requestCost;
5
+ let tokenUsage;
6
+ let requestsTotal;
7
+ let errorsTotal;
8
+ let agentStepsPerQuery;
9
+ let agentToolUsage;
10
+ let guardEvaluations;
11
+ let guardWouldBlock;
12
+ let semanticDrift;
13
+ let budgetExceeded;
14
+ let budgetBlocked;
15
+ let budgetDowngraded;
16
+ let initialized = false;
17
+ export function initMetrics() {
18
+ if (initialized)
19
+ return;
20
+ const meter = metrics.getMeter(INSTRUMENTATION_NAME);
21
+ requestDuration = meter.createHistogram(GEN_AI_METRICS.REQUEST_DURATION, {
22
+ description: "GenAI operation duration in milliseconds",
23
+ unit: "ms",
24
+ });
25
+ requestCost = meter.createHistogram(GEN_AI_METRICS.REQUEST_COST, {
26
+ description: "GenAI request cost in USD",
27
+ unit: "USD",
28
+ });
29
+ tokenUsage = meter.createCounter(GEN_AI_METRICS.TOKEN_USAGE, {
30
+ description: "Total tokens consumed",
31
+ });
32
+ requestsTotal = meter.createCounter(GEN_AI_METRICS.REQUESTS, {
33
+ description: "Total GenAI requests",
34
+ });
35
+ errorsTotal = meter.createCounter(GEN_AI_METRICS.ERRORS, {
36
+ description: "Total failed GenAI requests",
37
+ });
38
+ agentStepsPerQuery = meter.createHistogram(GEN_AI_METRICS.AGENT_STEPS_PER_QUERY, {
39
+ description: "Number of agent steps per query",
40
+ });
41
+ agentToolUsage = meter.createCounter(GEN_AI_METRICS.AGENT_TOOL_USAGE, {
42
+ description: "Agent tool invocations by tool name",
43
+ });
44
+ guardEvaluations = meter.createCounter(GEN_AI_METRICS.GUARD_EVALUATIONS, {
45
+ description: "Total guard evaluations (shadow + enforce)",
46
+ });
47
+ guardWouldBlock = meter.createCounter(GEN_AI_METRICS.GUARD_WOULD_BLOCK, {
48
+ description: "Guard evaluations that would have blocked the response",
49
+ });
50
+ semanticDrift = meter.createHistogram(GEN_AI_METRICS.SEMANTIC_DRIFT, {
51
+ description: "Semantic drift from baseline (0 = identical, 1 = completely different)",
52
+ });
53
+ budgetExceeded = meter.createCounter(GEN_AI_METRICS.BUDGET_EXCEEDED, {
54
+ description: "Number of times a budget limit was exceeded",
55
+ });
56
+ budgetBlocked = meter.createCounter(GEN_AI_METRICS.BUDGET_BLOCKED, {
57
+ description: "Number of LLM calls blocked due to budget limits",
58
+ });
59
+ budgetDowngraded = meter.createCounter(GEN_AI_METRICS.BUDGET_DOWNGRADED, {
60
+ description: "Number of LLM calls downgraded due to budget limits",
61
+ });
62
+ initialized = true;
63
+ }
64
+ /** Build metric labels: provider + model + optional FinOps attributes. */
65
+ function baseLabels(provider, model, attrs) {
66
+ return {
67
+ [GEN_AI_ATTRS.PROVIDER]: provider,
68
+ [GEN_AI_ATTRS.REQUEST_MODEL]: model,
69
+ ...attrs,
70
+ };
71
+ }
72
+ export function recordRequestDuration(ms, provider, model, attrs) {
73
+ requestDuration.record(ms, baseLabels(provider, model, attrs));
74
+ }
75
+ export function recordRequestCost(usd, provider, model, attrs) {
76
+ requestCost.record(usd, baseLabels(provider, model, attrs));
77
+ }
78
+ export function recordTokens(count, provider, model, attrs) {
79
+ tokenUsage.add(count, baseLabels(provider, model, attrs));
80
+ }
81
+ export function recordRequest(provider, model, attrs) {
82
+ requestsTotal.add(1, baseLabels(provider, model, attrs));
83
+ }
84
+ export function recordError(provider, model, attrs) {
85
+ errorsTotal.add(1, baseLabels(provider, model, attrs));
86
+ }
87
+ export function recordAgentSteps(stepCount) {
88
+ agentStepsPerQuery.record(stepCount);
89
+ }
90
+ export function recordAgentToolUsage(toolName) {
91
+ agentToolUsage.add(1, {
92
+ [GEN_AI_ATTRS.AGENT_TOOL_NAME]: toolName,
93
+ });
94
+ }
95
+ export function recordGuardEvaluation(ruleName) {
96
+ guardEvaluations.add(1, {
97
+ [GEN_AI_ATTRS.GUARD_RULE_NAME]: ruleName,
98
+ });
99
+ }
100
+ export function recordGuardWouldBlock(ruleName) {
101
+ guardWouldBlock.add(1, {
102
+ [GEN_AI_ATTRS.GUARD_RULE_NAME]: ruleName,
103
+ });
104
+ }
105
+ export function recordSemanticDrift(drift, provider, model) {
106
+ semanticDrift.record(drift, {
107
+ [GEN_AI_ATTRS.PROVIDER]: provider,
108
+ [GEN_AI_ATTRS.REQUEST_MODEL]: model,
109
+ });
110
+ }
111
+ export function recordBudgetExceeded(budgetType) {
112
+ budgetExceeded.add(1, { budget_type: budgetType });
113
+ }
114
+ export function recordBudgetBlocked(budgetType) {
115
+ budgetBlocked.add(1, { budget_type: budgetType });
116
+ }
117
+ export function recordBudgetDowngraded(budgetType) {
118
+ budgetDowngraded.add(1, { budget_type: budgetType });
119
+ }
@@ -1,10 +1,12 @@
1
- import type { LLMSpanAttributes } from "./types/index.js";
1
+ import type { LLMSpanAttributes } from "../types/index.js";
2
2
  /** Input for traceLLMCall — what the user knows before calling the LLM */
3
3
  export interface LLMCallInput {
4
4
  readonly provider: LLMSpanAttributes["provider"];
5
5
  readonly model: string;
6
6
  readonly prompt: string;
7
7
  readonly temperature?: number | undefined;
8
+ /** Per-request FinOps attributes (team, userId, feature, etc). Merged with global attributes. */
9
+ readonly attributes?: Readonly<Record<string, string>> | undefined;
8
10
  }
9
11
  /** Output from the LLM call — what comes back */
10
12
  export interface LLMCallOutput {
@@ -0,0 +1,143 @@
1
+ import { createHash } from "node:crypto";
2
+ import { trace, SpanStatusCode } from "@opentelemetry/api";
3
+ import { GEN_AI_ATTRS, INSTRUMENTATION_NAME } from "../types/index.js";
4
+ import { recordRequestDuration, recordRequestCost, recordTokens, recordRequest, recordError, recordBudgetExceeded, recordBudgetBlocked, recordBudgetDowngraded, } from "./metrics.js";
5
+ import { getConfig, getBudgetTracker } from "./tracer.js";
6
+ import { GEN_AI_ATTRS as ATTRS } from "../types/index.js";
7
+ const tracer = trace.getTracer(INSTRUMENTATION_NAME);
8
+ function sha256(text) {
9
+ const salt = getConfig()?.salt ?? "";
10
+ return createHash("sha256")
11
+ .update(salt + text)
12
+ .digest("hex");
13
+ }
14
+ function processContent(text) {
15
+ const config = getConfig();
16
+ if (config?.recordContent === false)
17
+ return undefined;
18
+ let processed = text;
19
+ if (config?.redactPatterns?.length) {
20
+ for (const pattern of config.redactPatterns) {
21
+ processed = processed.replace(pattern, "[REDACTED]");
22
+ }
23
+ }
24
+ if (config?.hashContent) {
25
+ return `sha256:${sha256(processed)}`;
26
+ }
27
+ return processed;
28
+ }
29
+ function resolveSessionId() {
30
+ const config = getConfig();
31
+ return config?.sessionExtractor?.() ?? config?.sessionId;
32
+ }
33
+ /** Merge global config attributes with per-request attributes (per-request wins). */
34
+ function resolveAttributes(input) {
35
+ const global = getConfig()?.attributes;
36
+ const local = input.attributes;
37
+ if (!global && !local)
38
+ return undefined;
39
+ return { ...global, ...local };
40
+ }
41
+ function setBaseAttributes(span, input) {
42
+ const prompt = processContent(input.prompt);
43
+ const sessionId = resolveSessionId();
44
+ const attrs = resolveAttributes(input);
45
+ span.setAttributes({
46
+ [GEN_AI_ATTRS.PROVIDER]: input.provider,
47
+ [GEN_AI_ATTRS.REQUEST_MODEL]: input.model,
48
+ [GEN_AI_ATTRS.TEMPERATURE]: input.temperature ?? 1.0,
49
+ [GEN_AI_ATTRS.OPERATION]: "chat",
50
+ ...(prompt !== undefined && { [GEN_AI_ATTRS.PROMPT]: prompt }),
51
+ ...(sessionId !== undefined && { [GEN_AI_ATTRS.SESSION_ID]: sessionId }),
52
+ ...attrs,
53
+ });
54
+ }
55
+ function recordBaseMetrics(duration, provider, model, attrs) {
56
+ recordRequest(provider, model, attrs);
57
+ recordRequestDuration(duration, provider, model, attrs);
58
+ }
59
+ function setSuccessAttributes(span, input, output) {
60
+ const completion = processContent(output.completion);
61
+ span.setAttributes({
62
+ ...(completion !== undefined && { [GEN_AI_ATTRS.COMPLETION]: completion }),
63
+ [GEN_AI_ATTRS.RESPONSE_MODEL]: input.model,
64
+ [GEN_AI_ATTRS.INPUT_TOKENS]: output.inputTokens,
65
+ [GEN_AI_ATTRS.OUTPUT_TOKENS]: output.outputTokens,
66
+ [GEN_AI_ATTRS.COST]: output.cost,
67
+ [GEN_AI_ATTRS.STATUS]: "success",
68
+ [GEN_AI_ATTRS.FINISH_REASONS]: ["stop"],
69
+ });
70
+ span.setStatus({ code: SpanStatusCode.OK });
71
+ }
72
+ function setErrorAttributes(span, message) {
73
+ const sanitized = processContent(message);
74
+ span.setAttributes({
75
+ [GEN_AI_ATTRS.INPUT_TOKENS]: 0,
76
+ [GEN_AI_ATTRS.OUTPUT_TOKENS]: 0,
77
+ [GEN_AI_ATTRS.COST]: 0,
78
+ [GEN_AI_ATTRS.STATUS]: "error",
79
+ ...(sanitized !== undefined && { [GEN_AI_ATTRS.ERROR]: sanitized }),
80
+ });
81
+ span.setStatus({
82
+ code: SpanStatusCode.ERROR,
83
+ ...(sanitized !== undefined && { message: sanitized }),
84
+ });
85
+ }
86
+ export async function traceLLMCall(input, fn) {
87
+ const budget = getBudgetTracker();
88
+ let effectiveInput = input;
89
+ // Budget check BEFORE the LLM call
90
+ if (budget) {
91
+ const userId = input.attributes?.[ATTRS.USER_ID];
92
+ const override = budget.checkBefore(input.provider, input.model, userId);
93
+ if (override) {
94
+ // Downgrade mode — use modified provider/model
95
+ effectiveInput = {
96
+ ...input,
97
+ provider: override.provider,
98
+ model: override.model,
99
+ };
100
+ recordBudgetDowngraded("daily");
101
+ }
102
+ }
103
+ return tracer.startActiveSpan(`gen_ai.${effectiveInput.provider}.${effectiveInput.model}`, async (span) => {
104
+ const start = performance.now();
105
+ setBaseAttributes(span, effectiveInput);
106
+ try {
107
+ const output = await fn();
108
+ const duration = performance.now() - start;
109
+ const attrs = resolveAttributes(effectiveInput);
110
+ setSuccessAttributes(span, effectiveInput, output);
111
+ recordBaseMetrics(duration, effectiveInput.provider, effectiveInput.model, attrs);
112
+ recordRequestCost(output.cost, effectiveInput.provider, effectiveInput.model, attrs);
113
+ recordTokens(output.inputTokens + output.outputTokens, effectiveInput.provider, effectiveInput.model, attrs);
114
+ // Budget recording AFTER the call
115
+ if (budget) {
116
+ const userId = effectiveInput.attributes?.[ATTRS.USER_ID];
117
+ const exceeded = budget.recordCost(output.cost, effectiveInput.model, userId);
118
+ if (exceeded) {
119
+ recordBudgetExceeded(exceeded.budget);
120
+ console.warn(`toad-eye: ${exceeded.budget} budget exceeded — limit $${exceeded.limit}, current $${exceeded.current.toFixed(2)}`);
121
+ }
122
+ }
123
+ return output;
124
+ }
125
+ catch (error) {
126
+ const duration = performance.now() - start;
127
+ const message = error instanceof Error ? error.message : String(error);
128
+ const attrs = resolveAttributes(effectiveInput);
129
+ setErrorAttributes(span, message);
130
+ recordBaseMetrics(duration, effectiveInput.provider, effectiveInput.model, attrs);
131
+ recordError(effectiveInput.provider, effectiveInput.model, attrs);
132
+ // If this was a budget block, record the metric
133
+ if (error instanceof Error &&
134
+ error.name === "ToadBudgetExceededError") {
135
+ recordBudgetBlocked("daily");
136
+ }
137
+ throw error;
138
+ }
139
+ finally {
140
+ span.end();
141
+ }
142
+ });
143
+ }
@@ -0,0 +1,9 @@
1
+ import type { ToadEyeConfig } from "../types/index.js";
2
+ import { BudgetTracker } from "../budget/index.js";
3
+ import "../instrumentations/openai.js";
4
+ import "../instrumentations/anthropic.js";
5
+ import "../instrumentations/gemini.js";
6
+ export declare function getConfig(): ToadEyeConfig | null;
7
+ export declare function getBudgetTracker(): BudgetTracker | null;
8
+ export declare function initObservability(config: ToadEyeConfig): void;
9
+ export declare function shutdown(): Promise<void>;