llm-large-files 0.4.1 → 0.7.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/CHANGELOG.md +1400 -338
- package/README.md +217 -49
- package/dist/analyze.d.ts +5 -0
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +96 -47
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts +117 -0
- package/dist/anthropic/adapter.d.ts.map +1 -0
- package/dist/anthropic/adapter.js +542 -0
- package/dist/anthropic/adapter.js.map +1 -0
- package/dist/anthropic/index.d.ts +3 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +2 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/ask.d.ts +200 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +1402 -0
- package/dist/ask.js.map +1 -0
- package/dist/budget.d.ts +23 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +64 -0
- package/dist/budget.js.map +1 -0
- package/dist/encoding.d.ts +17 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +852 -0
- package/dist/encoding.js.map +1 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +333 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +159 -0
- package/dist/events.js.map +1 -0
- package/dist/evidence.d.ts +42 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +138 -0
- package/dist/evidence.js.map +1 -0
- package/dist/extract.d.ts +35 -3
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +626 -58
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +59 -0
- package/dist/gemini/adapter.d.ts.map +1 -0
- package/dist/gemini/adapter.js +226 -0
- package/dist/gemini/adapter.js.map +1 -0
- package/dist/gemini/chat.d.ts +53 -77
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +42 -300
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/errors.d.ts +16 -0
- package/dist/gemini/errors.d.ts.map +1 -0
- package/dist/gemini/errors.js +46 -0
- package/dist/gemini/errors.js.map +1 -0
- package/dist/gemini/generate.d.ts +18 -21
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +49 -92
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +6 -4
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +4 -3
- package/dist/gemini/index.js.map +1 -1
- package/dist/gemini/sse.d.ts +1 -2
- package/dist/gemini/sse.d.ts.map +1 -1
- package/dist/gemini/sse.js +1 -38
- package/dist/gemini/sse.js.map +1 -1
- package/dist/http.d.ts +136 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +401 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +18 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +96 -8
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +414 -96
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +37 -1
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +255 -61
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +278 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +222 -0
- package/dist/model.js.map +1 -0
- package/dist/node/handlers.d.ts +20 -4
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +122 -20
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/sse.d.ts +59 -0
- package/dist/node/sse.d.ts.map +1 -0
- package/dist/node/sse.js +174 -0
- package/dist/node/sse.js.map +1 -0
- package/dist/openai/adapter.d.ts +109 -0
- package/dist/openai/adapter.d.ts.map +1 -0
- package/dist/openai/adapter.js +567 -0
- package/dist/openai/adapter.js.map +1 -0
- package/dist/openai/index.d.ts +3 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +2 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/output-limit.d.ts +42 -0
- package/dist/output-limit.d.ts.map +1 -0
- package/dist/output-limit.js +121 -0
- package/dist/output-limit.js.map +1 -0
- package/dist/pause.d.ts +24 -0
- package/dist/pause.d.ts.map +1 -0
- package/dist/pause.js +38 -0
- package/dist/pause.js.map +1 -0
- package/dist/pdf-pages.d.ts +32 -2
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +275 -14
- package/dist/pdf-pages.js.map +1 -1
- package/dist/pdf-pictures.d.ts +30 -3
- package/dist/pdf-pictures.d.ts.map +1 -1
- package/dist/pdf-pictures.js +50 -8
- package/dist/pdf-pictures.js.map +1 -1
- package/dist/pdf-work.d.ts +40 -0
- package/dist/pdf-work.d.ts.map +1 -0
- package/dist/pdf-work.js +734 -0
- package/dist/pdf-work.js.map +1 -0
- package/dist/plan.d.ts +22 -4
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +61 -12
- package/dist/plan.js.map +1 -1
- package/dist/retry.d.ts +76 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +141 -0
- package/dist/retry.js.map +1 -0
- package/dist/search.d.ts +8 -2
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +60 -10
- package/dist/search.js.map +1 -1
- package/dist/sse.d.ts +36 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +153 -0
- package/dist/sse.js.map +1 -0
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +5 -2
- package/dist/stores.js.map +1 -1
- package/dist/text-index.d.ts +6 -0
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +25 -8
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +62 -2
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +314 -14
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +72 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +341 -60
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +128 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/zip-directory.d.ts +43 -0
- package/dist/zip-directory.d.ts.map +1 -0
- package/dist/zip-directory.js +130 -0
- package/dist/zip-directory.js.map +1 -0
- package/docs/README.md +288 -0
- package/docs/api.md +58 -184
- package/docs/configuration.md +34 -490
- package/docs/getting-started.md +412 -177
- package/docs/guides/anthropic.md +590 -0
- package/docs/guides/conversations.md +629 -0
- package/docs/guides/costs-and-limits.md +726 -0
- package/docs/guides/deployment.md +654 -0
- package/docs/guides/errors-and-reliability.md +735 -0
- package/docs/guides/explorer-model.md +399 -0
- package/docs/guides/openai-compatible.md +602 -0
- package/docs/guides/own-adapter-and-loop.md +1169 -0
- package/docs/guides/pdfs.md +971 -0
- package/docs/guides/storage.md +731 -0
- package/docs/guides/testing.md +1199 -0
- package/docs/guides/vertex-and-gemini.md +717 -0
- package/docs/guides/web-app.md +1589 -0
- package/docs/how-it-works.md +1022 -138
- package/docs/measurements.md +881 -0
- package/docs/publishing.md +274 -46
- package/docs/reference/anthropic.md +427 -0
- package/docs/reference/ask.md +892 -0
- package/docs/reference/errors-and-retries.md +758 -0
- package/docs/reference/events.md +993 -0
- package/docs/reference/gemini.md +823 -0
- package/docs/reference/library.md +1467 -0
- package/docs/reference/node.md +528 -0
- package/docs/reference/openai.md +385 -0
- package/docs/reference/tools.md +883 -0
- package/docs/security.md +902 -90
- package/docs/troubleshooting.md +1390 -0
- package/package.json +22 -4
- package/src/analyze.ts +93 -42
- package/src/anthropic/adapter.ts +611 -0
- package/src/anthropic/index.ts +2 -0
- package/src/ask.ts +1560 -0
- package/src/budget.ts +71 -0
- package/src/encoding.ts +850 -0
- package/src/errors.ts +17 -0
- package/src/events.ts +442 -0
- package/src/evidence.ts +165 -0
- package/src/extract.ts +602 -62
- package/src/gemini/adapter.ts +271 -0
- package/src/gemini/chat.ts +87 -355
- package/src/gemini/errors.ts +48 -0
- package/src/gemini/generate.ts +67 -94
- package/src/gemini/index.ts +6 -4
- package/src/gemini/sse.ts +1 -34
- package/src/http.ts +434 -0
- package/src/index.ts +55 -4
- package/src/library.ts +497 -97
- package/src/map-reduce.ts +266 -59
- package/src/model.ts +433 -0
- package/src/node/handlers.ts +137 -22
- package/src/node/index.ts +2 -0
- package/src/node/sse.ts +198 -0
- package/src/openai/adapter.ts +613 -0
- package/src/openai/index.ts +2 -0
- package/src/output-limit.ts +135 -0
- package/src/pause.ts +41 -0
- package/src/pdf-pages.ts +268 -15
- package/src/pdf-pictures.ts +56 -9
- package/src/pdf-work.ts +722 -0
- package/src/plan.ts +68 -12
- package/src/retry.ts +176 -0
- package/src/search.ts +63 -10
- package/src/sse.ts +159 -0
- package/src/stores.ts +5 -2
- package/src/text-index.ts +26 -7
- package/src/text.ts +323 -13
- package/src/tools.ts +338 -62
- package/src/types.ts +129 -5
- package/src/zip-directory.ts +136 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
# Claude
|
|
2
|
+
|
|
3
|
+
This guide is for you if Claude answers the questions about your files. It covers what
|
|
4
|
+
`anthropic()` needs, the options that matter, gateways, what a conversation that may only grow
|
|
5
|
+
changes in the tool loop, pages of PDFs, errors, conversations you keep as Messages API messages,
|
|
6
|
+
and Claude answering while a Gemini model explores. Every option and its default is in the
|
|
7
|
+
reference: [`llm-large-files/anthropic`](../reference/anthropic.md).
|
|
8
|
+
|
|
9
|
+
## Set up `anthropic()`
|
|
10
|
+
|
|
11
|
+
`anthropic()` from `llm-large-files/anthropic` makes Claude a model of `askWithFiles`. It needs no
|
|
12
|
+
SDK: it posts to the Messages API with `fetch`. It needs two things:
|
|
13
|
+
|
|
14
|
+
- `model`: the model ID, such as `"claude-opus-5-5"`.
|
|
15
|
+
- A credential: `apiKey`, sent as `x-api-key`, or `authToken`, sent as `Authorization: Bearer …`
|
|
16
|
+
(an OAuth token, or a gateway's token). Either can be a function: it is called for each request,
|
|
17
|
+
so a token can be renewed.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { readFile } from "node:fs/promises";
|
|
21
|
+
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
22
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
23
|
+
|
|
24
|
+
const files = createLargeFiles({ store: new MemoryDocumentStore() });
|
|
25
|
+
const meta = await files.ingest({ name: "app.log", bytes: await readFile("app.log") });
|
|
26
|
+
|
|
27
|
+
const claude = anthropic({
|
|
28
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
29
|
+
model: "claude-opus-5-5",
|
|
30
|
+
contextTokens: 1_000_000, // your model's window: the budget of a question follows it
|
|
31
|
+
fallbacks: "default", // another model answers when a safety classifier declines
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const result = await askWithFiles({
|
|
35
|
+
files,
|
|
36
|
+
model: claude,
|
|
37
|
+
documentIds: [meta.id],
|
|
38
|
+
messages: [{ role: "user", text: "Why did the nightly import fail?" }],
|
|
39
|
+
onText: (delta) => process.stdout.write(delta),
|
|
40
|
+
});
|
|
41
|
+
if (!result.answered) console.log(result.blockReason ? "\nThe request was declined." : "\nNo answer this time.");
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
With these options, each chat request is a `POST` to `https://api.anthropic.com/v1/messages` with:
|
|
45
|
+
|
|
46
|
+
- the headers `x-api-key`, `anthropic-version: 2026-10-21` (the exported `ANTHROPIC_VERSION`) and,
|
|
47
|
+
because `fallbacks` is set, `anthropic-beta: server-side-fallback-2026-07-01`;
|
|
48
|
+
- a body of `model`, `max_tokens: 64000`, `system` (when there is one), `messages`,
|
|
49
|
+
`stream: true`, `cache_control: { type: "ephemeral" }` and `fallbacks`, plus `tools` when the
|
|
50
|
+
question uses them (and `tool_choice: { type: "none" }` in the request for the answer).
|
|
51
|
+
|
|
52
|
+
No sampling settings are sent: the API version in `ANTHROPIC_VERSION` refuses `temperature`, so do
|
|
53
|
+
not add sampling settings in `body`.
|
|
54
|
+
|
|
55
|
+
`usage.byModel` names the model by its `model` ID; set `id` for another name.
|
|
56
|
+
|
|
57
|
+
The adapter has not been run against the real Messages API, in 0.6.0 or any earlier version. It
|
|
58
|
+
is tested against the request and response shapes of the Messages API documentation.
|
|
59
|
+
[Measurements](../measurements.md) lists what has been run live.
|
|
60
|
+
|
|
61
|
+
## The options that matter
|
|
62
|
+
|
|
63
|
+
| Option | Default | What it does |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `contextTokens` | none | The model's context window. The budget of a question follows it. |
|
|
66
|
+
| `maxTokens` | 64,000 | `max_tokens` of a chat request: the most the model may write, thinking included. It is kept free in the window. |
|
|
67
|
+
| `tokenScale` | 1.35 | How many of Claude's tokens one token of the library's estimate comes to. |
|
|
68
|
+
| `body`, `analysisBody` | none | More fields for every chat request, and `(maxOutputTokens) => fields` for the part reads of a whole-file read: `thinking`, `output_config`. |
|
|
69
|
+
| `firstByteTimeoutMs`, `idleTimeoutMs` | 600,000 and 300,000 | Waits on a chat reply that sends nothing. 0 waits without end. |
|
|
70
|
+
| `timeoutMs` | 180,000 | Milliseconds one part read of a whole-file read may take. |
|
|
71
|
+
|
|
72
|
+
The sections below explain each, and more: `stream` and `eagerInputStreaming` in
|
|
73
|
+
[Streaming and waits](#streaming-and-waits), `cache` in [Prompt caching](#prompt-caching),
|
|
74
|
+
`fallbacks` in [Fallbacks and refusals](#fallbacks-and-refusals), the gateway options in
|
|
75
|
+
[Gateways and other hosts](#gateways-and-other-hosts), and `toolFilePlacement` and `pageTokens` in
|
|
76
|
+
[PDF pages and pictures](#pdf-pages-and-pictures). Every other option is in
|
|
77
|
+
[the reference](../reference/anthropic.md).
|
|
78
|
+
|
|
79
|
+
### The window: `contextTokens`, `maxTokens`, `tokenScale`
|
|
80
|
+
|
|
81
|
+
The library plans a question in its own estimate of tokens (`estimateTokens`), which is fitted to
|
|
82
|
+
Gemini. Three numbers turn Claude's window into that budget:
|
|
83
|
+
|
|
84
|
+
1. **The window** is the smaller of the library's `budget.contextTokens` and the adapter's
|
|
85
|
+
`contextTokens`. `anthropic()` declares none unless you set it, so without it the library's
|
|
86
|
+
window holds: 1,000,000 tokens unless you set another in `createLargeFiles({ budget })`.
|
|
87
|
+
2. **`tokenScale`** divides it: Claude counts 1.35 tokens, by default, for one token of the
|
|
88
|
+
estimate, so its window is that much smaller in the library's terms.
|
|
89
|
+
3. **`maxTokens`**, divided the same way, is kept free: Claude counts `max_tokens` against the
|
|
90
|
+
window. At least a quarter of the window is left for the prompt.
|
|
91
|
+
|
|
92
|
+
The rest of the budget (room for documents, for tool results, for pages) follows the window that
|
|
93
|
+
is left. `result.budget` shows what a question ran with:
|
|
94
|
+
|
|
95
|
+
| `contextTokens` | `maxTokens` | `result.budget.contextTokens` |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| not set | 64,000 (default) | 693,332 |
|
|
98
|
+
| 200,000 | 64,000 (default) | 100,740 |
|
|
99
|
+
| 200,000 | 16,000 | 136,296 |
|
|
100
|
+
| 102,600 | 16,200 | 64,000 |
|
|
101
|
+
|
|
102
|
+
- **Set `contextTokens`** whenever your model's window is smaller than the library's. A budget
|
|
103
|
+
larger than the window makes requests the API refuses, and with Claude the loop can recover
|
|
104
|
+
from that only before the first tool ran ([below](#a-conversation-that-may-only-grow)).
|
|
105
|
+
- **`maxTokens` is room every request keeps free**, and the room the model has to think and write.
|
|
106
|
+
A reply cut at the limit (`finishReason: "max_tokens"`) loses every tool call in it, including
|
|
107
|
+
calls whose input was complete: each is answered "This call was cut off at the output limit…"
|
|
108
|
+
and not run. Lower it for a small window only as far as your answers allow.
|
|
109
|
+
- **`tokenScale` is a cautious guess.** During a question, the count Claude reports with each reply
|
|
110
|
+
raises the estimate when the text costs more than the scale says; it never lowers it below the
|
|
111
|
+
scale. If you measure the ratio for your own files (Anthropic's count of a text against
|
|
112
|
+
`estimateTokens` of it), set `tokenScale` to it.
|
|
113
|
+
|
|
114
|
+
The adapter also says that Claude takes requests of up to 32 MB (`maxRequestBytes`). A question
|
|
115
|
+
sends no more than the smaller of that and the library's `budget.maxRequestBytes`, which is 10 MB
|
|
116
|
+
by default. Raise the library's when you want the 32 MB, for example for pages of PDFs.
|
|
117
|
+
|
|
118
|
+
### Thinking and effort
|
|
119
|
+
|
|
120
|
+
The adapter sends no `thinking` or `output_config` field of its own, so the model's own defaults
|
|
121
|
+
apply. Put the fields in `body` for chat requests and in `analysisBody` for the part reads of a
|
|
122
|
+
whole-file read:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
126
|
+
|
|
127
|
+
const claude = anthropic({
|
|
128
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
129
|
+
model: "claude-opus-5-5",
|
|
130
|
+
body: { output_config: { effort: "high" } }, // every chat request
|
|
131
|
+
analysisBody: () => ({ output_config: { effort: "low" } }), // the part reads
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Which `thinking` and `output_config` values a model takes is the API's business: see Anthropic's
|
|
136
|
+
documentation for your model. What the library does with thinking is the same for every model:
|
|
137
|
+
|
|
138
|
+
- **It goes back unchanged to Claude.** A turn of Claude's, thinking blocks included, is sent back
|
|
139
|
+
as it came in Claude's later requests of the question, except for what the API would refuse: an
|
|
140
|
+
empty text block that a stream left after the last thinking block is left out, and a call whose
|
|
141
|
+
input was not valid JSON goes back with `{}`. Another model of the question (the answering
|
|
142
|
+
model, when Claude explores) gets the turn rebuilt from its tool calls, without thinking blocks.
|
|
143
|
+
- **It is not shown.** `onText` and `result.text` carry text blocks only.
|
|
144
|
+
- **It is not stored.** `result.toolTurns` hold the calls and their results, and
|
|
145
|
+
`fromAnthropicMessages` leaves thinking blocks out: they belong to the question they were
|
|
146
|
+
written in.
|
|
147
|
+
- **It is counted in `outputTokens`.** The adapter reports no `thoughtTokens` (always 0), for part
|
|
148
|
+
reads too.
|
|
149
|
+
- **It counts against `maxTokens`.** A part read that comes back cut off at its limit with no text,
|
|
150
|
+
or that thought most of its limit away and wrote a little, is asked for once more with 32,768
|
|
151
|
+
tokens (`THINKING_OUTPUT_TOKENS`, exported by `llm-large-files/gemini`), and later part reads of
|
|
152
|
+
that model start there. Claude does not count its thoughts apart: in a part read that thought,
|
|
153
|
+
they are taken as what it wrote besides its text (its `output_tokens` less the tokens of the
|
|
154
|
+
text). 0.5.0 counted none of them, so such notes came back cut, with the little the model wrote.
|
|
155
|
+
Gemini 3.8 Flash, which spends its limit on thoughts the same way, wrote two lines when asked for
|
|
156
|
+
4,096 tokens ([Measurements](../measurements.md#reading-a-whole-file)).
|
|
157
|
+
|
|
158
|
+
`body` and `analysisBody` are added last, so a field in them replaces the adapter's own. Set
|
|
159
|
+
`max_tokens` with `maxTokens`, streaming with `stream` and caching with `cache`: the budget and the
|
|
160
|
+
reading of the reply follow the options, not `body`.
|
|
161
|
+
|
|
162
|
+
### Streaming and waits
|
|
163
|
+
|
|
164
|
+
- **Every chat request is streamed** (`stream: true`), whether or not you pass `onText`. With
|
|
165
|
+
`onText`, the answer's text arrives as it is written. A reply that is not `text/event-stream` is
|
|
166
|
+
read by what its body starts with: as events after `data:` or a comment (a gateway that streams
|
|
167
|
+
under `application/octet-stream`, or under no type), as JSON in one piece after `{`. `stream:
|
|
168
|
+
false` asks for no stream.
|
|
169
|
+
- **`eagerInputStreaming`** sets `eager_input_streaming: true` on each tool of a streamed request,
|
|
170
|
+
so the input of a call arrives as it is written. It defaults to `true` for api.anthropic.com and
|
|
171
|
+
`false` for any other URL, which may refuse the field. Either way the input is read once its
|
|
172
|
+
block is complete, and only a JSON object is taken. A call whose input is not one, any call of a
|
|
173
|
+
reply cut off at `max_tokens`, and a call whose block never closed are answered with what was
|
|
174
|
+
wrong and not run, and Claude can send them again.
|
|
175
|
+
- **Waits on silence.** A chat request that gets nothing for `firstByteTimeoutMs` (600 s) before the
|
|
176
|
+
first piece of its reply, or for `idleTimeoutMs` (300 s) between two pieces, is aborted and sent
|
|
177
|
+
again, once, unless text of it was already shown. Headers do not count as a piece; any bytes of
|
|
178
|
+
the stream do, Claude's pings included. The error is an `AnthropicRequestError` that names the
|
|
179
|
+
wait that ran out ("Claude sent nothing for 600 s.", or 300 s between pieces), with `status` 0
|
|
180
|
+
and `kind: "timeout"`. 0 waits without end. With `stream: false` the reply comes whole at the
|
|
181
|
+
end, so `firstByteTimeoutMs` must cover all of it.
|
|
182
|
+
- **A stream cut short is not a reply.** A stream that ends before the `message_delta` event that
|
|
183
|
+
gives `stop_reason` (a gateway that closed it early) throws "Claude's reply ended before it was
|
|
184
|
+
complete." with `status` 0. It is sent again when none of it was shown, and `onActivity` says
|
|
185
|
+
"The model's reply was cut off on its way. Trying again…".
|
|
186
|
+
- **A stream that ended is a reply.** After `message_stop`, the body is read for one more second at
|
|
187
|
+
most, then cancelled: a gateway that leaves the connection open does not hold the request until
|
|
188
|
+
`idleTimeoutMs`, and a connection that breaks after `message_stop` loses nothing. 0.5.0 waited
|
|
189
|
+
for such a connection to close, and took one that broke for a model that could not be reached,
|
|
190
|
+
with the whole answer already shown.
|
|
191
|
+
- **Part reads are not streamed.** Each may take `timeoutMs` (180 s); one that takes longer is
|
|
192
|
+
stopped and tried once more.
|
|
193
|
+
|
|
194
|
+
Streaming the answer on to a browser is in [web app](./web-app.md), and proxies that cut long
|
|
195
|
+
streams in [deployment](./deployment.md).
|
|
196
|
+
|
|
197
|
+
### Prompt caching
|
|
198
|
+
|
|
199
|
+
With `cache: true` (the default) each chat request carries a top-level
|
|
200
|
+
`cache_control: { type: "ephemeral" }`. It caches the request up to its last block, so each request
|
|
201
|
+
of the tool loop, and a follow-up question, reads what the one before wrote. `"5m"` is the same,
|
|
202
|
+
`"1h"` keeps it an hour (`ttl: "1h"`), and `false` sends none. Part reads carry none: each part is
|
|
203
|
+
sent once. Keep `documentPlacement` at `"first"`, the default of `askWithFiles`: the documents
|
|
204
|
+
then stay at the start of the conversation, where a follow-up question reads them from the cache.
|
|
205
|
+
|
|
206
|
+
In `usage`, `inputTokens` counts every token of the prompts: those not cached, those written to
|
|
207
|
+
the cache, and those read from it. `cachedInputTokens` is the part read. Cache writes have no field
|
|
208
|
+
of their own, and Anthropic bills them at another rate than plain input. To price them, read
|
|
209
|
+
Claude's own usage of each reply, `providerUsage`, by wrapping the adapter:
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import type { ModelAdapter } from "llm-large-files";
|
|
213
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
214
|
+
|
|
215
|
+
const base = anthropic({ apiKey: process.env.ANTHROPIC_API_KEY!, model: "claude-opus-5-5" });
|
|
216
|
+
|
|
217
|
+
const claude: ModelAdapter = {
|
|
218
|
+
...base,
|
|
219
|
+
async send(request, hooks) {
|
|
220
|
+
const reply = await base.send(request, hooks);
|
|
221
|
+
// Claude's usage object: input_tokens, cache_creation_input_tokens, cache_read_input_tokens, output_tokens.
|
|
222
|
+
console.log(base.id, reply.providerUsage);
|
|
223
|
+
return reply;
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
`result.lastUsage` is the same object for the last chat request of a question. Turning usage into
|
|
229
|
+
cost per user or session is in [costs and limits](./costs-and-limits.md).
|
|
230
|
+
|
|
231
|
+
### Fallbacks and refusals
|
|
232
|
+
|
|
233
|
+
`fallbacks: "default"`, or a list such as `[{ model: "claude-opus-4-8" }]`, adds the `fallbacks`
|
|
234
|
+
field to every chat request and `server-side-fallback-2026-07-01` to `anthropic-beta`: when a
|
|
235
|
+
safety classifier declines the request, another model answers it. It is off unless you set it. The
|
|
236
|
+
adapter's own note says it is offered on the Claude API, not on Amazon Bedrock, Vertex AI or
|
|
237
|
+
Microsoft Foundry, and not for every model.
|
|
238
|
+
|
|
239
|
+
- A reply in which another model took over goes back with what came after the last switch: the
|
|
240
|
+
thinking and calls of the model that declined are left out, and its text is kept.
|
|
241
|
+
- `usage` counts every attempt of such a reply: both were billed.
|
|
242
|
+
|
|
243
|
+
When the request is declined (`stop_reason: "refusal"`; with `fallbacks`, by every model), the
|
|
244
|
+
result has `answered: false` and `blockReason: "refusal"`, or `"refusal: cyber"` when the API names
|
|
245
|
+
a category. No call of that reply runs, and nothing more is asked. Text that Claude wrote before it
|
|
246
|
+
stopped was already passed to `onText` and is in `result.text`: replace it with your own message.
|
|
247
|
+
|
|
248
|
+
## Gateways and other hosts
|
|
249
|
+
|
|
250
|
+
A gateway that speaks the Messages API takes the same adapter:
|
|
251
|
+
|
|
252
|
+
| Option | Default | What it does |
|
|
253
|
+
|---|---|---|
|
|
254
|
+
| `baseUrl` | `https://api.anthropic.com` | The base of the API: `/v1/messages` is added to it. |
|
|
255
|
+
| `url` | `baseUrl` + `/v1/messages` | The whole Messages URL, when it is not that. |
|
|
256
|
+
| `authToken` | none | Sent as `Authorization: Bearer …`. A function is called for each request. |
|
|
257
|
+
| `headers` | none | More headers, or a function that returns them for each request. Added last, so they can replace the adapter's own. |
|
|
258
|
+
| `version` | `ANTHROPIC_VERSION` (`"2026-10-21"`) | The `anthropic-version` header. |
|
|
259
|
+
| `betas` | none | Values of the `anthropic-beta` header, joined with commas. |
|
|
260
|
+
| `maxRequestBytes` | 32,000,000 | The largest request the gateway takes, when it is lower. |
|
|
261
|
+
| `fetch` | the global `fetch` | Your own `fetch`: a proxy, logging, tests. |
|
|
262
|
+
|
|
263
|
+
A gateway that cannot stream needs `stream: false`, and one that passes `eager_input_streaming` on
|
|
264
|
+
can take `eagerInputStreaming: true`: see [Streaming and waits](#streaming-and-waits).
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
268
|
+
|
|
269
|
+
const claude = anthropic({
|
|
270
|
+
model: "claude-opus-5-5",
|
|
271
|
+
baseUrl: process.env.CLAUDE_GATEWAY_URL!, // "https://llm-gateway.example.com/anthropic"
|
|
272
|
+
authToken: () => process.env.CLAUDE_GATEWAY_TOKEN!, // called for each request: return a fresh token here
|
|
273
|
+
headers: { "X-Team": "search" },
|
|
274
|
+
maxRequestBytes: 8_000_000, // the gateway's own limit
|
|
275
|
+
contextTokens: 1_000_000,
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
A gateway that refuses a request with HTTP 413 is taken to have refused it as too large
|
|
280
|
+
(`kind: "context_overflow"`), so give it the gateway's limit in `maxRequestBytes` and the request
|
|
281
|
+
stays under it.
|
|
282
|
+
|
|
283
|
+
### Claude on Vertex AI, Amazon Bedrock or Microsoft Foundry
|
|
284
|
+
|
|
285
|
+
The adapter speaks the Claude API's own Messages endpoint: it posts to `url`, sends the
|
|
286
|
+
`anthropic-version` header and `model` in the body. Whether a cloud platform's Claude endpoint
|
|
287
|
+
takes requests in that form has not been checked, and `fallbacks` is not offered there. The `fetch`
|
|
288
|
+
option sees every request (URL, headers and body) and can change it on its way, but no such path
|
|
289
|
+
is tested. If you run Claude on one of these platforms, put a gateway that takes the Messages API
|
|
290
|
+
in front of it, or [write an adapter](./own-adapter-and-loop.md) for the platform's own form.
|
|
291
|
+
|
|
292
|
+
## A conversation that may only grow
|
|
293
|
+
|
|
294
|
+
`anthropic()` declares `capabilities.appendOnly: true`. Claude checks its thinking blocks against
|
|
295
|
+
the conversation they were written in, so an earlier turn that is rewritten makes the later ones
|
|
296
|
+
invalid. The loop therefore never rewrites a turn of a question in which a model that is
|
|
297
|
+
append-only takes part: Claude alone, Claude answering, or Claude exploring. Three things change.
|
|
298
|
+
|
|
299
|
+
**Old results are never replaced by stand-ins.** With other models, when the room in the request
|
|
300
|
+
runs short, the loop replaces the results of older tool rounds with short stand-ins, and `steps`
|
|
301
|
+
says "Older tool results were shortened to make room in the request". With Claude every result
|
|
302
|
+
stays as it was. When the calls of a turn do not all fit, the last ones are answered "Not run:
|
|
303
|
+
there is no room left in this conversation for another tool result. Answer now, from what the
|
|
304
|
+
tools returned so far.", and the next request is the answer. This holds for the whole question:
|
|
305
|
+
with Claude answering and Gemini exploring, the explorer's rounds are not shortened either.
|
|
306
|
+
|
|
307
|
+
**Notes stay in the conversation.** After a first reply with nothing in it, the loop asks once
|
|
308
|
+
more with a short note ("Your last reply was empty. …"). A model whose adapter reports a tool call
|
|
309
|
+
it could not form (`gemini()` does) gets another note; Claude never needs that one, because a call
|
|
310
|
+
of Claude's that cannot be read is answered with a tool result that says what was wrong (see
|
|
311
|
+
[Streaming and waits](#streaming-and-waits)). Other models get the note in that one request. When
|
|
312
|
+
Claude makes the tool calls, the note stays in its conversation for the rest of the question,
|
|
313
|
+
because Claude's later thinking follows it; when Claude is the only model, its request for the
|
|
314
|
+
answer has the note too. When Claude only answers, after an explorer of another provider, it never
|
|
315
|
+
gets the explorer's notes. The note of the answer's own second try, sent when the answer comes back
|
|
316
|
+
without text ("Your last reply had no text. …"), is in that one request only, for Claude too: it
|
|
317
|
+
is the last request of the question.
|
|
318
|
+
|
|
319
|
+
**A request too large is recovered only by planning again.** When Claude refuses a request as
|
|
320
|
+
larger than it takes ("prompt is too long: 210417 tokens > 200000 maximum", the error type
|
|
321
|
+
`request_too_large`, or HTTP 413), the loop has nothing it may shorten, and does not send the same
|
|
322
|
+
request again. If no tool call was answered yet and no text was shown, it plans the documents again
|
|
323
|
+
for a smaller window, once a question: the window Claude named, divided by how far Claude's count
|
|
324
|
+
was above the estimate (by 1.25 when it gave no count), or, when Claude named none, 7/10 of the
|
|
325
|
+
window the budget assumed, divided by that ratio when Claude gave a count; a window that comes out
|
|
326
|
+
no smaller than the assumed one becomes 7/10 of it. `steps` then says so: "The first request was
|
|
327
|
+
larger than the model's window of 200,000 tokens: the files were planned again to fit it".
|
|
328
|
+
Otherwise the error is thrown, with `kind: "context_overflow"`, `limitTokens` and
|
|
329
|
+
`requestedTokens`, and `partialResultOf(error)` holds what the question found. [How it works](../how-it-works.md) has the details of both
|
|
330
|
+
recoveries.
|
|
331
|
+
|
|
332
|
+
What this means for your application:
|
|
333
|
+
|
|
334
|
+
- **Set `contextTokens`** to your model's window, and keep `maxTokens` no larger than you need. A
|
|
335
|
+
right budget is what keeps Claude's requests inside its window.
|
|
336
|
+
- **Keep the history in check between questions.** The library shortens no plain turn of the
|
|
337
|
+
history, for any model, and with Claude it shortens no stored tool round either. A long history
|
|
338
|
+
leaves less room for each new question: its documents become cards sooner, fewer results fit,
|
|
339
|
+
and at last its first request is too large. Between two questions the history you pass is yours
|
|
340
|
+
to shorten: it holds no thinking blocks, and the rule above is about the turns of one question.
|
|
341
|
+
Keeping the text of every turn and the `toolTurns` of the last few questions is a simple rule.
|
|
342
|
+
The prompt cache misses once from the first turn you change. See
|
|
343
|
+
[conversations](./conversations.md).
|
|
344
|
+
- **Bound a question** with `limits` (`toolTurns`, `inputTokens`, `requests`): see
|
|
345
|
+
[costs and limits](./costs-and-limits.md).
|
|
346
|
+
|
|
347
|
+
`body` and `betas` pass the API's own features through, context editing among them:
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
351
|
+
|
|
352
|
+
const claude = anthropic({
|
|
353
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
354
|
+
model: "claude-opus-5-5",
|
|
355
|
+
betas: ["context-management-2025-06-27"],
|
|
356
|
+
body: { context_management: { edits: [{ type: "clear_tool_uses_20250919" }] } },
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
With an edit that clears tool uses (`clear_tool_uses_…`) or compacts the conversation
|
|
361
|
+
(`compact_…`) in `body.context_management`, `anthropic()` declares
|
|
362
|
+
`capabilities.clearsToolResults: true`. The API may then have cleared a result before Claude reads
|
|
363
|
+
the request, so the loop answers no repeated call with a pointer to an earlier result ("already
|
|
364
|
+
shown above"): each runs again, and the pointers in stored rounds become stand-ins that say to call
|
|
365
|
+
again ([Conversations](./conversations.md#repeated-calls-in-a-follow-up)). An edit of
|
|
366
|
+
`clear_thinking` alone changes nothing.
|
|
367
|
+
|
|
368
|
+
The loop still counts every result as sent, so its room runs out at the same point as without
|
|
369
|
+
context editing. What clearing does to the bill and the cache is the API's: see Anthropic's
|
|
370
|
+
documentation.
|
|
371
|
+
|
|
372
|
+
## PDF pages and pictures
|
|
373
|
+
|
|
374
|
+
When the library keeps the original of a PDF, the model gets `view_pages` and can look at up to
|
|
375
|
+
five pages a call, as a PDF. [PDFs](./pdfs.md) covers what that needs (`keepOriginals`, the
|
|
376
|
+
optional dependencies) and when it helps.
|
|
377
|
+
|
|
378
|
+
Claude sees a picture, chart or drawing in a PDF only through `view_pages`: the text of a document
|
|
379
|
+
cannot show it. With the optional dependency `@cantoo/pdf-lib`, the text marks each page that
|
|
380
|
+
shows one ("[Picture on this page. This text cannot show it.]"), so Claude knows which pages to
|
|
381
|
+
look at. [PDFs](./pdfs.md) covers the marker, scanned pages and what they need. With Claude:
|
|
382
|
+
|
|
383
|
+
- **Where the pages go** is `toolFilePlacement`. `"inside"` (the default) puts them in the tool
|
|
384
|
+
result, as a `document` block after the result's JSON. `"beside"` puts them after all the tool
|
|
385
|
+
results of the turn, each after a line such as "pages-2-3.pdf (returned by view_pages):".
|
|
386
|
+
`"none"` turns `view_pages` off, for every model of the question.
|
|
387
|
+
- **What a page costs** is `pageTokens`, 3,500 by default: its text and its picture. In the
|
|
388
|
+
library's terms that is divided by `tokenScale`. A question may show pages up to half of
|
|
389
|
+
`reservedTokens`: with the default `maxTokens`, 12 pages when `contextTokens` is not set, and 4
|
|
390
|
+
with a window of 200,000.
|
|
391
|
+
- **Pages stay** in every later request of the question, as base64, because Claude's conversation
|
|
392
|
+
only grows. They count against the request size, which is 10 MB unless you raise the library's
|
|
393
|
+
`budget.maxRequestBytes`. The `toolTurns` of the question say that the pages were shown; they do
|
|
394
|
+
not hold them.
|
|
395
|
+
|
|
396
|
+
```ts
|
|
397
|
+
import { createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
398
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
399
|
+
|
|
400
|
+
const files = createLargeFiles({
|
|
401
|
+
store: new MemoryDocumentStore(),
|
|
402
|
+
keepOriginals: ["pdf"], // view_pages needs the original
|
|
403
|
+
budget: { maxRequestBytes: 32_000_000 }, // Claude's limit, not the library's 10 MB
|
|
404
|
+
});
|
|
405
|
+
const claude = anthropic({ apiKey: process.env.ANTHROPIC_API_KEY!, model: "claude-opus-5-5", toolFilePlacement: "inside" });
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Errors: `AnthropicRequestError`
|
|
409
|
+
|
|
410
|
+
A request Claude did not answer throws `AnthropicRequestError`, a `ModelRequestError` with the
|
|
411
|
+
API's own word for the error in `type`. Its fields, and every case the adapter throws one for:
|
|
412
|
+
[`AnthropicRequestError`](../reference/anthropic.md#anthropicrequesterror). Which errors inside a
|
|
413
|
+
stream are sent again: [A stream cut off](./errors-and-reliability.md#a-stream-cut-off).
|
|
414
|
+
|
|
415
|
+
Two failures of a chat request are not an `AnthropicRequestError`, and have no status or kind: an
|
|
416
|
+
error thrown by your `apiKey`, `authToken` or `headers` function, as it was thrown and not sent
|
|
417
|
+
again; and the abort of your own `signal`, thrown as it came. A part read of a whole-file read
|
|
418
|
+
differs in the first: an error of your `apiKey`, `authToken` or `headers` function becomes an
|
|
419
|
+
`AnthropicRequestError` with `status` 0 and `kind: "unreachable"` ("Claude could not be reached
|
|
420
|
+
(…)."), and the part read is sent again, as after a connection that failed.
|
|
421
|
+
|
|
422
|
+
What else the adapter reads from a reply:
|
|
423
|
+
|
|
424
|
+
- **A body that is not JSON.** A gateway's HTTP 200 whose body is not JSON (an HTML error page) is
|
|
425
|
+
an `AnthropicRequestError` of kind `"invalid"`, status 200, with the `SyntaxError` as `cause`:
|
|
426
|
+
"Claude's reply could not be read: it was not JSON (text/html). A proxy or gateway may have
|
|
427
|
+
answered in its place." It is not sent again, in a chat request or a part read. Until 0.6.0 it
|
|
428
|
+
was the `SyntaxError` itself. An empty body of HTTP 200 is "Claude could not be reached (the
|
|
429
|
+
reply was empty).", status 0, and is sent again.
|
|
430
|
+
- **`x-should-retry: false`.** A request that the API says not to try again is not sent again,
|
|
431
|
+
whatever its status, as Anthropic's own SDKs do.
|
|
432
|
+
- **No credit, or a spending limit reached.** "Your credit balance is too low" (HTTP 400) and the
|
|
433
|
+
spending limits of an organization ("You have reached your API usage limits", or HTTP 429 with
|
|
434
|
+
`enforced_spend_limit_reached`) are of kind `"quota"`, and are not sent again. The 429 was sent
|
|
435
|
+
again three times before 0.6.0.
|
|
436
|
+
- **An error inside a stream** gets the status of its type: `overloaded_error` 529,
|
|
437
|
+
`rate_limit_error` 429, `api_error` 500, `timeout_error` 408, `invalid_request_error` 400,
|
|
438
|
+
`authentication_error` 401, `billing_error` 402, `permission_error` 403, `not_found_error` 404,
|
|
439
|
+
`request_too_large` 413. A type the adapter does not know is the server's failure, 500, and is
|
|
440
|
+
sent again when none of the reply was shown.
|
|
441
|
+
|
|
442
|
+
The API refuses a request that holds half of a character outside the basic plane, such as half of
|
|
443
|
+
an emoji (HTTP 400). Since 0.6.0 the library sends none: a file name, a log template or a search
|
|
444
|
+
window that 0.5.0 or an earlier version stored cut inside an emoji is sent with U+FFFD in place of
|
|
445
|
+
the half, and
|
|
446
|
+
the message to Claude about a call whose input was not valid JSON quotes up to 300 characters of
|
|
447
|
+
it, one fewer when the 300th is the first half of an emoji.
|
|
448
|
+
|
|
449
|
+
```ts
|
|
450
|
+
import { askWithFiles, partialResultOf } from "llm-large-files";
|
|
451
|
+
import { AnthropicRequestError } from "llm-large-files/anthropic";
|
|
452
|
+
|
|
453
|
+
// files, claude and meta as in the first sample.
|
|
454
|
+
try {
|
|
455
|
+
const result = await askWithFiles({ files, model: claude, documentIds: [meta.id], messages: [{ role: "user", text: "Which jobs failed?" }] });
|
|
456
|
+
console.log(result.text);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (error instanceof AnthropicRequestError) {
|
|
459
|
+
// For your logs, not for your users: the message is the API's own.
|
|
460
|
+
console.error(error.status, error.type, error.kind, error.message);
|
|
461
|
+
}
|
|
462
|
+
const partial = partialResultOf(error); // what the question found and paid for before it failed
|
|
463
|
+
if (partial) console.error(`${partial.usage.requests} requests were answered before it failed`);
|
|
464
|
+
throw error;
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
What to show your users for each kind, and how retries and deadlines work, is in
|
|
469
|
+
[errors and reliability](./errors-and-reliability.md).
|
|
470
|
+
|
|
471
|
+
## Conversations kept as Messages API messages
|
|
472
|
+
|
|
473
|
+
If your application keeps its chats as Messages API messages, two functions convert them:
|
|
474
|
+
|
|
475
|
+
- **`fromAnthropicMessages(history)`** gives the `messages` of `askWithFiles`. Text blocks become
|
|
476
|
+
text. A `tool_use` block and the `tool_result` that answers it become a stored tool round, with
|
|
477
|
+
the result read back from its JSON. A call with no result kept is answered
|
|
478
|
+
`{ error: "No result was kept for this call." }`. Every other block (an image, a document) is
|
|
479
|
+
kept as a native part, which Claude reads. Thinking blocks are left out. `system` is not part of
|
|
480
|
+
the history: pass it to `askWithFiles` as `system`.
|
|
481
|
+
- **`anthropicToolTurns(result.toolTurns)`** gives the tool rounds of a question as Messages API
|
|
482
|
+
messages: for each round, an assistant message of `tool_use` blocks and a user message of
|
|
483
|
+
`tool_result` blocks (`is_error: true` for a result with an `error`). Put them between the
|
|
484
|
+
question and its answer.
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
import { askWithFiles } from "llm-large-files";
|
|
488
|
+
import { anthropicToolTurns, fromAnthropicMessages, type AnthropicMessage } from "llm-large-files/anthropic";
|
|
489
|
+
|
|
490
|
+
// files, claude and meta as in the first sample. `history` is what your application keeps for the chat.
|
|
491
|
+
const history: AnthropicMessage[] = [];
|
|
492
|
+
|
|
493
|
+
async function ask(question: string): Promise<string> {
|
|
494
|
+
const result = await askWithFiles({
|
|
495
|
+
files,
|
|
496
|
+
model: claude,
|
|
497
|
+
documentIds: [meta.id],
|
|
498
|
+
messages: fromAnthropicMessages([...history, { role: "user", content: question }]),
|
|
499
|
+
});
|
|
500
|
+
// Convert the tool rounds once, and keep what you got: a call without an ID gets a new random one each time.
|
|
501
|
+
history.push({ role: "user", content: question }, ...anthropicToolTurns(result.toolTurns));
|
|
502
|
+
if (result.answered) history.push({ role: "assistant", content: result.text });
|
|
503
|
+
return result.text;
|
|
504
|
+
}
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
The IDs of the calls:
|
|
508
|
+
|
|
509
|
+
- **Since 0.6.0, `anthropicToolTurns` gives a call that came without an ID a random one**,
|
|
510
|
+
`toolu_` and 24 hex digits, the same in its result. Before, it numbered them `toolu_1_1`,
|
|
511
|
+
`toolu_1_2`, … each time, so the stored rounds of two questions had the same IDs, which the
|
|
512
|
+
Messages API refuses. If your code expected IDs of that form, make it accept any ID.
|
|
513
|
+
- **IDs of other providers are made valid.** The API takes only the characters `A-Z`, `a-z`, `0-9`,
|
|
514
|
+
`_` and `-`, and each ID once in a request. Others become `_` (Kimi's
|
|
515
|
+
`functions.read_document:0` is sent as `functions_read_document_0`), and an ID used twice gets a
|
|
516
|
+
suffix (`toolu_1_1_2`). `anthropicToolTurns` does this, and so does the adapter for every request,
|
|
517
|
+
so rounds stored from another provider's model can go to Claude as they are. Claude's own turns
|
|
518
|
+
keep their IDs.
|
|
519
|
+
|
|
520
|
+
A native part goes to every model of a question as it is: with an explorer of another provider, a
|
|
521
|
+
block in Claude's form goes to that model too. Keeping toolTurns, history across servers and
|
|
522
|
+
mixing providers are in [conversations](./conversations.md).
|
|
523
|
+
|
|
524
|
+
## Claude answering, Gemini exploring
|
|
525
|
+
|
|
526
|
+
With an explorer, a second model makes the tool calls and reads whole files in parts, and Claude
|
|
527
|
+
writes the answer from what the tools returned. A cheap Gemini model on Vertex AI can explore:
|
|
528
|
+
|
|
529
|
+
```ts
|
|
530
|
+
import { GoogleAuth } from "google-auth-library";
|
|
531
|
+
import { askWithFiles } from "llm-large-files";
|
|
532
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
533
|
+
import { gemini, vertexAi } from "llm-large-files/gemini";
|
|
534
|
+
|
|
535
|
+
// files and meta as in the first sample.
|
|
536
|
+
const auth = new GoogleAuth({ scopes: "https://www.googleapis.com/auth/cloud-platform" });
|
|
537
|
+
|
|
538
|
+
const explorer = gemini(vertexAi({
|
|
539
|
+
project: process.env.GOOGLE_CLOUD_PROJECT!,
|
|
540
|
+
model: "gemini-3.5-flash-lite",
|
|
541
|
+
accessToken: async () => (await auth.getAccessToken())!,
|
|
542
|
+
}));
|
|
543
|
+
const claude = anthropic({
|
|
544
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
545
|
+
model: "claude-opus-5-5",
|
|
546
|
+
contextTokens: 1_000_000,
|
|
547
|
+
fallbacks: "default",
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
const result = await askWithFiles({
|
|
551
|
+
files,
|
|
552
|
+
model: claude, // writes the answer
|
|
553
|
+
explorer, // searches, reads, looks at pages, reads whole files in parts
|
|
554
|
+
documentIds: [meta.id],
|
|
555
|
+
messages: [{ role: "user", text: "Which jobs failed last night, and why?" }],
|
|
556
|
+
onText: (delta) => process.stdout.write(delta),
|
|
557
|
+
});
|
|
558
|
+
console.log(result.usage.byModel); // "claude-opus-5-5", and "gemini-3.5-flash-lite" when the explorer ran
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
What the pair changes:
|
|
562
|
+
|
|
563
|
+
- **The explorer works only when there is something to explore**: when every document fits whole
|
|
564
|
+
and no PDF pages can be looked at, Claude answers in one request and Gemini is not called.
|
|
565
|
+
- **The budget is planned in Claude's tokens.** Each adapter has its own `tokenScale`: Claude 1.35,
|
|
566
|
+
Gemini 1 (`gemini()` declares none). A question uses the largest, because both models get the
|
|
567
|
+
same conversation. The window is the smallest one declared (or the library's), `maxTokens` is
|
|
568
|
+
kept free, and a page costs Claude's 3,500. Give the Gemini adapter its own `contextTokens` when
|
|
569
|
+
its window is the smaller one.
|
|
570
|
+
- **Each count is read by its own model's scale.** The count Gemini reports with a reply is read by
|
|
571
|
+
Gemini's scale, Claude's by Claude's, so the estimate rises when either model counted more than
|
|
572
|
+
its scale says. Before 0.6.0 every count was read by the largest scale, and a Gemini explorer's
|
|
573
|
+
could not raise it.
|
|
574
|
+
- **No result is shortened.** Claude is append-only, so the rule
|
|
575
|
+
[above](#a-conversation-that-may-only-grow) holds for the explorer's rounds too.
|
|
576
|
+
- **Claude gets the calls and what they returned**, in its own form: an assistant turn of
|
|
577
|
+
`tool_use` blocks and a user turn of `tool_result` blocks. A call that came without an ID is
|
|
578
|
+
given one (`toolu_1_1`: round 1, call 1), the same in its result. Claude does not get the
|
|
579
|
+
explorer's thoughts, its words or its notes.
|
|
580
|
+
- **The answer is one request**, with the tools listed and `tool_choice: { type: "none" }`, and a
|
|
581
|
+
line after the last results that asks for the answer: `FINAL_ANSWER_REQUEST`, or
|
|
582
|
+
`FINAL_ANSWER_AFTER_PAGES` when pages were the only thing to look at, or
|
|
583
|
+
`FINAL_ANSWER_WITHOUT_TOOLS` when no tool ran.
|
|
584
|
+
- **Pages** are offered when both models can take them: `gemini()` and `anthropic()` both can by
|
|
585
|
+
default.
|
|
586
|
+
|
|
587
|
+
Claude can explore too (`explorer: anthropic(…)`): its notes then stay in its conversation, and its
|
|
588
|
+
requests stream though nothing it writes is shown. Who does what, the cost, and any other pair of
|
|
589
|
+
providers are in [explorer model](./explorer-model.md); the Vertex AI setup is in
|
|
590
|
+
[Vertex AI and Gemini](./vertex-and-gemini.md).
|