@aigne/core 1.72.0-beta.2 → 1.72.0-beta.3

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.
@@ -27,35 +27,38 @@ export declare class ChatMessageTemplate {
27
27
  content?: ChatModelInputMessage["content"];
28
28
  name?: string | undefined;
29
29
  options?: FormatOptions | undefined;
30
- constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined);
30
+ cacheControl?: ChatModelInputMessage["cacheControl"];
31
+ constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined, options?: FormatOptions | undefined, cacheControl?: ChatModelInputMessage["cacheControl"]);
31
32
  format(variables?: Record<string, unknown>, options?: FormatOptions): Promise<ChatModelInputMessage>;
32
33
  }
33
34
  export declare class SystemMessageTemplate extends ChatMessageTemplate {
34
- static from(content: string, name?: string, options?: FormatOptions): SystemMessageTemplate;
35
+ static from(content: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): SystemMessageTemplate;
35
36
  }
36
37
  export declare class UserMessageTemplate extends ChatMessageTemplate {
37
- static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions): UserMessageTemplate;
38
+ static from(template: ChatModelInputMessageContent, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): UserMessageTemplate;
38
39
  }
39
40
  export declare class AgentMessageTemplate extends ChatMessageTemplate {
40
41
  toolCalls?: ChatModelOutputToolCall[] | undefined;
41
- static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions): AgentMessageTemplate;
42
- constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions);
42
+ static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): AgentMessageTemplate;
43
+ constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
43
44
  format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
44
45
  role: "agent" | "system" | "user" | "tool";
45
46
  name: string | undefined;
46
47
  content: ChatModelInputMessageContent | undefined;
47
48
  toolCalls: ChatModelOutputToolCall[] | undefined;
49
+ cacheControl: import("../agents/chat-model.js").CacheControl | undefined;
48
50
  }>;
49
51
  }
50
52
  export declare class ToolMessageTemplate extends ChatMessageTemplate {
51
53
  toolCallId: string;
52
- static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions): ToolMessageTemplate;
53
- constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions);
54
+ static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): ToolMessageTemplate;
55
+ constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
54
56
  format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
55
57
  role: "agent" | "system" | "user" | "tool";
56
58
  name: string | undefined;
57
59
  content: ChatModelInputMessageContent | undefined;
58
60
  toolCallId: string;
61
+ cacheControl: import("../agents/chat-model.js").CacheControl | undefined;
59
62
  }>;
60
63
  }
61
64
  export declare class ChatMessagesTemplate {
@@ -69,26 +72,62 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
69
72
  role: z.ZodLiteral<"system">;
70
73
  content: z.ZodString;
71
74
  name: z.ZodOptional<z.ZodString>;
75
+ cacheControl: z.ZodOptional<z.ZodObject<{
76
+ type: z.ZodLiteral<"ephemeral">;
77
+ ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
78
+ }, "strip", z.ZodTypeAny, {
79
+ type: "ephemeral";
80
+ ttl?: "5m" | "1h" | undefined;
81
+ }, {
82
+ type: "ephemeral";
83
+ ttl?: "5m" | "1h" | undefined;
84
+ }>>;
72
85
  }, "strip", z.ZodTypeAny, {
73
86
  role: "system";
74
87
  content: string;
75
88
  name?: string | undefined;
89
+ cacheControl?: {
90
+ type: "ephemeral";
91
+ ttl?: "5m" | "1h" | undefined;
92
+ } | undefined;
76
93
  }, {
77
94
  role: "system";
78
95
  content: string;
79
96
  name?: string | undefined;
97
+ cacheControl?: {
98
+ type: "ephemeral";
99
+ ttl?: "5m" | "1h" | undefined;
100
+ } | undefined;
80
101
  }>, z.ZodObject<{
81
102
  role: z.ZodLiteral<"user">;
82
103
  content: z.ZodString;
83
104
  name: z.ZodOptional<z.ZodString>;
105
+ cacheControl: z.ZodOptional<z.ZodObject<{
106
+ type: z.ZodLiteral<"ephemeral">;
107
+ ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
108
+ }, "strip", z.ZodTypeAny, {
109
+ type: "ephemeral";
110
+ ttl?: "5m" | "1h" | undefined;
111
+ }, {
112
+ type: "ephemeral";
113
+ ttl?: "5m" | "1h" | undefined;
114
+ }>>;
84
115
  }, "strip", z.ZodTypeAny, {
85
116
  role: "user";
86
117
  content: string;
87
118
  name?: string | undefined;
119
+ cacheControl?: {
120
+ type: "ephemeral";
121
+ ttl?: "5m" | "1h" | undefined;
122
+ } | undefined;
88
123
  }, {
89
124
  role: "user";
90
125
  content: string;
91
126
  name?: string | undefined;
127
+ cacheControl?: {
128
+ type: "ephemeral";
129
+ ttl?: "5m" | "1h" | undefined;
130
+ } | undefined;
92
131
  }>, z.ZodObject<{
93
132
  role: z.ZodLiteral<"agent">;
94
133
  content: z.ZodOptional<z.ZodString>;
@@ -121,6 +160,16 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
121
160
  id: string;
122
161
  }>, "many">>;
