@jaypie/llm 1.3.14 → 1.3.15

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.
@@ -530,6 +530,7 @@ type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise<unknow
530
530
  * `input` is the raw, pre-interpolation input with multimodal parts preserved.
531
531
  */
532
532
  interface LlmExchangeRequest {
533
+ cache?: LlmCache;
533
534
  data?: NaturalMap;
534
535
  effort?: LlmEffort;
535
536
  explain?: boolean;
@@ -600,7 +601,21 @@ interface LlmExchangeEnvelope {
600
601
  timing: LlmExchangeTiming;
601
602
  }
602
603
  type LlmExchangeCallback = (envelope: LlmExchangeEnvelope) => unknown | Promise<unknown>;
604
+ /**
605
+ * Prompt-caching control for operate()/stream().
606
+ * - `true` / omitted → caching enabled at the default `"5m"` TTL
607
+ * - `false` / `0` → caching disabled
608
+ * - `"5m"` / `"1h"` → enabled at that TTL (TTL honored by Anthropic/OpenRouter;
609
+ * other providers ignore it and cache with their own defaults)
610
+ */
611
+ type LlmCache = boolean | 0 | "5m" | "1h";
603
612
  interface LlmOperateOptions {
613
+ /**
614
+ * Prompt caching for the stable request prefix (system prompt + tools).
615
+ * Enabled by default; pass `false`/`0` to opt out, or `"5m"`/`"1h"` to set
616
+ * the TTL. See {@link LlmCache}.
617
+ */
618
+ cache?: LlmCache;
604
619
  data?: NaturalMap;
605
620
  /**
606
621
  * Provider-neutral reasoning effort (lowest | low | medium | high | highest).
@@ -709,6 +724,10 @@ interface LlmUsageItem {
709
724
  output: number;
710
725
  reasoning: number;
711
726
  total: number;
727
+ /** Prompt-cache tokens served from cache this call (billed at ~0.1x input) */
728
+ cacheRead?: number;
729
+ /** Prompt-cache tokens written to cache this call (billed at ~1.25x input) */
730
+ cacheWrite?: number;
712
731
  provider?: string;
713
732
  model?: string;
714
733
  }
@@ -1023,4 +1042,4 @@ declare class XaiProvider implements LlmProvider {
1023
1042
  }
1024
1043
 
1025
1044
  export { BedrockProvider, ErrorCategory, FireworksProvider, GoogleProvider as GeminiProvider, GoogleProvider, JaypieToolkit, constants as LLM, Llm, LlmError, LlmMessageRole, LlmMessageType, LlmProgressEventType, LlmQuotaError, LlmRateLimitError, LlmStreamChunkType, LlmTransientError, LlmUnrecoverableError, OpenRouterProvider, Toolkit, XaiProvider, extractReasoning, isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, jsonSchemaToNaturalSchema, naturalSchemaToJsonSchema, toolkit, tools };
1026
- export type { LlmEffort, LlmErrorOptions, LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, LlmStreamChunk, LlmStreamChunkDone, LlmStreamChunkError, LlmStreamChunkText, LlmStreamChunkToolCall, LlmStreamChunkToolResult, LlmTool };
1045
+ export type { LlmCache, LlmEffort, LlmErrorOptions, LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, LlmStreamChunk, LlmStreamChunkDone, LlmStreamChunkError, LlmStreamChunkText, LlmStreamChunkToolCall, LlmStreamChunkToolResult, LlmTool };
@@ -1,7 +1,7 @@
1
1
  export { default as Llm } from "./Llm.js";
2
2
  export * as LLM from "./constants.js";
3
3
  export type { LlmEffort } from "./constants.js";
4
- export type { LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, } from "./types/LlmProvider.interface.js";
4
+ export type { LlmCache, LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, } from "./types/LlmProvider.interface.js";
5
5
  export { LlmMessageRole, LlmMessageType, LlmProgressEventType, } from "./types/LlmProvider.interface.js";
6
6
  export { isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, } from "./types/LlmOperateInput.guards.js";
7
7
  export type { LlmTool } from "./types/LlmTool.interface.js";
@@ -52,11 +52,17 @@ export declare class BedrockAdapter extends BaseProviderAdapter {
52
52
  readonly defaultModel: "amazon.nova-pro-v1:0";
53
53
  private _modelsFallbackToStructuredOutputTool;
54
54
  private _modelsWithoutTemperature;
55
+ private _modelsWithoutCachePoint;
56
+ private rememberModelRejectsCachePoint;
57
+ private supportsCachePoint;
55
58
  private rememberModelRejectsOutputConfig;
56
59
  private useFakeToolForStructuredOutput;
57
60
  private rememberModelRejectsTemperature;
58
61
  private supportsTemperature;
59
62
  buildRequest(request: OperateRequest): BedrockRequest;
63
+ /** Remove any cachePoint blocks so the request can be retried unsupported. */
64
+ private stripCachePoints;
65
+ private requestHasCachePoints;
60
66
  formatTools(toolkit: Toolkit): ProviderToolDefinition[];
61
67
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
62
68
  executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<AnnotatedBedrockResponse>;
@@ -21,6 +21,11 @@ export declare class OpenAiAdapter extends BaseProviderAdapter {
21
21
  private supportsTemperature;
22
22
  /** Whether `reasoning.effort` may be sent for this model. Overridden by xAI. */
23
23
  protected supportsReasoningEffort(model: string): boolean;
24
+ /**
25
+ * Whether to emit `prompt_cache_key` (OpenAI Responses API). Overridable by
26
+ * OpenAI-compatible subclasses whose backend rejects the field.
27
+ */
28
+ protected supportsPromptCacheKey(): boolean;
24
29
  /** Translate a normalized effort to this provider's `reasoning.effort` value. */
25
30
  protected mapReasoningEffort(effort: LlmEffort, model: string): LlmEffortMapping;
26
31
  buildRequest(request: OperateRequest): unknown;
@@ -42,6 +42,13 @@ interface OpenRouterUsage {
42
42
  completionTokensDetails?: {
43
43
  reasoningTokens?: number;
44
44
  };
45
+ promptTokensDetails?: {
46
+ cachedTokens?: number;
47
+ cached_tokens?: number;
48
+ };
49
+ prompt_tokens_details?: {
50
+ cached_tokens?: number;
51
+ };
45
52
  }
46
53
  interface OpenRouterResponse {
47
54
  id: string;
@@ -95,6 +102,10 @@ interface OpenRouterRequest {
95
102
  type OpenRouterContentPart = {
96
103
  type: "text";
97
104
  text: string;
105
+ cache_control?: {
106
+ type: "ephemeral";
107
+ ttl?: "5m" | "1h";
108
+ };
98
109
  } | {
99
110
  type: "image_url";
100
111
  imageUrl: {
@@ -1,6 +1,6 @@
1
1
  import { JsonObject } from "@jaypie/types";
2
2
  import { type LlmEffort } from "../constants.js";
3
- import { LlmHistory, LlmMessageType, LlmOperateOptions, LlmUsageItem } from "../types/LlmProvider.interface.js";
3
+ import { LlmCache, LlmHistory, LlmMessageType, LlmOperateOptions, LlmUsageItem } from "../types/LlmProvider.interface.js";
4
4
  import { Toolkit } from "../tools/Toolkit.class.js";
5
5
  /**
6
6
  * Standardized tool call representation across providers
@@ -73,6 +73,11 @@ export interface OperateRequest {
73
73
  effort?: LlmEffort;
74
74
  /** Provider-specific options */
75
75
  providerOptions?: JsonObject;
76
+ /**
77
+ * Prompt-caching control for the stable prefix (system + tools). `true`/
78
+ * omitted = enabled@5m, `false`/`0` = disabled, `"5m"`/`"1h"` = enabled@ttl.
79
+ */
80
+ cache?: LlmCache;
76
81
  /** Whether the request will execute over a streaming transport */
77
82
  stream?: boolean;
78
83
  /** Sampling temperature (0-2 for most providers) */
@@ -2,10 +2,20 @@ import { JsonObject } from "@jaypie/types";
2
2
  import { LlmMessageRole } from "../../types/LlmProvider.interface.js";
3
3
  export declare const ROLE_MAP: Record<LlmMessageRole, string>;
4
4
  export declare namespace Anthropic {
5
+ interface CacheControlEphemeral {
6
+ type: "ephemeral";
7
+ ttl?: "5m" | "1h";
8
+ }
5
9
  interface TextBlock {
6
10
  type: "text";
7
11
  text: string;
8
12
  }
13
+ /** Top-level `system` block form that can carry a cache breakpoint */
14
+ interface SystemTextBlockParam {
15
+ type: "text";
16
+ text: string;
17
+ cache_control?: CacheControlEphemeral;
18
+ }
9
19
  interface ToolUseBlock {
10
20
  type: "tool_use";
11
21
  id: string;
@@ -63,17 +73,20 @@ export declare namespace Anthropic {
63
73
  description?: string;
64
74
  input_schema: Messages.Tool.InputSchema;
65
75
  type?: "custom";
76
+ cache_control?: CacheControlEphemeral;
66
77
  }
67
78
  interface Usage {
68
79
  input_tokens: number;
69
80
  output_tokens: number;
70
81
  thinking_tokens?: number;
82
+ cache_read_input_tokens?: number;
83
+ cache_creation_input_tokens?: number;
71
84
  }
72
85
  interface MessageCreateParams {
73
86
  model: string;
74
87
  messages: MessageParam[];
75
88
  max_tokens: number;
76
- system?: string;
89
+ system?: string | SystemTextBlockParam[];
77
90
  tools?: Tool[];
78
91
  tool_choice?: {
79
92
  type: "auto" | "any" | "tool";
@@ -217,6 +217,7 @@ export type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise
217
217
  * `input` is the raw, pre-interpolation input with multimodal parts preserved.
218
218
  */
219
219
  export interface LlmExchangeRequest {
220
+ cache?: LlmCache;
220
221
  data?: NaturalMap;
221
222
  effort?: LlmEffort;
222
223
  explain?: boolean;
@@ -287,7 +288,21 @@ export interface LlmExchangeEnvelope {
287
288
  timing: LlmExchangeTiming;
288
289
  }
289
290
  export type LlmExchangeCallback = (envelope: LlmExchangeEnvelope) => unknown | Promise<unknown>;
291
+ /**
292
+ * Prompt-caching control for operate()/stream().
293
+ * - `true` / omitted → caching enabled at the default `"5m"` TTL
294
+ * - `false` / `0` → caching disabled
295
+ * - `"5m"` / `"1h"` → enabled at that TTL (TTL honored by Anthropic/OpenRouter;
296
+ * other providers ignore it and cache with their own defaults)
297
+ */
298
+ export type LlmCache = boolean | 0 | "5m" | "1h";
290
299
  export interface LlmOperateOptions {
300
+ /**
301
+ * Prompt caching for the stable request prefix (system prompt + tools).
302
+ * Enabled by default; pass `false`/`0` to opt out, or `"5m"`/`"1h"` to set
303
+ * the TTL. See {@link LlmCache}.
304
+ */
305
+ cache?: LlmCache;
291
306
  data?: NaturalMap;
292
307
  /**
293
308
  * Provider-neutral reasoning effort (lowest | low | medium | high | highest).
@@ -396,6 +411,10 @@ export interface LlmUsageItem {
396
411
  output: number;
397
412
  reasoning: number;
398
413
  total: number;
414
+ /** Prompt-cache tokens served from cache this call (billed at ~0.1x input) */
415
+ cacheRead?: number;
416
+ /** Prompt-cache tokens written to cache this call (billed at ~1.25x input) */
417
+ cacheWrite?: number;
399
418
  provider?: string;
400
419
  model?: string;
401
420
  }
@@ -0,0 +1,21 @@
1
+ import { type LlmCache } from "../types/LlmProvider.interface.js";
2
+ export declare const CACHE_TTL_DEFAULT: "5m";
3
+ export type CacheTtl = "5m" | "1h";
4
+ export interface ResolvedCache {
5
+ enabled: boolean;
6
+ ttl: CacheTtl;
7
+ }
8
+ /**
9
+ * Normalize the caller-facing `cache` option into a concrete decision.
10
+ * - `undefined` / `true` → enabled at the default TTL
11
+ * - `false` / `0` → disabled
12
+ * - `"5m"` / `"1h"` → enabled at that TTL
13
+ */
14
+ export declare function resolveCache(cache: LlmCache | undefined): ResolvedCache;
15
+ /**
16
+ * Deterministic short key for providers with automatic, prefix-based caching
17
+ * (e.g. OpenAI `prompt_cache_key`). Derived from the stable prefix so the same
18
+ * system prompt + tools + model always routes to the same cache. Dependency-
19
+ * free FNV-1a; not cryptographic.
20
+ */
21
+ export declare function promptCacheKey(seed: string): string;
@@ -1,3 +1,4 @@
1
+ export * from "./cacheControl.js";
1
2
  export * from "./determineModelProvider.js";
2
3
  export * from "./extractReasoning.js";
3
4
  export * from "./fillFormatArrays.js";
@@ -530,6 +530,7 @@ type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise<unknow
530
530
  * `input` is the raw, pre-interpolation input with multimodal parts preserved.
531
531
  */
532
532
  interface LlmExchangeRequest {
533
+ cache?: LlmCache;
533
534
  data?: NaturalMap;
534
535
  effort?: LlmEffort;
535
536
  explain?: boolean;
@@ -600,7 +601,21 @@ interface LlmExchangeEnvelope {
600
601
  timing: LlmExchangeTiming;
601
602
  }
602
603
  type LlmExchangeCallback = (envelope: LlmExchangeEnvelope) => unknown | Promise<unknown>;
604
+ /**
605
+ * Prompt-caching control for operate()/stream().
606
+ * - `true` / omitted → caching enabled at the default `"5m"` TTL
607
+ * - `false` / `0` → caching disabled
608
+ * - `"5m"` / `"1h"` → enabled at that TTL (TTL honored by Anthropic/OpenRouter;
609
+ * other providers ignore it and cache with their own defaults)
610
+ */
611
+ type LlmCache = boolean | 0 | "5m" | "1h";
603
612
  interface LlmOperateOptions {
613
+ /**
614
+ * Prompt caching for the stable request prefix (system prompt + tools).
615
+ * Enabled by default; pass `false`/`0` to opt out, or `"5m"`/`"1h"` to set
616
+ * the TTL. See {@link LlmCache}.
617
+ */
618
+ cache?: LlmCache;
604
619
  data?: NaturalMap;
605
620
  /**
606
621
  * Provider-neutral reasoning effort (lowest | low | medium | high | highest).
@@ -709,6 +724,10 @@ interface LlmUsageItem {
709
724
  output: number;
710
725
  reasoning: number;
711
726
  total: number;
727
+ /** Prompt-cache tokens served from cache this call (billed at ~0.1x input) */
728
+ cacheRead?: number;
729
+ /** Prompt-cache tokens written to cache this call (billed at ~1.25x input) */
730
+ cacheWrite?: number;
712
731
  provider?: string;
713
732
  model?: string;
714
733
  }
@@ -1023,4 +1042,4 @@ declare class XaiProvider implements LlmProvider {
1023
1042
  }
1024
1043
 
1025
1044
  export { BedrockProvider, ErrorCategory, FireworksProvider, GoogleProvider as GeminiProvider, GoogleProvider, JaypieToolkit, constants as LLM, Llm, LlmError, LlmMessageRole, LlmMessageType, LlmProgressEventType, LlmQuotaError, LlmRateLimitError, LlmStreamChunkType, LlmTransientError, LlmUnrecoverableError, OpenRouterProvider, Toolkit, XaiProvider, extractReasoning, isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, jsonSchemaToNaturalSchema, naturalSchemaToJsonSchema, toolkit, tools };
1026
- export type { LlmEffort, LlmErrorOptions, LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, LlmStreamChunk, LlmStreamChunkDone, LlmStreamChunkError, LlmStreamChunkText, LlmStreamChunkToolCall, LlmStreamChunkToolResult, LlmTool };
1045
+ export type { LlmCache, LlmEffort, LlmErrorOptions, LlmExchangeCallback, LlmExchangeEnvelope, LlmExchangeRequest, LlmExchangeResolution, LlmExchangeResponse, LlmExchangeTiming, LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmModelOption, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, LlmStreamChunk, LlmStreamChunkDone, LlmStreamChunkError, LlmStreamChunkText, LlmStreamChunkToolCall, LlmStreamChunkToolResult, LlmTool };