@effect-uai/anthropic 0.11.0 → 0.12.0
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.
- package/dist/Anthropic.d.mts +18 -1
- package/dist/Anthropic.d.mts.map +1 -1
- package/dist/Anthropic.mjs +11 -1
- package/dist/Anthropic.mjs.map +1 -1
- package/dist/AnthropicTools.d.mts +12 -2
- package/dist/AnthropicTools.d.mts.map +1 -1
- package/dist/anthropicTools.test.mjs +3 -13631
- package/dist/anthropicTools.test.mjs.map +1 -1
- package/dist/codec.d.mts +16 -2
- package/dist/codec.d.mts.map +1 -1
- package/dist/codec.mjs +24 -10
- package/dist/codec.mjs.map +1 -1
- package/dist/{magic-string.es-PAZgLX4h.mjs → magic-string.es-CaWgF-3p.mjs} +1 -1
- package/dist/{magic-string.es-PAZgLX4h.mjs.map → magic-string.es-CaWgF-3p.mjs.map} +1 -1
- package/dist/test.DNmyFkvJ-CPKukVWS.mjs +13633 -0
- package/dist/test.DNmyFkvJ-CPKukVWS.mjs.map +1 -0
- package/dist/usage.test.d.mts +1 -0
- package/dist/usage.test.mjs +46 -0
- package/dist/usage.test.mjs.map +1 -0
- package/package.json +2 -2
- package/src/Anthropic.ts +36 -0
- package/src/anthropicTools.test.ts +1 -0
- package/src/codec.ts +46 -15
- package/src/usage.test.ts +54 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { emptyAccumulator, mergeUsage } from "./codec.mjs";
|
|
2
|
+
import { applyEvent } from "./streamEvents.mjs";
|
|
3
|
+
import { n as describe, r as it, t as globalExpect } from "./test.DNmyFkvJ-CPKukVWS.mjs";
|
|
4
|
+
//#region src/usage.test.ts
|
|
5
|
+
const wire = {
|
|
6
|
+
input_tokens: 94,
|
|
7
|
+
output_tokens: 210,
|
|
8
|
+
cache_read_input_tokens: 31851,
|
|
9
|
+
cache_creation_input_tokens: 604
|
|
10
|
+
};
|
|
11
|
+
describe("mergeUsage", () => {
|
|
12
|
+
it("keeps all three input buckets, not just cache reads", () => {
|
|
13
|
+
const usage = mergeUsage(emptyAccumulator, wire).usage;
|
|
14
|
+
globalExpect(usage.input_tokens).toBe(94);
|
|
15
|
+
globalExpect(usage.input_tokens_details?.cached_tokens).toBe(31851);
|
|
16
|
+
globalExpect(usage.input_tokens_details?.cache_write_tokens).toBe(604);
|
|
17
|
+
});
|
|
18
|
+
it("totals every input bucket plus output", () => {
|
|
19
|
+
const usage = mergeUsage(emptyAccumulator, wire).usage;
|
|
20
|
+
globalExpect(usage.total_tokens).toBe(32759);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe("streamed usage", () => {
|
|
24
|
+
it("computes a non-stale total across message_start and message_delta", () => {
|
|
25
|
+
const usage = applyEvent(applyEvent(emptyAccumulator, {
|
|
26
|
+
type: "message_start",
|
|
27
|
+
message: { usage: {
|
|
28
|
+
input_tokens: 94,
|
|
29
|
+
output_tokens: 1,
|
|
30
|
+
cache_read_input_tokens: 31851,
|
|
31
|
+
cache_creation_input_tokens: 604
|
|
32
|
+
} }
|
|
33
|
+
}), {
|
|
34
|
+
type: "message_delta",
|
|
35
|
+
delta: { stop_reason: "end_turn" },
|
|
36
|
+
usage: { output_tokens: 210 }
|
|
37
|
+
}).usage;
|
|
38
|
+
globalExpect(usage.output_tokens).toBe(210);
|
|
39
|
+
globalExpect(usage.input_tokens_details?.cache_write_tokens).toBe(604);
|
|
40
|
+
globalExpect(usage.total_tokens).toBe(32759);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
//#endregion
|
|
44
|
+
export {};
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=usage.test.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.test.mjs","names":[],"sources":["../src/usage.test.ts"],"sourcesContent":["import { describe, expect, it } from \"vitest\"\nimport { type WireUsage, emptyAccumulator, mergeUsage } from \"./codec.js\"\nimport { type ProviderEvent, applyEvent } from \"./streamEvents.js\"\n\n// A completed cached turn: input split across post-breakpoint tokens, cache\n// reads, and cache writes, all billed separately.\nconst wire: WireUsage = {\n input_tokens: 94,\n output_tokens: 210,\n cache_read_input_tokens: 31851,\n cache_creation_input_tokens: 604,\n}\n\ndescribe(\"mergeUsage\", () => {\n it(\"keeps all three input buckets, not just cache reads\", () => {\n const usage = mergeUsage(emptyAccumulator, wire).usage\n expect(usage.input_tokens).toBe(94)\n expect(usage.input_tokens_details?.cached_tokens).toBe(31851)\n expect(usage.input_tokens_details?.cache_write_tokens).toBe(604)\n })\n\n it(\"totals every input bucket plus output\", () => {\n const usage = mergeUsage(emptyAccumulator, wire).usage\n expect(usage.total_tokens).toBe(94 + 31851 + 604 + 210)\n })\n})\n\ndescribe(\"streamed usage\", () => {\n // Anthropic sends input + cache on message_start and the final output_tokens\n // on message_delta. total_tokens must reflect the accumulated buckets, not\n // the (input + initial output) figure frozen at message_start.\n it(\"computes a non-stale total across message_start and message_delta\", () => {\n const start: ProviderEvent = {\n type: \"message_start\",\n message: {\n usage: {\n input_tokens: 94,\n output_tokens: 1,\n cache_read_input_tokens: 31851,\n cache_creation_input_tokens: 604,\n },\n },\n }\n const delta: ProviderEvent = {\n type: \"message_delta\",\n delta: { stop_reason: \"end_turn\" },\n usage: { output_tokens: 210 },\n }\n const usage = applyEvent(applyEvent(emptyAccumulator, start), delta).usage\n expect(usage.output_tokens).toBe(210)\n expect(usage.input_tokens_details?.cache_write_tokens).toBe(604)\n expect(usage.total_tokens).toBe(94 + 31851 + 604 + 210)\n })\n})\n"],"mappings":";;;;AAMA,MAAM,OAAkB;CACtB,cAAc;CACd,eAAe;CACf,yBAAyB;CACzB,6BAA6B;CAC9B;AAED,SAAS,oBAAoB;AAC3B,IAAG,6DAA6D;EAC9D,MAAM,QAAQ,WAAW,kBAAkB,KAAK,CAAC;AACjD,eAAO,MAAM,aAAa,CAAC,KAAK,GAAG;AACnC,eAAO,MAAM,sBAAsB,cAAc,CAAC,KAAK,MAAM;AAC7D,eAAO,MAAM,sBAAsB,mBAAmB,CAAC,KAAK,IAAI;GAChE;AAEF,IAAG,+CAA+C;EAChD,MAAM,QAAQ,WAAW,kBAAkB,KAAK,CAAC;AACjD,eAAO,MAAM,aAAa,CAAC,KAAK,MAAuB;GACvD;EACF;AAEF,SAAS,wBAAwB;AAI/B,IAAG,2EAA2E;EAiB5E,MAAM,QAAQ,WAAW,WAAW,kBAAkB;GAfpD,MAAM;GACN,SAAS,EACP,OAAO;IACL,cAAc;IACd,eAAe;IACf,yBAAyB;IACzB,6BAA6B;IAC9B,EACF;GAOwD,CAAC,EAAE;GAJ5D,MAAM;GACN,OAAO,EAAE,aAAa,YAAY;GAClC,OAAO,EAAE,eAAe,KAAK;GAEoC,CAAC,CAAC;AACrE,eAAO,MAAM,cAAc,CAAC,KAAK,IAAI;AACrC,eAAO,MAAM,sBAAsB,mBAAmB,CAAC,KAAK,IAAI;AAChE,eAAO,MAAM,aAAa,CAAC,KAAK,MAAuB;GACvD;EACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-uai/anthropic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Anthropic Messages API provider for @effect-uai/core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"effect": "4.0.0-beta.94",
|
|
47
47
|
"typescript": "^6.0.3",
|
|
48
|
-
"@effect-uai/core": "0.
|
|
48
|
+
"@effect-uai/core": "0.12.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@effect-uai/core": ">=0.2.0 <1",
|
package/src/Anthropic.ts
CHANGED
|
@@ -24,6 +24,8 @@ import { type Turn, TurnEvent } from "@effect-uai/core/Turn"
|
|
|
24
24
|
import { renderProviderTools } from "./AnthropicTools.js"
|
|
25
25
|
import {
|
|
26
26
|
type Accumulator,
|
|
27
|
+
type CacheControl,
|
|
28
|
+
type CacheTtl,
|
|
27
29
|
type ThinkingConfig,
|
|
28
30
|
accumulatorToTurn,
|
|
29
31
|
buildRequestBody,
|
|
@@ -119,6 +121,21 @@ export type Config = {
|
|
|
119
121
|
* 4096 if neither is set.
|
|
120
122
|
*/
|
|
121
123
|
readonly defaultMaxTokens?: number
|
|
124
|
+
/**
|
|
125
|
+
* Prompt caching. Off by default, so the library stays unopinionated about
|
|
126
|
+
* a setting that changes billing. `true` is `{}`: cache with the 5-minute
|
|
127
|
+
* default TTL.
|
|
128
|
+
*
|
|
129
|
+
* Anthropic caches the `tools` → `system` → `messages` prefix and moves the
|
|
130
|
+
* breakpoint forward as the conversation grows, so a long system prompt and
|
|
131
|
+
* tool definitions are paid for once rather than on every turn. Read
|
|
132
|
+
* `usage.input_tokens_details.cached_tokens` to see it working.
|
|
133
|
+
*
|
|
134
|
+
* Mutating earlier history (rewriting an old message, compaction) breaks
|
|
135
|
+
* the prefix from that point on. Anthropic falls back to the longest
|
|
136
|
+
* unchanged prefix, so this costs savings but never correctness.
|
|
137
|
+
*/
|
|
138
|
+
readonly promptCaching?: boolean | { readonly ttl?: CacheTtl }
|
|
122
139
|
}
|
|
123
140
|
|
|
124
141
|
const ANTHROPIC_VERSION = "2023-06-01"
|
|
@@ -139,6 +156,24 @@ const outputConfig = (request: AnthropicRequest): Option.Option<Record<string, u
|
|
|
139
156
|
const resolvedMaxTokens = (cfg: Config, request: AnthropicRequest): number =>
|
|
140
157
|
request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS
|
|
141
158
|
|
|
159
|
+
/**
|
|
160
|
+
* One request-level breakpoint when caching is on. Anthropic places it on the
|
|
161
|
+
* last cacheable block itself, so nothing has to be marked per-block and the
|
|
162
|
+
* breakpoint stays correct as history grows.
|
|
163
|
+
*/
|
|
164
|
+
const cacheControlOf = (cfg: Config): Option.Option<CacheControl> =>
|
|
165
|
+
pipe(
|
|
166
|
+
Option.fromUndefinedOr(cfg.promptCaching),
|
|
167
|
+
Option.flatMap((opt) =>
|
|
168
|
+
opt === false
|
|
169
|
+
? Option.none<CacheControl>()
|
|
170
|
+
: Option.some<CacheControl>({
|
|
171
|
+
type: "ephemeral",
|
|
172
|
+
...(opt !== true && opt.ttl !== undefined && { ttl: opt.ttl }),
|
|
173
|
+
}),
|
|
174
|
+
),
|
|
175
|
+
)
|
|
176
|
+
|
|
142
177
|
const functionToolDescriptors = (
|
|
143
178
|
request: AnthropicRequest,
|
|
144
179
|
): ReadonlyArray<Record<string, unknown>> =>
|
|
@@ -302,6 +337,7 @@ const buildNativeStream = (cfg: Config) => {
|
|
|
302
337
|
toolChoice: toolChoiceWire(request),
|
|
303
338
|
userId: Option.fromUndefinedOr(request.user),
|
|
304
339
|
outputConfig: structured,
|
|
340
|
+
cacheControl: cacheControlOf(cfg),
|
|
305
341
|
})
|
|
306
342
|
|
|
307
343
|
const body = yield* Result.match(bodyResult, {
|
package/src/codec.ts
CHANGED
|
@@ -324,6 +324,20 @@ export type ThinkingConfig = {
|
|
|
324
324
|
readonly budget_tokens: number
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
/** Cache lifetime. `1h` writes cost 2x base input; `5m` costs 1.25x. */
|
|
328
|
+
export type CacheTtl = "5m" | "1h"
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Request-level cache breakpoint. Anthropic applies it to the last cacheable
|
|
332
|
+
* block and moves it forward as the conversation grows, so the cached prefix
|
|
333
|
+
* (`tools` → `system` → `messages`) follows the conversation without the
|
|
334
|
+
* caller re-placing anything.
|
|
335
|
+
*/
|
|
336
|
+
export type CacheControl = {
|
|
337
|
+
readonly type: "ephemeral"
|
|
338
|
+
readonly ttl?: CacheTtl
|
|
339
|
+
}
|
|
340
|
+
|
|
327
341
|
export type RequestBody = {
|
|
328
342
|
readonly model: string
|
|
329
343
|
readonly messages: ReadonlyArray<RequestMessage>
|
|
@@ -338,6 +352,7 @@ export type RequestBody = {
|
|
|
338
352
|
readonly tool_choice?: Record<string, unknown>
|
|
339
353
|
readonly metadata?: { readonly user_id: string }
|
|
340
354
|
readonly output_config?: Record<string, unknown>
|
|
355
|
+
readonly cache_control?: CacheControl
|
|
341
356
|
readonly stream: true
|
|
342
357
|
}
|
|
343
358
|
|
|
@@ -354,6 +369,7 @@ export const buildRequestBody = (params: {
|
|
|
354
369
|
readonly toolChoice: Option.Option<Record<string, unknown>>
|
|
355
370
|
readonly userId: Option.Option<string>
|
|
356
371
|
readonly outputConfig: Option.Option<Record<string, unknown>>
|
|
372
|
+
readonly cacheControl: Option.Option<CacheControl>
|
|
357
373
|
}): Result.Result<RequestBody, JsonParseError> =>
|
|
358
374
|
pipe(
|
|
359
375
|
groupedMessages(params.history),
|
|
@@ -402,6 +418,10 @@ export const buildRequestBody = (params: {
|
|
|
402
418
|
onNone: () => ({}),
|
|
403
419
|
onSome: (output_config) => ({ output_config }),
|
|
404
420
|
}),
|
|
421
|
+
...Option.match(params.cacheControl, {
|
|
422
|
+
onNone: () => ({}),
|
|
423
|
+
onSome: (cache_control) => ({ cache_control }),
|
|
424
|
+
}),
|
|
405
425
|
stream: true,
|
|
406
426
|
}),
|
|
407
427
|
),
|
|
@@ -503,23 +523,34 @@ export const setStopReason = (acc: Accumulator, reason: string): Accumulator =>
|
|
|
503
523
|
stopReason: Option.some(reason),
|
|
504
524
|
})
|
|
505
525
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
526
|
+
// Anthropic splits input across three billed buckets: `input_tokens`
|
|
527
|
+
// (post-breakpoint), cache reads, and cache writes. All three are kept;
|
|
528
|
+
// `input_tokens` is not the input total. Each field carries over from the
|
|
529
|
+
// prior value since a streamed turn spreads usage across events (input +
|
|
530
|
+
// cache on message_start, output_tokens on message_delta), so total_tokens is
|
|
531
|
+
// summed from the accumulated buckets, not one event.
|
|
509
532
|
export const mergeUsage = (acc: Accumulator, wire: WireUsage): Accumulator => {
|
|
510
|
-
const
|
|
533
|
+
const prev = acc.usage
|
|
534
|
+
const input_tokens = wire.input_tokens ?? prev.input_tokens
|
|
535
|
+
const output_tokens = wire.output_tokens ?? prev.output_tokens
|
|
536
|
+
const cached_tokens = wire.cache_read_input_tokens ?? prev.input_tokens_details?.cached_tokens
|
|
537
|
+
const cache_write_tokens =
|
|
538
|
+
wire.cache_creation_input_tokens ?? prev.input_tokens_details?.cache_write_tokens
|
|
539
|
+
|
|
540
|
+
const buckets = [input_tokens, cached_tokens, cache_write_tokens, output_tokens]
|
|
541
|
+
const total_tokens = buckets.every((v) => v === undefined)
|
|
542
|
+
? undefined
|
|
543
|
+
: buckets.reduce<number>((sum, v) => sum + (v ?? 0), 0)
|
|
544
|
+
|
|
545
|
+
const details: Items.InputTokensDetails = {
|
|
546
|
+
...(cached_tokens !== undefined && { cached_tokens }),
|
|
547
|
+
...(cache_write_tokens !== undefined && { cache_write_tokens }),
|
|
548
|
+
}
|
|
511
549
|
const usage: Items.Usage = {
|
|
512
|
-
...
|
|
513
|
-
...(
|
|
514
|
-
...(
|
|
515
|
-
...(
|
|
516
|
-
wire.output_tokens !== undefined && {
|
|
517
|
-
total_tokens: wire.input_tokens + wire.output_tokens,
|
|
518
|
-
}),
|
|
519
|
-
...Option.match(cached, {
|
|
520
|
-
onNone: () => ({}),
|
|
521
|
-
onSome: (cached_tokens) => ({ input_tokens_details: { cached_tokens } }),
|
|
522
|
-
}),
|
|
550
|
+
...(input_tokens !== undefined && { input_tokens }),
|
|
551
|
+
...(output_tokens !== undefined && { output_tokens }),
|
|
552
|
+
...(total_tokens !== undefined && { total_tokens }),
|
|
553
|
+
...(Object.keys(details).length > 0 && { input_tokens_details: details }),
|
|
523
554
|
}
|
|
524
555
|
return { ...acc, usage }
|
|
525
556
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { type WireUsage, emptyAccumulator, mergeUsage } from "./codec.js"
|
|
3
|
+
import { type ProviderEvent, applyEvent } from "./streamEvents.js"
|
|
4
|
+
|
|
5
|
+
// A completed cached turn: input split across post-breakpoint tokens, cache
|
|
6
|
+
// reads, and cache writes, all billed separately.
|
|
7
|
+
const wire: WireUsage = {
|
|
8
|
+
input_tokens: 94,
|
|
9
|
+
output_tokens: 210,
|
|
10
|
+
cache_read_input_tokens: 31851,
|
|
11
|
+
cache_creation_input_tokens: 604,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("mergeUsage", () => {
|
|
15
|
+
it("keeps all three input buckets, not just cache reads", () => {
|
|
16
|
+
const usage = mergeUsage(emptyAccumulator, wire).usage
|
|
17
|
+
expect(usage.input_tokens).toBe(94)
|
|
18
|
+
expect(usage.input_tokens_details?.cached_tokens).toBe(31851)
|
|
19
|
+
expect(usage.input_tokens_details?.cache_write_tokens).toBe(604)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("totals every input bucket plus output", () => {
|
|
23
|
+
const usage = mergeUsage(emptyAccumulator, wire).usage
|
|
24
|
+
expect(usage.total_tokens).toBe(94 + 31851 + 604 + 210)
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe("streamed usage", () => {
|
|
29
|
+
// Anthropic sends input + cache on message_start and the final output_tokens
|
|
30
|
+
// on message_delta. total_tokens must reflect the accumulated buckets, not
|
|
31
|
+
// the (input + initial output) figure frozen at message_start.
|
|
32
|
+
it("computes a non-stale total across message_start and message_delta", () => {
|
|
33
|
+
const start: ProviderEvent = {
|
|
34
|
+
type: "message_start",
|
|
35
|
+
message: {
|
|
36
|
+
usage: {
|
|
37
|
+
input_tokens: 94,
|
|
38
|
+
output_tokens: 1,
|
|
39
|
+
cache_read_input_tokens: 31851,
|
|
40
|
+
cache_creation_input_tokens: 604,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
const delta: ProviderEvent = {
|
|
45
|
+
type: "message_delta",
|
|
46
|
+
delta: { stop_reason: "end_turn" },
|
|
47
|
+
usage: { output_tokens: 210 },
|
|
48
|
+
}
|
|
49
|
+
const usage = applyEvent(applyEvent(emptyAccumulator, start), delta).usage
|
|
50
|
+
expect(usage.output_tokens).toBe(210)
|
|
51
|
+
expect(usage.input_tokens_details?.cache_write_tokens).toBe(604)
|
|
52
|
+
expect(usage.total_tokens).toBe(94 + 31851 + 604 + 210)
|
|
53
|
+
})
|
|
54
|
+
})
|