123
162
  name: z.ZodOptional<z.ZodString>;
163
+ cacheControl: z.ZodOptional<z.ZodObject<{
164
+ type: z.ZodLiteral<"ephemeral">;
165
+ ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
166
+ }, "strip", z.ZodTypeAny, {
167
+ type: "ephemeral";
168
+ ttl?: "5m" | "1h" | undefined;
169
+ }, {
170
+ type: "ephemeral";
171
+ ttl?: "5m" | "1h" | undefined;
172
+ }>>;
124
173
  }, "strip", z.ZodTypeAny, {
125
174
  role: "agent";
126
175
  name?: string | undefined;
@@ -133,6 +182,10 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
133
182
  type: "function";
134
183
  id: string;
135
184
  }[] | undefined;
185
+ cacheControl?: {
186
+ type: "ephemeral";
187
+ ttl?: "5m" | "1h" | undefined;
188
+ } | undefined;
136
189
  }, {
137
190
  role: "agent";
138
191
  name?: string | undefined;
@@ -145,21 +198,43 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
145
198
  type: "function";
146
199
  id: string;
147
200
  }[] | undefined;
201
+ cacheControl?: {
202
+ type: "ephemeral";
203
+ ttl?: "5m" | "1h" | undefined;
204
+ } | undefined;
148
205
  }>, z.ZodObject<{
149
206
  role: z.ZodLiteral<"tool">;
150
207
  content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
151
208
  toolCallId: z.ZodString;
152
209
  name: z.ZodOptional<z.ZodString>;
210
+ cacheControl: z.ZodOptional<z.ZodObject<{
211
+ type: z.ZodLiteral<"ephemeral">;
212
+ ttl: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
213
+ }, "strip", z.ZodTypeAny, {
214
+ type: "ephemeral";
215
+ ttl?: "5m" | "1h" | undefined;
216
+ }, {
217
+ type: "ephemeral";
218
+ ttl?: "5m" | "1h" | undefined;
219
+ }>>;
153
220
  }, "strip", z.ZodTypeAny, {
154
221
  role: "tool";
155
222
  content: string;
156
223
  toolCallId: string;
157
224
  name?: string | undefined;
225
+ cacheControl?: {
226
+ type: "ephemeral";
227
+ ttl?: "5m" | "1h" | undefined;
228
+ } | undefined;
158
229
  }, {
159
230
  role: "tool";
160
231
  content: string | Record<string, unknown>;
161
232
  toolCallId: string;
162
233
  name?: string | undefined;
234
+ cacheControl?: {
235
+ type: "ephemeral";
236
+ ttl?: "5m" | "1h" | undefined;
237
+ } | undefined;
163
238
  }>]>;
164
239
  export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
