Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | 30x 30x 30x 30x 4x 4x 4x 4x 4x 4x 4x 4x 1x 106x 106x 106x 106x 13x 226x 226x 215x 11x 226x 226x 226x 338x 338x 2x 2x 336x 2x 2x 334x 48x 48x 286x 207x 79x 2x 2x 77x 2x 2x 75x 75x 12x 12x 12x 8x 8x 8x 8x 5x | import Anthropic from '@anthropic-ai/sdk';
import type { Span } from '@sentry/node';
import type { z } from 'zod';
import type { UsageStats } from '../types/index.js';
import { Sentry } from '../sentry.js';
import { apiUsageToStats } from './pricing.js';
import { aggregateUsage, emptyUsage } from './usage.js';
import {
setGenAiInputMessagesAttr,
setGenAiOutputMessagesAttr,
setGenAiUsageAttrs,
} from './otel.js';
export const HAIKU_MODEL = 'claude-haiku-4-5';
export const DEFAULT_AUXILIARY_MAX_RETRIES = 5;
const DEFAULT_TIMEOUT_MS = 30_000;
const DEFAULT_MAX_TOKENS = 4096;
/**
* Anthropic Messages API usage shape accepted by setGenAiResponseAttrs.
*/
interface ApiResponseUsage {
input_tokens: number;
output_tokens: number;
cache_read_input_tokens?: number | null;
cache_creation_input_tokens?: number | null;
cache_creation?: {
ephemeral_1h_input_tokens?: number | null;
ephemeral_5m_input_tokens?: number | null;
} | null;
}
/**
* Set standard gen_ai response attributes on a Sentry span.
*
* Follows the same token accounting as analyze.ts: gen_ai.usage.input_tokens
* is the total (non-cached + cache_read + cache_creation), with cache fields
* as subsets.
*/
export function setGenAiResponseAttrs(
span: Span,
usage: ApiResponseUsage,
stopReason?: string | null,
responseText?: string
): void {
const cacheRead = usage.cache_read_input_tokens ?? 0;
const rawCacheWrite = usage.cache_creation_input_tokens ?? 0;
const tieredCacheWrite =
(usage.cache_creation?.ephemeral_5m_input_tokens ?? 0) +
(usage.cache_creation?.ephemeral_1h_input_tokens ?? 0);
const cacheWrite = Math.max(rawCacheWrite, tieredCacheWrite);
setGenAiUsageAttrs(span, {
inputTokens: usage.input_tokens + cacheRead + cacheWrite,
outputTokens: usage.output_tokens,
cacheReadInputTokens: cacheRead,
cacheCreationInputTokens: cacheWrite,
cacheCreation5mInputTokens: usage.cache_creation?.ephemeral_5m_input_tokens ?? cacheWrite,
cacheCreation1hInputTokens: usage.cache_creation?.ephemeral_1h_input_tokens ?? 0,
webSearchRequests: 0,
costUSD: 0,
});
Eif (stopReason) {
span.setAttribute('gen_ai.response.finish_reasons', [stopReason]);
}
if (responseText !== undefined) {
setGenAiOutputMessagesAttr(span, responseText, stopReason);
}
}
/**
* Extract the first JSON object or array from LLM text.
* Handles markdown code fences and prose before/after JSON.
*/
export function extractJson(text: string): string | null {
const stripped = text.trim();
// Try parsing the whole thing first (common case: clean JSON output)
try {
JSON.parse(stripped);
return stripped;
} catch {
// Fall through to extraction
}
// Try every object/array opener. This handles prose, fenced JSON, orphaned
// prefill, and markdown fences embedded inside JSON string values.
for (let start = 0; start < stripped.length; start++) {
const opener = stripped[start];
if (opener !== '{' && opener !== '[') {
continue;
}
const stack = [opener === '{' ? '}' : ']'];
let inString = false;
let escape = false;
for (let i = start + 1; i < stripped.length; i++) {
const char = stripped[i];
if (escape) {
escape = false;
continue;
}
if (char === '\\' && inString) {
escape = true;
continue;
}
if (char === '"') {
inString = !inString;
continue;
}
if (inString) {
continue;
}
if (char === '{') {
stack.push('}');
continue;
}
if (char === '[') {
stack.push(']');
continue;
}
const expectedCloser = stack[stack.length - 1];
if (char === '}' || char === ']') {
Iif (char !== expectedCloser) {
break;
}
stack.pop();
if (stack.length === 0) {
const candidate = stripped.slice(start, i + 1);
try {
JSON.parse(candidate);
return candidate;
} catch {
break;
}
}
}
}
}
return null;
}
/**
* Result from a structured Haiku call.
*/
export type HaikuResult<T> =
| { success: true; data: T; usage: UsageStats }
| { success: false; error: string; usage: UsageStats };
/**
* Options for callHaiku.
*/
export interface CallHaikuOptions<T> {
apiKey: string;
prompt: string;
schema: z.ZodType<T>;
agentName?: string;
task?: string;
model?: string;
maxTokens?: number;
timeout?: number;
maxRetries?: number;
}
/**
* Infer prefill character from schema type to force JSON output.
*/
function inferPrefill(schema: z.ZodType): string | undefined {
// Check for ZodObject (name === 'ZodObject')
if ('_def' in schema && (schema as { _def: { typeName?: string } })._def.typeName === 'ZodObject') return '{';
// Check for ZodArray
if ('_def' in schema && (schema as { _def: { typeName?: string } })._def.typeName === 'ZodArray') return '[';
return undefined;
}
/**
* Single-turn structured Haiku call.
* Auto-prefills based on Zod schema type, extracts JSON, validates with Zod.
*/
export async function callHaiku<T>(options: CallHaikuOptions<T>): Promise<HaikuResult<T>> {
const { apiKey, prompt, schema, agentName, task, model = HAIKU_MODEL, maxTokens = DEFAULT_MAX_TOKENS, timeout = DEFAULT_TIMEOUT_MS, maxRetries = DEFAULT_AUXILIARY_MAX_RETRIES } = options;
return Sentry.startSpan(
{
op: 'gen_ai.chat',
name: `chat ${model}`,
attributes: {
'gen_ai.operation.name': 'chat',
'gen_ai.provider.name': 'anthropic',
...(agentName ? { 'gen_ai.agent.name': agentName } : {}),
...(task ? { 'warden.ai.task': task } : {}),
'gen_ai.request.model': model,
'gen_ai.request.max_tokens': maxTokens,
'gen_ai.output.type': 'json',
},
},
async (span) => {
const client = new Anthropic({ apiKey, timeout, maxRetries });
const prefill = inferPrefill(schema);
const messages: Anthropic.MessageParam[] = [
{ role: 'user', content: prompt },
];
if (prefill) {
messages.push({ role: 'assistant', content: prefill });
}
setGenAiInputMessagesAttr(span, messages);
try {
const response = await client.messages.create({
model,
max_tokens: maxTokens,
messages,
});
const usage = apiUsageToStats(model, response.usage);
const content = response.content[0];
if (!content || content.type !== 'text') {
setGenAiResponseAttrs(span, response.usage, response.stop_reason);
span.setAttribute('error.type', 'empty_response');
return { success: false, error: 'Empty response from model', usage };
}
let fullText = content.text;
if (prefill) {
fullText = prefill + fullText;
}
setGenAiResponseAttrs(span, response.usage, response.stop_reason, fullText);
const jsonStr = extractJson(fullText);
if (!jsonStr) {
span.setAttribute('error.type', 'invalid_json');
return { success: false, error: 'No JSON found in response', usage };
}
const parsed = JSON.parse(jsonStr);
const validated = schema.safeParse(parsed);
if (!validated.success) {
span.setAttribute('error.type', 'validation_error');
return { success: false, error: `Validation failed: ${validated.error.message}`, usage };
}
return { success: true, data: validated.data, usage };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
span.setAttribute('error.type', error instanceof Error ? error.name : '_OTHER');
return { success: false, error: message, usage: emptyUsage() };
}
},
);
}
/**
* Options for callHaikuWithTools.
*/
export interface CallHaikuWithToolsOptions<T> {
apiKey: string;
prompt: string;
schema: z.ZodType<T>;
tools: Anthropic.Tool[];
executeTool: (name: string, input: Record<string, unknown>) => Promise<string>;
agentName?: string;
task?: string;
model?: string;
maxTokens?: number;
maxIterations?: number;
timeout?: number;
maxRetries?: number;
}
/**
* Multi-turn Haiku call with tool use loop.
* Iterates tool calls until the model produces a final text response.
* Accumulates usage across all iterations.
*/
export async function callHaikuWithTools<T>(options: CallHaikuWithToolsOptions<T>): Promise<HaikuResult<T>> {
const {
apiKey,
prompt,
schema,
tools,
executeTool,
agentName,
task,
model = HAIKU_MODEL,
maxTokens = DEFAULT_MAX_TOKENS,
maxIterations = 5,
timeout = DEFAULT_TIMEOUT_MS,
maxRetries = DEFAULT_AUXILIARY_MAX_RETRIES,
} = options;
return Sentry.startSpan(
{
op: 'gen_ai.chat',
name: `chat ${model}`,
attributes: {
'gen_ai.operation.name': 'chat',
'gen_ai.provider.name': 'anthropic',
...(agentName ? { 'gen_ai.agent.name': agentName } : {}),
...(task ? { 'warden.ai.task': task } : {}),
'gen_ai.request.model': model,
'gen_ai.request.max_tokens': maxTokens,
'gen_ai.output.type': 'json',
},
},
async (span) => {
const client = new Anthropic({ apiKey, timeout, maxRetries });
// No prefill for tool-use loops: prefill biases the model to output JSON
// immediately instead of calling tools to gather information first.
const messages: Anthropic.MessageParam[] = [
{ role: 'user', content: prompt },
];
setGenAiInputMessagesAttr(span, messages);
const usages: UsageStats[] = [];
// Accumulate raw API usage across iterations so setGenAiResponseAttrs
// can compute totals consistently (input_tokens + cache subsets).
const cumulativeUsage = {
input_tokens: 0,
output_tokens: 0,
cache_read_input_tokens: 0,
cache_creation_input_tokens: 0,
cache_creation: {
ephemeral_5m_input_tokens: 0,
ephemeral_1h_input_tokens: 0,
},
};
function setFinalSpanAttrs(stopReason?: string | null, responseText?: string): void {
setGenAiResponseAttrs(span, cumulativeUsage, stopReason, responseText);
}
function currentUsage(): UsageStats {
return usages.length > 0 ? aggregateUsage(usages) : emptyUsage();
}
for (let iteration = 0; iteration < maxIterations; iteration++) {
let response: Anthropic.Message;
try {
response = await client.messages.create({
model,
max_tokens: maxTokens,
messages,
tools,
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
span.setAttribute('error.type', error instanceof Error ? error.name : '_OTHER');
return { success: false, error: message, usage: currentUsage() };
}
usages.push(apiUsageToStats(model, response.usage));
cumulativeUsage.input_tokens += response.usage.input_tokens;
cumulativeUsage.output_tokens += response.usage.output_tokens;
cumulativeUsage.cache_read_input_tokens += response.usage.cache_read_input_tokens ?? 0;
cumulativeUsage.cache_creation_input_tokens += response.usage.cache_creation_input_tokens ?? 0;
cumulativeUsage.cache_creation.ephemeral_5m_input_tokens +=
response.usage.cache_creation?.ephemeral_5m_input_tokens ?? 0;
cumulativeUsage.cache_creation.ephemeral_1h_input_tokens +=
response.usage.cache_creation?.ephemeral_1h_input_tokens ?? 0;
// Handle tool use
if (response.stop_reason === 'tool_use') {
const toolUseBlocks = response.content.filter(
(b): b is Anthropic.ToolUseBlock => b.type === 'tool_use'
);
if (toolUseBlocks.length === 0) {
span.setAttribute('error.type', 'missing_tool_call');
return { success: false, error: 'Tool use indicated but no tool calls found', usage: aggregateUsage(usages) };
}
const toolResults: Anthropic.ToolResultBlockParam[] = [];
for (const block of toolUseBlocks) {
await Sentry.startSpan(
{
op: 'gen_ai.execute_tool',
name: `execute_tool ${block.name}`,
attributes: {
'gen_ai.operation.name': 'execute_tool',
...(agentName ? { 'gen_ai.agent.name': agentName } : {}),
...(task ? { 'warden.ai.task': task } : {}),
'gen_ai.tool.name': block.name,
},
},
async () => {
try {
const result = await executeTool(block.name, block.input as Record<string, unknown>);
toolResults.push({ type: 'tool_result', tool_use_id: block.id, content: result });
} catch (error) {
const errMsg = error instanceof Error ? error.message : String(error);
toolResults.push({ type: 'tool_result', tool_use_id: block.id, content: errMsg, is_error: true });
}
},
);
}
messages.push({ role: 'assistant', content: response.content });
messages.push({ role: 'user', content: toolResults });
continue;
}
// Final response - extract text and set span attributes
if (response.stop_reason !== 'end_turn' && response.stop_reason !== 'max_tokens') {
setFinalSpanAttrs(response.stop_reason);
span.setAttribute('error.type', 'unexpected_stop_reason');
return { success: false, error: `Unexpected stop reason: ${response.stop_reason}`, usage: aggregateUsage(usages) };
}
const textBlock = response.content.find(
(b): b is Anthropic.TextBlock => b.type === 'text'
);
if (!textBlock) {
setFinalSpanAttrs(response.stop_reason);
span.setAttribute('error.type', 'empty_response');
return { success: false, error: 'No text in final response', usage: aggregateUsage(usages) };
}
setFinalSpanAttrs(response.stop_reason, textBlock.text);
const jsonStr = extractJson(textBlock.text);
if (!jsonStr) {
span.setAttribute('error.type', 'invalid_json');
return { success: false, error: 'No JSON found in response', usage: aggregateUsage(usages) };
}
const parsed = JSON.parse(jsonStr);
const validated = schema.safeParse(parsed);
if (!validated.success) {
span.setAttribute('error.type', 'validation_error');
return { success: false, error: `Validation failed: ${validated.error.message}`, usage: aggregateUsage(usages) };
}
return { success: true, data: validated.data, usage: aggregateUsage(usages) };
}
// Max iterations exceeded - still record usage on span
setFinalSpanAttrs();
span.setAttribute('error.type', 'max_tool_iterations');
return { success: false, error: 'Max tool iterations exceeded', usage: aggregateUsage(usages) };
},
);
}
|