165
240
  export declare function parseChatMessages(messages: (z.infer<typeof chatMessageSchema> & {
@@ -203,6 +203,13 @@ export interface ChatModelInputMessage {
203
203
  * Name of the message sender (for multi-agent scenarios)
204
204
  */
205
205
  name?: string;
206
+ /**
207
+ * Cache control marker for the entire message (only supported by Claude)
208
+ *
209
+ * This is syntactic sugar that applies cacheControl to the last content block
210
+ * of the message. See {@link CacheControl} for details.
211
+ */
212
+ cacheControl?: CacheControl;
206
213
  }
207
214
  /**
208
215
  * Type of input message content
@@ -218,27 +225,64 @@ export type ChatModelInputMessageContent = string | UnionContent[];
218
225
  export type TextContent = {
219
226
  type: "text";
220
227
  text: string;
228
+ /**
229
+ * Cache control marker (only supported by Claude)
230
+ *
231
+ * When set, this content block will be marked as a cache breakpoint.
232
+ * See {@link CacheControl} for details.
233
+ */
234
+ cacheControl?: CacheControl;
221
235
  };
222
236
  export declare const textContentSchema: z.ZodObject<{
223
237
  type: z.ZodLiteral<"text">;
224
238
  text: z.ZodString;
239
+ cacheControl: ZodType<{
240
+ type: "ephemeral";
241
+ ttl?: "5m" | "1h" | undefined;
242
+ } | undefined, z.ZodTypeDef, {
243
+ type: "ephemeral";
244
+ ttl?: "5m" | "1h" | undefined;
245
+ } | undefined>;
225
246
  }, "strip", z.ZodTypeAny, {
226
247
  type: "text";
227
248
  text: string;
249
+ cacheControl?: {
250
+ type: "ephemeral";
251
+ ttl?: "5m" | "1h" | undefined;
252
+ } | undefined;
228
253
  }, {
229
254
  type: "text";
230
255
  text: string;
256
+ cacheControl?: {
257
+ type: "ephemeral";
258
+ ttl?: "5m" | "1h" | undefined;
259
+ } | undefined;
231
260
  }>;
232
261
  export type UnionContent = TextContent | FileUnionContent;
233
262
  export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
234
263
  type: z.ZodLiteral<"text">;
235
264
  text: z.ZodString;
265
+ cacheControl: ZodType<{
266
+ type: "ephemeral";
267
+ ttl?: "5m" | "1h" | undefined;
268
+ } | undefined, z.ZodTypeDef, {
269
+ type: "ephemeral";
270
+ ttl?: "5m" | "1h" | undefined;
271
+ } | undefined>;
236
272
  }, "strip", z.ZodTypeAny, {
237
273
  type: "text";
238
274
  text: string;
275
+ cacheControl?: {
276
+ type: "ephemeral";
277
+ ttl?: "5m" | "1h" | undefined;
278
+ } | undefined;
239
279
  }, {
240
280
  type: "text";
241
281
  text: string;
282
+ cacheControl?: {
283
+ type: "ephemeral";
284
+ ttl?: "5m" | "1h" | undefined;
285
+ } | undefined;
242
286
  }>, z.ZodObject<{
243
287
  filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
244
288
  mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -340,6 +384,14 @@ export interface ChatModelInputTool {
340
384
  * For example, Gemini's thought_signature
341
385
  */
342
386
  metadata?: Record<string, any>;
387
+ /**
388
+ * Cache control marker (only supported by Claude)
389
+ *
390
+ * When set, this tool definition will be marked as a cache breakpoint.
391
+ * Typically applied to the last tool in the tools array.
392
+ * See {@link CacheControl} for details.
393
+ */
394
+ cacheControl?: CacheControl;
343
395
  }
344
396
  /**
345
397
  * Tool selection strategy
@@ -362,6 +414,82 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
362
414
  };
363
415
  };
364
416
  export type Modality = "text" | "image" | "audio";
417
+ /**
418
+ * Cache control marker for prompt caching
419
+ *
420
+ * Used to mark content blocks, messages, or tools for caching.
421
+ * Currently only supported by Anthropic (Claude) models.
422
+ */
423
+ export interface CacheControl {
424
+ /**
425
+ * Cache type (currently only "ephemeral" is supported)
426
+ */
427
+ type: "ephemeral";
428
+ /**
429
+ * Cache TTL (Time To Live)
430
+ * - "5m": 5 minutes (default)
431
+ * - "1h": 1 hour
432
+ */
433
+ ttl?: "5m" | "1h";
434
+ }
435
+ /**
436
+ * Cache configuration options
437
+ *
438
+ * Controls how prompt caching is used for supported providers.
439
+ * Prompt caching can significantly reduce costs and latency by reusing
440
+ * previously processed prompts (system messages, tool definitions, etc.).
441
+ */
442
+ export interface CacheConfig {
443
+ /**
444
+ * Whether to enable prompt caching
445
+ *
446
+ * - OpenAI: Ignored (always enabled automatically)
447
+ * - Gemini: Controls explicit caching
448
+ * - Claude: Controls whether to add cache_control markers
449
+ *
450
+ * @default true
451
+ */
452
+ enabled?: boolean;
453
+ /**
454
+ * Cache TTL (Time To Live)
455
+ *
456
+ * - OpenAI: Ignored (automatic)
457
+ * - Gemini: Supports custom seconds
458
+ * - Claude: Only supports "5m" or "1h"
459
+ *
460
+ * @default "5m"
461
+ */
462
+ ttl?: "5m" | "1h" | number;
463
+ /**
464
+ * Caching strategy
465
+ *
466
+ * - "auto": Automatically add cache breakpoints at optimal locations
467
+ * - "manual": Require explicit cacheControl markers on messages/tools
468
+ *
469
+ * @default "auto"
470
+ */
471
+ strategy?: "auto" | "manual";
472
+ /**
473
+ * Auto cache breakpoint locations (only effective when strategy="auto")
474
+ *
475
+ * @default { tools: true, system: true, lastMessage: false }
476
+ */
477
+ autoBreakpoints?: {
478
+ /** Cache tool definitions */
479
+ tools?: boolean;
480
+ /** Cache system messages */
481
+ system?: boolean;
482
+ /** Cache last message in conversation history */
483
+ lastMessage?: boolean;
484
+ };
485
+ }
486
+ /**
487
+ * Default cache configuration
488
+ *
489
+ * Enables automatic caching for system messages and tool definitions,
490
+ * which typically provides the best cost/performance tradeoff.
491
+ */
492
+ export declare const DEFAULT_CACHE_CONFIG: CacheConfig;
365
493
  /**
366
494
  * Model-specific configuration options
367
495
  *
@@ -395,6 +523,15 @@ export interface ChatModelInputOptions extends Record<string, unknown> {
395
523
  modalities?: Modality[];
396
524
  preferInputFileType?: "file" | "url";
397
525
  reasoningEffort?: number | "minimal" | "low" | "medium" | "high";
526
+ /**
527
+ * Cache configuration for prompt caching
528
+ *
529
+ * Enables caching of system messages, tool definitions, and conversation history
530
+ * to reduce costs and latency. See {@link CacheConfig} for details.
531
+ *
532
+ * @default DEFAULT_CACHE_CONFIG (enabled with auto strategy)
533
+ */
534
+ cacheConfig?: CacheConfig;
398
535
  }
399
536
  export type ChatModelInputOptionsWithGetter = GetterSchema<ChatModelInputOptions>;
400
537
  /**
@@ -492,6 +629,16 @@ export interface ChatModelOutputUsage {
492
629
  * AIGNE Hub credit usage
493
630
  */
494
631
  aigneHubCredits?: number;
632
+ /**
633
+ * Number of tokens written to cache (first time caching)
634
+ * Only applicable for providers that support explicit cache creation (e.g., Anthropic)
635
+ */
636
+ cacheCreationInputTokens?: number;
637
+ /**
638
+ * Number of tokens read from cache (cache hit)
639
+ * Supported by OpenAI, Anthropic, and Gemini
640
+ */
641
+ cacheReadInputTokens?: number;
495
642
  /**
496
643
  * Credit prefix
497
644
  */
@@ -501,15 +648,21 @@ export declare const chatModelOutputUsageSchema: z.ZodObject<{
501
648
  inputTokens: z.ZodNumber;
502
649
  outputTokens: z.ZodNumber;
503
650
  aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
651
+ cacheCreationInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
652
+ cacheReadInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
504
653
  creditPrefix: ZodType<"$" | "€" | "¥" | undefined, z.ZodTypeDef, "$" | "€" | "¥" | undefined>;
505
654
  }, "strip", z.ZodTypeAny, {
506
655
  inputTokens: number;
507
656
  outputTokens: number;
508
657
  aigneHubCredits?: number | undefined;
658
+ cacheCreationInputTokens?: number | undefined;
659
+ cacheReadInputTokens?: number | undefined;
509
660
  creditPrefix?: "$" | "€" | "¥" | undefined;
510
661
  }, {
511
662
  inputTokens: number;
512
663
  outputTokens: number;
513
664
  aigneHubCredits?: number | undefined;
665
+ cacheCreationInputTokens?: number | undefined;
666
+ cacheReadInputTokens?: number | undefined;
514
667
  creditPrefix?: "$" | "€" | "¥" | undefined;
515
668
  }>;
@@ -188,6 +188,10 @@ export class ChatModel extends Model {
188
188
  options.context.usage.inputTokens += usage.inputTokens;
189
189
  if (usage.aigneHubCredits)
190
190
  options.context.usage.aigneHubCredits += usage.aigneHubCredits;
191
+ if (usage.cacheCreationInputTokens)
192
+ options.context.usage.cacheCreationInputTokens += usage.cacheCreationInputTokens;
193
+ if (usage.cacheReadInputTokens)
194
+ options.context.usage.cacheReadInputTokens += usage.cacheReadInputTokens;
191
195
  if (usage.creditPrefix)
192
196
  options.context.usage.creditPrefix = usage.creditPrefix;
193
197
  }
@@ -244,6 +248,10 @@ export const roleSchema = z.union([
244
248
  export const textContentSchema = z.object({
245
249
  type: z.literal("text"),
246
250
  text: z.string(),
251
+ cacheControl: optionalize(z.object({
252
+ type: z.literal("ephemeral"),
253
+ ttl: optionalize(z.union([z.literal("5m"), z.literal("1h")])),
254
+ })),
247
255
  });
248
256
  export const unionContentSchema = z.discriminatedUnion("type", [
249
257
  textContentSchema,
@@ -265,6 +273,10 @@ const chatModelInputMessageSchema = z.object({
265
273
  }))),
266
274
  toolCallId: optionalize(z.string()),
267
275
  name: optionalize(z.string()),
276
+ cacheControl: optionalize(z.object({
277
+ type: z.literal("ephemeral"),
278
+ ttl: optionalize(z.union([z.literal("5m"), z.literal("1h")])),
279
+ })),
268
280
  });
269
281
  const chatModelInputResponseFormatSchema = z.discriminatedUnion("type", [
270
282
  z.object({ type: z.literal("text") }),
@@ -286,6 +298,10 @@ const chatModelInputToolSchema = z.object({
286
298
  parameters: z.record(z.string(), z.unknown()),
287
299
  }),
288
300
  metadata: optionalize(z.record(z.string(), z.unknown())),
301
+ cacheControl: optionalize(z.object({
302
+ type: z.literal("ephemeral"),
303
+ ttl: optionalize(z.union([z.literal("5m"), z.literal("1h")])),
304
+ })),
289
305
  });
290
306
  const chatModelInputToolChoiceSchema = z.union([
291
307
  z.literal("auto"),
@@ -293,6 +309,22 @@ const chatModelInputToolChoiceSchema = z.union([
293
309
  z.literal("required"),
294
310
  chatModelInputToolSchema,
295
311
  ]);
312
+ /**
313
+ * Default cache configuration
314
+ *
315
+ * Enables automatic caching for system messages and tool definitions,
316
+ * which typically provides the best cost/performance tradeoff.
317
+ */
318
+ export const DEFAULT_CACHE_CONFIG = {
319
+ enabled: true,
320
+ ttl: "5m",
321
+ strategy: "auto",
322
+ autoBreakpoints: {
323
+ tools: true,
324
+ system: true,
325
+ lastMessage: false,
326
+ },
327
+ };
296
328
  const modelOptionsSchemaProperties = {
297
329
  model: z.string(),
298
330
  temperature: z.number(),
@@ -341,6 +373,8 @@ export const chatModelOutputUsageSchema = z.object({
341
373
  inputTokens: z.number(),
342
374
  outputTokens: z.number(),
343
375
  aigneHubCredits: optionalize(z.number()),
376
+ cacheCreationInputTokens: optionalize(z.number()),
377
+ cacheReadInputTokens: optionalize(z.number()),
344
378
  creditPrefix: optionalize(z.union([z.literal("$"), z.literal("€"), z.literal("¥")])),
345
379
  });
346
380
  const chatModelOutputSchema = z.object({
@@ -196,16 +196,22 @@ export declare const imageModelOutputSchema: z.ZodObject<{
196
196
  inputTokens: z.ZodNumber;
197
197
  outputTokens: z.ZodNumber;
198
198
  aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
199
+ cacheCreationInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
200
+ cacheReadInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
199
201
  creditPrefix: ZodType<"$" | "€" | "¥" | undefined, z.ZodTypeDef, "$" | "€" | "¥" | undefined>;
200
202
  }, "strip", z.ZodTypeAny, {
201
203
  inputTokens: number;
202
204
  outputTokens: number;
203
205
  aigneHubCredits?: number | undefined;
206
+ cacheCreationInputTokens?: number | undefined;
207
+ cacheReadInputTokens?: number | undefined;
204
208
  creditPrefix?: "$" | "€" | "¥" | undefined;
205
209
  }, {
206
210
  inputTokens: number;
207
211
  outputTokens: number;
208
212
  aigneHubCredits?: number | undefined;
213
+ cacheCreationInputTokens?: number | undefined;
214
+ cacheReadInputTokens?: number | undefined;
209
215
  creditPrefix?: "$" | "€" | "¥" | undefined;
210
216
  }>>;
211
217
  model: z.ZodOptional<z.ZodString>;
@@ -231,6 +237,8 @@ export declare const imageModelOutputSchema: z.ZodObject<{
231
237
  inputTokens: number;
232
238
  outputTokens: number;
233
239
  aigneHubCredits?: number | undefined;
240
+ cacheCreationInputTokens?: number | undefined;
241
+ cacheReadInputTokens?: number | undefined;
234
242
  creditPrefix?: "$" | "€" | "¥" | undefined;
235
243
  } | undefined;
236
244
  }, {
@@ -255,6 +263,8 @@ export declare const imageModelOutputSchema: z.ZodObject<{
255
263
  inputTokens: number;
256
264
  outputTokens: number;
257
265
  aigneHubCredits?: number | undefined;
266
+ cacheCreationInputTokens?: number | undefined;
267
+ cacheReadInputTokens?: number | undefined;
258
268
  creditPrefix?: "$" | "€" | "¥" | undefined;
259
269
  } | undefined;
260
270
  }>;
@@ -203,16 +203,22 @@ export declare const videoModelOutputSchema: z.ZodObject<{
203
203
  inputTokens: z.ZodNumber;
204
204
  outputTokens: z.ZodNumber;
205
205
  aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
+ cacheCreationInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
207
+ cacheReadInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
208
  creditPrefix: ZodType<"$" | "€" | "¥" | undefined, z.ZodTypeDef, "$" | "€" | "¥" | undefined>;
207
209
  }, "strip", z.ZodTypeAny, {
208
210
  inputTokens: number;
209
211
  outputTokens: number;
210
212
  aigneHubCredits?: number | undefined;
213
+ cacheCreationInputTokens?: number | undefined;
214
+ cacheReadInputTokens?: number | undefined;
211
215
  creditPrefix?: "$" | "€" | "¥" | undefined;
212
216
  }, {
213
217
  inputTokens: number;
214
218
  outputTokens: number;
215
219
  aigneHubCredits?: number | undefined;
220
+ cacheCreationInputTokens?: number | undefined;
221
+ cacheReadInputTokens?: number | undefined;
216
222
  creditPrefix?: "$" | "€" | "¥" | undefined;
217
223
  }>>;
218
224
  model: z.ZodOptional<z.ZodString>;
@@ -239,6 +245,8 @@ export declare const videoModelOutputSchema: z.ZodObject<{
239
245
  inputTokens: number;
240
246
  outputTokens: number;
241
247
  aigneHubCredits?: number | undefined;
248
+ cacheCreationInputTokens?: number | undefined;
249
+ cacheReadInputTokens?: number | undefined;
242
250
  creditPrefix?: "$" | "€" | "¥" | undefined;
243
251
  } | undefined;
244
252
  seconds?: number | undefined;
@@ -264,6 +272,8 @@ export declare const videoModelOutputSchema: z.ZodObject<{
264
272
  inputTokens: number;
265
273
  outputTokens: number;
266
274
  aigneHubCredits?: number | undefined;
275
+ cacheCreationInputTokens?: number | undefined;
276
+ cacheReadInputTokens?: number | undefined;
267
277
  creditPrefix?: "$" | "€" | "¥" | undefined;
268
278
  } | undefined;
269
279
  seconds?: number | undefined;
@@ -8,6 +8,10 @@ export interface ContextUsage {
8
8
  creditPrefix?: "$" | "€" | "¥";
9
9
  agentCalls: number;
10
10
  duration: number;
11
+ /** Number of tokens written to cache (first time caching) */
12
+ cacheCreationInputTokens: number;
13
+ /** Number of tokens read from cache (cache hit) */
14
+ cacheReadInputTokens: number;
11
15
  }
12
16
  /**
13
17
  * @hidden
@@ -8,6 +8,8 @@ export function newEmptyContextUsage() {
8
8
  aigneHubCredits: 0,
9
9
  agentCalls: 0,
10
10
  duration: 0,
11
+ cacheCreationInputTokens: 0,
12
+ cacheReadInputTokens: 0,
11
13
  };
12
14
  }
13
15
  export function mergeContextUsage(usage, additional) {
@@ -21,4 +23,8 @@ export function mergeContextUsage(usage, additional) {
21
23
  usage.agentCalls += additional.agentCalls;
22
24
  if (additional.duration)
23
25
  usage.duration += additional.duration;
26
+ if (additional.cacheCreationInputTokens)
27
+ usage.cacheCreationInputTokens += additional.cacheCreationInputTokens;
28
+ if (additional.cacheReadInputTokens)
29
+ usage.cacheReadInputTokens += additional.cacheReadInputTokens;
24
30
  }
@@ -72,6 +72,10 @@ export type Instructions = {
72
72
  role: Exclude<Role, "tool">;
73
73
  content: string;
74
74
  path: string;
75
+ cacheControl?: {
76
+ type: "ephemeral";
77
+ ttl?: "5m" | "1h";
78
+ };
75
79
  }[];
76
80
  export interface AIAgentSchema extends BaseAgentSchema {
77
81
  type: "ai";
@@ -25,27 +25,37 @@ export async function loadAgentFromYamlFile(path, options) {
25
25
  }, options), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
26
26
  return agent;
27
27
  }
28
- const instructionItemSchema = z.union([
28
+ const instructionItemSchema = camelizeSchema(z.union([
29
29
  z.object({
30
30
  role: roleSchema.default("system"),
31
31
  url: z.string(),
32
+ cacheControl: optionalize(z.object({
33
+ type: z.literal("ephemeral"),
34
+ ttl: optionalize(z.union([z.literal("5m"), z.literal("1h")])),
35
+ })),
32
36
  }),
33
37
  z.object({
34
38
  role: roleSchema.default("system"),
35
39
  content: z.string(),
40
+ cacheControl: optionalize(z.object({
41
+ type: z.literal("ephemeral"),
42
+ ttl: optionalize(z.union([z.literal("5m"), z.literal("1h")])),
43
+ })),
36
44
  }),
37
- ]);
38
- const parseInstructionItem = ({ filepath }) => async ({ role, ...v }) => {
45
+ ]));
46
+ const parseInstructionItem = ({ filepath }) => async ({ role, cacheControl, ...v }) => {
39
47
  if (role === "tool")
40
48
  throw new Error(`'tool' role is not allowed in instruction item in agent file ${filepath}`);
41
49
  if ("content" in v && typeof v.content === "string") {
42
- return { role, content: v.content, path: filepath };
50
+ return { role, content: v.content, path: filepath, cacheControl };
43
51
  }
44
52
  if ("url" in v && typeof v.url === "string") {
45
53
  const url = nodejs.path.isAbsolute(v.url)
46
54
  ? v.url
47
55
  : nodejs.path.join(nodejs.path.dirname(filepath), v.url);
48
- return nodejs.fs.readFile(url, "utf8").then((content) => ({ role, content, path: url }));
56
+ return nodejs.fs
57
+ .readFile(url, "utf8")
58
+ .then((content) => ({ role, content, path: url, cacheControl }));
49
59
  }
50
60
  throw new Error(`Invalid instruction item in agent file ${filepath}. Expected 'content' or 'url' property`);
51
61
  };