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.
Files changed (236) hide show
  1. package/CHANGELOG.md +1400 -338
  2. package/README.md +217 -49
  3. package/dist/analyze.d.ts +5 -0
  4. package/dist/analyze.d.ts.map +1 -1
  5. package/dist/analyze.js +96 -47
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/anthropic/adapter.d.ts +117 -0
  8. package/dist/anthropic/adapter.d.ts.map +1 -0
  9. package/dist/anthropic/adapter.js +542 -0
  10. package/dist/anthropic/adapter.js.map +1 -0
  11. package/dist/anthropic/index.d.ts +3 -0
  12. package/dist/anthropic/index.d.ts.map +1 -0
  13. package/dist/anthropic/index.js +2 -0
  14. package/dist/anthropic/index.js.map +1 -0
  15. package/dist/ask.d.ts +200 -0
  16. package/dist/ask.d.ts.map +1 -0
  17. package/dist/ask.js +1402 -0
  18. package/dist/ask.js.map +1 -0
  19. package/dist/budget.d.ts +23 -0
  20. package/dist/budget.d.ts.map +1 -0
  21. package/dist/budget.js +64 -0
  22. package/dist/budget.js.map +1 -0
  23. package/dist/encoding.d.ts +17 -0
  24. package/dist/encoding.d.ts.map +1 -0
  25. package/dist/encoding.js +852 -0
  26. package/dist/encoding.js.map +1 -0
  27. package/dist/errors.d.ts +4 -0
  28. package/dist/errors.d.ts.map +1 -1
  29. package/dist/errors.js +15 -0
  30. package/dist/errors.js.map +1 -1
  31. package/dist/events.d.ts +333 -0
  32. package/dist/events.d.ts.map +1 -0
  33. package/dist/events.js +159 -0
  34. package/dist/events.js.map +1 -0
  35. package/dist/evidence.d.ts +42 -0
  36. package/dist/evidence.d.ts.map +1 -0
  37. package/dist/evidence.js +138 -0
  38. package/dist/evidence.js.map +1 -0
  39. package/dist/extract.d.ts +35 -3
  40. package/dist/extract.d.ts.map +1 -1
  41. package/dist/extract.js +626 -58
  42. package/dist/extract.js.map +1 -1
  43. package/dist/gemini/adapter.d.ts +59 -0
  44. package/dist/gemini/adapter.d.ts.map +1 -0
  45. package/dist/gemini/adapter.js +226 -0
  46. package/dist/gemini/adapter.js.map +1 -0
  47. package/dist/gemini/chat.d.ts +53 -77
  48. package/dist/gemini/chat.d.ts.map +1 -1
  49. package/dist/gemini/chat.js +42 -300
  50. package/dist/gemini/chat.js.map +1 -1
  51. package/dist/gemini/errors.d.ts +16 -0
  52. package/dist/gemini/errors.d.ts.map +1 -0
  53. package/dist/gemini/errors.js +46 -0
  54. package/dist/gemini/errors.js.map +1 -0
  55. package/dist/gemini/generate.d.ts +18 -21
  56. package/dist/gemini/generate.d.ts.map +1 -1
  57. package/dist/gemini/generate.js +49 -92
  58. package/dist/gemini/generate.js.map +1 -1
  59. package/dist/gemini/index.d.ts +6 -4
  60. package/dist/gemini/index.d.ts.map +1 -1
  61. package/dist/gemini/index.js +4 -3
  62. package/dist/gemini/index.js.map +1 -1
  63. package/dist/gemini/sse.d.ts +1 -2
  64. package/dist/gemini/sse.d.ts.map +1 -1
  65. package/dist/gemini/sse.js +1 -38
  66. package/dist/gemini/sse.js.map +1 -1
  67. package/dist/http.d.ts +136 -0
  68. package/dist/http.d.ts.map +1 -0
  69. package/dist/http.js +401 -0
  70. package/dist/http.js.map +1 -0
  71. package/dist/index.d.ts +18 -2
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +13 -4
  74. package/dist/index.js.map +1 -1
  75. package/dist/library.d.ts +96 -8
  76. package/dist/library.d.ts.map +1 -1
  77. package/dist/library.js +414 -96
  78. package/dist/library.js.map +1 -1
  79. package/dist/map-reduce.d.ts +37 -1
  80. package/dist/map-reduce.d.ts.map +1 -1
  81. package/dist/map-reduce.js +255 -61
  82. package/dist/map-reduce.js.map +1 -1
  83. package/dist/model.d.ts +278 -0
  84. package/dist/model.d.ts.map +1 -0
  85. package/dist/model.js +222 -0
  86. package/dist/model.js.map +1 -0
  87. package/dist/node/handlers.d.ts +20 -4
  88. package/dist/node/handlers.d.ts.map +1 -1
  89. package/dist/node/handlers.js +122 -20
  90. package/dist/node/handlers.js.map +1 -1
  91. package/dist/node/index.d.ts +2 -0
  92. package/dist/node/index.d.ts.map +1 -1
  93. package/dist/node/index.js +1 -0
  94. package/dist/node/index.js.map +1 -1
  95. package/dist/node/sse.d.ts +59 -0
  96. package/dist/node/sse.d.ts.map +1 -0
  97. package/dist/node/sse.js +174 -0
  98. package/dist/node/sse.js.map +1 -0
  99. package/dist/openai/adapter.d.ts +109 -0
  100. package/dist/openai/adapter.d.ts.map +1 -0
  101. package/dist/openai/adapter.js +567 -0
  102. package/dist/openai/adapter.js.map +1 -0
  103. package/dist/openai/index.d.ts +3 -0
  104. package/dist/openai/index.d.ts.map +1 -0
  105. package/dist/openai/index.js +2 -0
  106. package/dist/openai/index.js.map +1 -0
  107. package/dist/output-limit.d.ts +42 -0
  108. package/dist/output-limit.d.ts.map +1 -0
  109. package/dist/output-limit.js +121 -0
  110. package/dist/output-limit.js.map +1 -0
  111. package/dist/pause.d.ts +24 -0
  112. package/dist/pause.d.ts.map +1 -0
  113. package/dist/pause.js +38 -0
  114. package/dist/pause.js.map +1 -0
  115. package/dist/pdf-pages.d.ts +32 -2
  116. package/dist/pdf-pages.d.ts.map +1 -1
  117. package/dist/pdf-pages.js +275 -14
  118. package/dist/pdf-pages.js.map +1 -1
  119. package/dist/pdf-pictures.d.ts +30 -3
  120. package/dist/pdf-pictures.d.ts.map +1 -1
  121. package/dist/pdf-pictures.js +50 -8
  122. package/dist/pdf-pictures.js.map +1 -1
  123. package/dist/pdf-work.d.ts +40 -0
  124. package/dist/pdf-work.d.ts.map +1 -0
  125. package/dist/pdf-work.js +734 -0
  126. package/dist/pdf-work.js.map +1 -0
  127. package/dist/plan.d.ts +22 -4
  128. package/dist/plan.d.ts.map +1 -1
  129. package/dist/plan.js +61 -12
  130. package/dist/plan.js.map +1 -1
  131. package/dist/retry.d.ts +76 -0
  132. package/dist/retry.d.ts.map +1 -0
  133. package/dist/retry.js +141 -0
  134. package/dist/retry.js.map +1 -0
  135. package/dist/search.d.ts +8 -2
  136. package/dist/search.d.ts.map +1 -1
  137. package/dist/search.js +60 -10
  138. package/dist/search.js.map +1 -1
  139. package/dist/sse.d.ts +36 -0
  140. package/dist/sse.d.ts.map +1 -0
  141. package/dist/sse.js +153 -0
  142. package/dist/sse.js.map +1 -0
  143. package/dist/stores.d.ts.map +1 -1
  144. package/dist/stores.js +5 -2
  145. package/dist/stores.js.map +1 -1
  146. package/dist/text-index.d.ts +6 -0
  147. package/dist/text-index.d.ts.map +1 -1
  148. package/dist/text-index.js +25 -8
  149. package/dist/text-index.js.map +1 -1
  150. package/dist/text.d.ts +62 -2
  151. package/dist/text.d.ts.map +1 -1
  152. package/dist/text.js +314 -14
  153. package/dist/text.js.map +1 -1
  154. package/dist/tools.d.ts +72 -1
  155. package/dist/tools.d.ts.map +1 -1
  156. package/dist/tools.js +341 -60
  157. package/dist/tools.js.map +1 -1
  158. package/dist/types.d.ts +128 -4
  159. package/dist/types.d.ts.map +1 -1
  160. package/dist/zip-directory.d.ts +43 -0
  161. package/dist/zip-directory.d.ts.map +1 -0
  162. package/dist/zip-directory.js +130 -0
  163. package/dist/zip-directory.js.map +1 -0
  164. package/docs/README.md +288 -0
  165. package/docs/api.md +58 -184
  166. package/docs/configuration.md +34 -490
  167. package/docs/getting-started.md +412 -177
  168. package/docs/guides/anthropic.md +590 -0
  169. package/docs/guides/conversations.md +629 -0
  170. package/docs/guides/costs-and-limits.md +726 -0
  171. package/docs/guides/deployment.md +654 -0
  172. package/docs/guides/errors-and-reliability.md +735 -0
  173. package/docs/guides/explorer-model.md +399 -0
  174. package/docs/guides/openai-compatible.md +602 -0
  175. package/docs/guides/own-adapter-and-loop.md +1169 -0
  176. package/docs/guides/pdfs.md +971 -0
  177. package/docs/guides/storage.md +731 -0
  178. package/docs/guides/testing.md +1199 -0
  179. package/docs/guides/vertex-and-gemini.md +717 -0
  180. package/docs/guides/web-app.md +1589 -0
  181. package/docs/how-it-works.md +1022 -138
  182. package/docs/measurements.md +881 -0
  183. package/docs/publishing.md +274 -46
  184. package/docs/reference/anthropic.md +427 -0
  185. package/docs/reference/ask.md +892 -0
  186. package/docs/reference/errors-and-retries.md +758 -0
  187. package/docs/reference/events.md +993 -0
  188. package/docs/reference/gemini.md +823 -0
  189. package/docs/reference/library.md +1467 -0
  190. package/docs/reference/node.md +528 -0
  191. package/docs/reference/openai.md +385 -0
  192. package/docs/reference/tools.md +883 -0
  193. package/docs/security.md +902 -90
  194. package/docs/troubleshooting.md +1390 -0
  195. package/package.json +22 -4
  196. package/src/analyze.ts +93 -42
  197. package/src/anthropic/adapter.ts +611 -0
  198. package/src/anthropic/index.ts +2 -0
  199. package/src/ask.ts +1560 -0
  200. package/src/budget.ts +71 -0
  201. package/src/encoding.ts +850 -0
  202. package/src/errors.ts +17 -0
  203. package/src/events.ts +442 -0
  204. package/src/evidence.ts +165 -0
  205. package/src/extract.ts +602 -62
  206. package/src/gemini/adapter.ts +271 -0
  207. package/src/gemini/chat.ts +87 -355
  208. package/src/gemini/errors.ts +48 -0
  209. package/src/gemini/generate.ts +67 -94
  210. package/src/gemini/index.ts +6 -4
  211. package/src/gemini/sse.ts +1 -34
  212. package/src/http.ts +434 -0
  213. package/src/index.ts +55 -4
  214. package/src/library.ts +497 -97
  215. package/src/map-reduce.ts +266 -59
  216. package/src/model.ts +433 -0
  217. package/src/node/handlers.ts +137 -22
  218. package/src/node/index.ts +2 -0
  219. package/src/node/sse.ts +198 -0
  220. package/src/openai/adapter.ts +613 -0
  221. package/src/openai/index.ts +2 -0
  222. package/src/output-limit.ts +135 -0
  223. package/src/pause.ts +41 -0
  224. package/src/pdf-pages.ts +268 -15
  225. package/src/pdf-pictures.ts +56 -9
  226. package/src/pdf-work.ts +722 -0
  227. package/src/plan.ts +68 -12
  228. package/src/retry.ts +176 -0
  229. package/src/search.ts +63 -10
  230. package/src/sse.ts +159 -0
  231. package/src/stores.ts +5 -2
  232. package/src/text-index.ts +26 -7
  233. package/src/text.ts +323 -13
  234. package/src/tools.ts +338 -62
  235. package/src/types.ts +129 -5
  236. package/src/zip-directory.ts +136 -0
@@ -0,0 +1,399 @@
1
+ # An explorer model
2
+
3
+ This guide is for you when questions about large files take many searches and reads, and you want
4
+ a cheaper or faster model to do them while the model you chose writes the answer. It shows how to
5
+ set that second model with `askGemini` or `askWithFiles`, what each model gets, what changes for
6
+ your application, and when a second model pays off.
7
+
8
+ ## Who does what
9
+
10
+ The model is yours to choose: the package has no default. One model does both halves of the work
11
+ on a large file: it explores the file (searches, reads, looks at pages), and it answers. An
12
+ explorer is a second model for the first half. The model you chose, `model`, then writes the
13
+ answer from what the explorer's tool calls returned.
14
+
15
+ | Work | The explorer | `model` |
16
+ |---|---|---|
17
+ | Requests with tools: `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`, `view_pages` | All of them | None |
18
+ | The part reads of `analyze_whole_document` | All of them | None |
19
+ | A second try after an empty or broken turn, with its note | Gets it | Never sees the explorer's |
20
+ | The answer | None | One request, in which no tool can be called, and a second try with its own note when that request comes back without text |
21
+ | A question whose documents are all sent whole, with no pages to look at | Not called | One request, as without an explorer |
22
+
23
+ The explorer is called only when the question needs tools: a document is too large to send whole,
24
+ or a PDF has pages the model may look at (`view_pages`). When every document goes into the prompt
25
+ whole and there are no pages to look at, `model` gets one request, exactly as without an explorer.
26
+ The tools themselves are described in the [tools reference](../reference/tools.md).
27
+
28
+ ## Set an explorer with `askGemini`
29
+
30
+ `askGemini` is `askWithFiles` with a `gemini()` model for the endpoint, and a second one for the
31
+ explorer when the endpoint names one. `geminiApi()` and `vertexAi()` take `explorerModel` next to
32
+ `model`. On Vertex AI:
33
+
34
+ ```ts
35
+ import { GoogleAuth } from "google-auth-library";
36
+ import { askGemini, vertexAi } from "llm-large-files/gemini";
37
+
38
+ const auth = new GoogleAuth({ scopes: "https://www.googleapis.com/auth/cloud-platform" });
39
+
40
+ const endpoint = vertexAi({
41
+ project: process.env.GOOGLE_CLOUD_PROJECT!,
42
+ location: process.env.GOOGLE_CLOUD_LOCATION ?? "global",
43
+ model: "gemini-3.8-flash", // writes the answer
44
+ explorerModel: process.env.GEMINI_EXPLORER_MODEL, // searches and reads; unset or empty: no explorer
45
+ accessToken: async () => (await auth.getAccessToken())!,
46
+ });
47
+
48
+ // files is your library (createLargeFiles); documentIds and messages come from the request.
49
+ const result = await askGemini({
50
+ files,
51
+ endpoint,
52
+ documentIds,
53
+ messages,
54
+ generationConfig: { thinkingConfig: { thinkingLevel: "high" } }, // for `model`
55
+ explorerGenerationConfig: { thinkingConfig: { thinkingLevel: "low" } }, // for the explorer
56
+ onText: (delta) => process.stdout.write(delta),
57
+ });
58
+ ```
59
+
60
+ `geminiApi({ apiKey, model, explorerModel })` takes it the same way. The name is trimmed. Left out,
61
+ empty, or the same as `model`, there is no explorer, and `askGemini` sends exactly the requests it
62
+ sends without one. So a setting that may be unset can be passed as it is.
63
+
64
+ `explorerModel` sets the endpoint's `explorerUrl`: the `...:generateContent` URL of the explorer,
65
+ next to the model's own `url` and `streamUrl`. With URLs of your own, for a gateway, set
66
+ `explorerUrl` yourself. An `explorerUrl` that is the same as `url` is no explorer.
67
+
68
+ ```ts
69
+ import type { GeminiEndpoint } from "llm-large-files/gemini";
70
+
71
+ const gateway: GeminiEndpoint = {
72
+ url: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:generateContent",
73
+ streamUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:streamGenerateContent?alt=sse",
74
+ explorerUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.5-flash-lite:generateContent",
75
+ headers: { "Content-Type": "application/json", "x-api-key": process.env.GATEWAY_API_KEY! },
76
+ };
77
+ ```
78
+
79
+ What `askGemini` gives each model:
80
+
81
+ - **The same headers.** The explorer is called with the endpoint's `headers`, so it uses the same
82
+ API key or access token as `model`. `vertexAi()` builds the explorer's URL from the same
83
+ `project` and `location`, so a `vertexAi()` explorer is in the same project and location too.
84
+ Two models that need different headers (one on the Gemini API with a key, the other on Vertex AI
85
+ with a token) cannot be paired this way: use `askWithFiles`
86
+ ([below](#set-an-explorer-with-askwithfiles)).
87
+ - **Their own `generationConfig`.** `generationConfig` goes to `model`. `explorerGenerationConfig`
88
+ goes to the explorer and defaults to `generationConfig`. Set it when the two models do not take
89
+ the same settings: a `thinkingLevel` that a Gemini 2.5 model refuses with HTTP 400, for example
90
+ (Gemini 2.5 takes `thinkingBudget`). It is not used without an explorer.
91
+ - **One value of the other settings for both.** `toolFilePlacement`, `analysisConfig`,
92
+ `firstByteTimeoutMs`, `idleTimeoutMs` and `fetch` apply to both models. `analysisConfig` is the
93
+ `generationConfig` of the part reads of a whole-file read, which the explorer makes: write it for
94
+ the explorer. `toolFilePlacement: "inside"` is read by Gemini 3 models; if either model is
95
+ Gemini 2.5, use `"beside"`, which both read (see [PDFs](./pdfs.md)).
96
+ - **The defaults of `gemini()`.** `askGemini` has no option for `contextTokens`,
97
+ `maxRequestBytes`, `pageTokens` or `id`, so both of its models keep the defaults of `gemini()`:
98
+ no window and no request size declared, 530 tokens a page, and the model's name from its URL in
99
+ `usage.byModel`. Set the window and the request size of the smaller of the two models in the
100
+ library's `budget` (or in `budget` of the question). See [Costs and limits](./costs-and-limits.md).
101
+ - **No streaming for the explorer.** Its requests go to `explorerUrl` in one piece. The answer
102
+ streams from `streamUrl` when you pass `onText`.
103
+
104
+ The options of `askGemini`, `geminiApi()` and `vertexAi()` are in the
105
+ [Gemini reference](../reference/gemini.md). Production setup for Vertex AI (access tokens,
106
+ locations, quotas) is in [Gemini and Vertex AI](./vertex-and-gemini.md).
107
+
108
+ ## Set an explorer with `askWithFiles`
109
+
110
+ `askWithFiles` takes `explorer`: any `ModelAdapter`, of any provider. Each model is an adapter with
111
+ its own settings, so the two can differ in everything: provider, API, project, location,
112
+ `generationConfig`, where pages go, the window they declare, and their name in the usage totals.
113
+ The options of the question (`limits`, `retry`, `budget`, `signal`) apply to both.
114
+
115
+ ```ts
116
+ import { GoogleAuth } from "google-auth-library";
117
+ import { askWithFiles } from "llm-large-files";
118
+ import { gemini, vertexAi } from "llm-large-files/gemini";
119
+
120
+ const auth = new GoogleAuth({ scopes: "https://www.googleapis.com/auth/cloud-platform" });
121
+ const onVertex = (model: string) => vertexAi({
122
+ project: process.env.GOOGLE_CLOUD_PROJECT!,
123
+ location: "global",
124
+ model,
125
+ accessToken: async () => (await auth.getAccessToken())!,
126
+ });
127
+
128
+ const model = gemini(onVertex("gemini-3.8-flash"), {
129
+ generationConfig: { thinkingConfig: { thinkingLevel: "high" } },
130
+ contextTokens: 1_048_576, // its input window: no question is planned for more (the library's default, 1,000,000, is smaller here)
131
+ });
132
+ const explorer = gemini(onVertex("gemini-2.5-flash-lite"), {
133
+ generationConfig: { thinkingConfig: { thinkingBudget: 0 } },
134
+ toolFilePlacement: "beside", // Gemini 2.5 does not see a file inside a function response
135
+ contextTokens: 1_048_576,
136
+ });
137
+
138
+ // files, documentIds and messages as in the sample above.
139
+ const result = await askWithFiles({
140
+ files, model, explorer, documentIds, messages,
141
+ onText: (delta) => process.stdout.write(delta),
142
+ });
143
+ ```
144
+
145
+ Here each model gets pages its own way: `"beside"` for the Gemini 2.5 explorer, `"inside"` (the
146
+ default) for the Gemini 3 model that answers. With `askGemini`, one placement serves both.
147
+
148
+ The explorer can be of another provider. A model on your own vLLM server, through the
149
+ OpenAI-compatible adapter, explores while Gemini on Vertex AI answers:
150
+
151
+ ```ts
152
+ import { openAiCompatible } from "llm-large-files/openai";
153
+
154
+ // askWithFiles, files, documentIds, messages and `model` (the Gemini adapter) are those of the sample above.
155
+ const local = openAiCompatible({
156
+ baseUrl: process.env.VLLM_BASE_URL!, // for example http://vllm.internal:8000/v1
157
+ model: process.env.VLLM_MODEL!,
158
+ contextTokens: 131_072, // the --max-model-len the server runs with
159
+ });
160
+
161
+ const answer = await askWithFiles({ files, model, explorer: local, documentIds, messages });
162
+ ```
163
+
164
+ The question then runs inside the smaller window, 131,072 tokens, for both models. And this
165
+ explorer is sent no PDF (`pdfInput` is off by default), so `view_pages` is not offered to either
166
+ model: see [the budget, token scale and pages](#the-budget-token-scale-and-pages). The roles can
167
+ be the other way round, and any adapter can take either: `model: anthropic({ ... })` with
168
+ `explorer: gemini(...)` works the same. Recipes for each provider are in
169
+ [OpenAI-compatible endpoints](./openai-compatible.md) and [Claude](./anthropic.md).
170
+
171
+ Three things to know:
172
+
173
+ - **The same adapter is no explorer.** `explorer: model`, the same object, is the same as leaving
174
+ `explorer` out. Two adapters for the same model are two models.
175
+ - **`gemini()` ignores `explorerUrl`.** An endpoint built with `explorerModel` and passed to
176
+ `gemini()` gives one model. Make the explorer an adapter of its own, as above.
177
+ - **Whole-file reads need the explorer's `generateText`.** The part reads are the explorer's, so
178
+ `analyze_whole_document` is offered only when the explorer's adapter has `generateText`. All
179
+ three adapters of the package have it; an adapter of your own may not (see
180
+ [Your own adapter and loop](./own-adapter-and-loop.md)).
181
+
182
+ `AskOptions` and the adapter contract are in the [ask reference](../reference/ask.md).
183
+
184
+ ## What the answering model gets
185
+
186
+ The request for the answer holds:
187
+
188
+ - **Your system instruction and the tool guide**, without the explorer's line
189
+ (`EXPLORER_INSTRUCTION`, [below](#the-explorers-part-done)).
190
+ - **The conversation**, with the documents' text and cards where `documentPlacement` puts them, as
191
+ every request of the question has them.
192
+ - **Each tool round of the explorer, as its calls and their results**, in the answering model's
193
+ own provider form. Pages that the explorer looked at are part of the results, so the answering
194
+ model is shown them too, placed the way its own adapter places files.
195
+ - **A user message at the end** that asks for the answer:
196
+
197
+ | When | The message |
198
+ |---|---|
199
+ | No tool call was made: the explorer replied `DONE` at once, its replies stayed empty or its calls could not be read, or `limits` left no turn for tools | `FINAL_ANSWER_WITHOUT_TOOLS` |
200
+ | Otherwise, when a document is searchable (too large to send whole) | `FINAL_ANSWER_REQUEST` |
201
+ | Otherwise: the only tool was `view_pages`, and every text is in the prompt | `FINAL_ANSWER_AFTER_PAGES` |
202
+
203
+ The tools are listed in that request with a tool choice of "none", so no tool can be called. Each
204
+ adapter says it in its own way: `gemini()` leaves the tools out, `anthropic()` sends
205
+ `tool_choice: { type: "none" }`, and `openAiCompatible()` sends `tool_choice: "none"`, or leaves
206
+ the tools out with `answerWithoutTools: "omit"`.
207
+
208
+ The request does not hold the explorer's thoughts, its thought signatures, anything it wrote next
209
+ to its calls, its `DONE`, or the notes it was sent. A thought signature belongs to the model that
210
+ wrote it, and what the explorer made of the files is not what the files say: the answer is written
211
+ from the tool results. So the answering model can use only what the explorer fetched. It has no
212
+ tools to fetch more. What each call showed is in `result.evidence`, as it is with one model.
213
+
214
+ ## The explorer's part: DONE
215
+
216
+ Every chat request to the explorer (its tool turns and second tries, not the part reads of a
217
+ whole-file read) has one paragraph added at the end of its system instruction,
218
+ `EXPLORER_INSTRUCTION`, exported from `llm-large-files` and `llm-large-files/gemini`. It tells the
219
+ explorer that another model writes the answer from what its tool calls returned, and to reply with
220
+ the single word DONE when the results hold what the answer needs
221
+ ([the text](../reference/ask.md#explorer_instruction)).
222
+
223
+ Its own turns go back to the explorer unchanged, thought signatures included, so a Gemini 3
224
+ explorer keeps its signatures across its tool rounds.
225
+
226
+ A reply of the explorer that calls no tool ends the exploring, whatever it says, unless it is an
227
+ empty or broken turn, which is asked for once more
228
+ ([below](#notes-after-an-empty-or-broken-turn)). `DONE` is what it is asked for. A model that
229
+ writes an answer instead is not an error: its text is dropped, not shown and not sent to the
230
+ answering model, but you pay for its output tokens. The request for the answer follows in either
231
+ case.
232
+
233
+ Two cases end the question without an answer request:
234
+
235
+ - **The explorer's prompt was blocked.** A blocked prompt would be blocked again: the question ends
236
+ with `answered: false` and `blockReason`.
237
+ - **The explorer's request failed.** Its error reaches you after the retries, as an error of
238
+ `model` does (see [below](#what-changes-for-your-application)).
239
+
240
+ An explorer that never stops calling tools is stopped by `limits.toolTurns`, or earlier by the
241
+ ceilings of `limits` or the room left in the request. The request for the answer is still made,
242
+ from what the tools returned so far.
243
+
244
+ ## Notes after an empty or broken turn
245
+
246
+ The loop asks the model that explores once more, with a short note, after one of two turns:
247
+
248
+ | Turn | The note to an explorer |
249
+ |---|---|
250
+ | A tool call that could not be read (`finish: "malformed_call"`: Gemini's `MALFORMED_FUNCTION_CALL` or `UNEXPECTED_TOOL_CALL`). `anthropic()` and `openAiCompatible()` answer such a call in the tool round with an error instead, and send no note. | "Your last reply tried to call a tool, but the call could not be read. Call the tools again, with arguments that match their declarations, or reply DONE if the results above already hold what the answer needs." |
251
+ | A first reply with nothing in it: no text and no call, before any tool call of the question, and not cut off or filtered | "Your last reply was empty. Call the tools to gather what the answer needs from the files, then reply DONE." |
252
+
253
+ Without an explorer, the notes ask the model to call the tools or to answer, instead of replying
254
+ `DONE`.
255
+
256
+ - When a second try is made, what it costs and what `steps` says:
257
+ [Errors and reliability](./errors-and-reliability.md#empty-or-broken-turns).
258
+ - An explorer whose adapter declares `appendOnly` (Claude) keeps the note in its own conversation,
259
+ because Claude ties what it thinks next to the turns before it.
260
+ - **With an explorer, the answering model never gets the explorer's notes**, whichever adapter
261
+ explores or answers. It gets a note of its own only when its answer comes back without text,
262
+ empty or with a tool call: the request for the answer is then made once more, with "Your last
263
+ reply had no text. No tool can be called in this reply: write the answer now, in text, from what
264
+ is above." at its end, and the note is not kept. That try is one of the two of the question, and
265
+ it can be one request past `limits.toolTurns`.
266
+
267
+ ## Limits and usage across both models
268
+
269
+ `limits` of the question counts the work of both models together. The table of every limit is in
270
+ the [ask reference](../reference/ask.md); what each costs is in
271
+ [Costs and limits](./costs-and-limits.md).
272
+
273
+ - **`limits.toolTurns`** (12 by default) counts every chat request: the explorer's, its second
274
+ tries, and the answer. The second try of the answer itself, when the answering model wrote no
275
+ text, can be one past it. With an explorer the answer is always a request of its own, so the
276
+ explorer has at most `toolTurns − 1` requests. With `toolTurns: 1` there is no exploring: the one
277
+ request is the answer, from what is in the prompt. `maxToolTurns` of `askGemini` is the same
278
+ limit under its name from before 0.5.0.
279
+ - **`limits.callsPerTurn`** and **`limits.wholeDocumentReads`** apply to the explorer's turns, as
280
+ they do to one model's.
281
+ - **`limits.inputTokens`** and **`limits.requests`** are ceilings on the totals of the question:
282
+ both models' chat requests and the explorer's part reads together. Once one is reached (for
283
+ `requests`, once one request is left), no more tools run and the next request is the answer.
284
+
285
+ `result.usage` has the totals of the question and an entry for each model in `usage.byModel`. The
286
+ part reads of a whole-file read are counted under the explorer.
287
+
288
+ ```ts
289
+ // `result` is what askWithFiles or askGemini returned.
290
+ for (const [id, used] of Object.entries(result.usage.byModel)) {
291
+ console.log(`${id}: ${used.requests} requests, ${used.inputTokens} input tokens (${used.cachedInputTokens} cached), ${used.outputTokens} output tokens`);
292
+ }
293
+ ```
294
+
295
+ The key of an entry is the adapter's `id`. By default it is the model's name: the `model` option of
296
+ `openAiCompatible()` and `anthropic()`, and for `gemini()` the name after `/models/` in its URL.
297
+ A gateway URL without `/models/` gives the host instead, so two Gemini models behind the same host
298
+ share one entry. With `askWithFiles`, name each model with the `id` option of its adapter;
299
+ `askGemini` has no such option.
300
+
301
+ `turns` counts the chat requests of both models, part reads not included. `finishReason` and
302
+ `lastUsage` are those of the last chat request that reported them: the answer, unless the
303
+ explorer's prompt was blocked.
304
+
305
+ ## The budget, token scale and pages
306
+
307
+ Both models get the same conversation and the same tool results, so a question is planned for
308
+ what both can take. What a pair changes:
309
+
310
+ - **The smaller window.** The budget of the question, for both models, is no larger than the
311
+ smaller `contextTokens` and `maxRequestBytes` that either adapter declares. With `askGemini`,
312
+ whose models declare neither, set the smaller window in the library's `budget`.
313
+ - **The larger token scale.** The budget is divided by the larger `tokenScale` of the two, so a
314
+ Claude model (1.35 by default) on either side plans the documents for a smaller window. The
315
+ count each provider reports is read by the scale of the model that reported it, and a whole-file
316
+ read is priced, against `limits.inputTokens`, in the explorer's tokens.
317
+ - **Pages only when both can take them.** `view_pages` is offered only when both adapters can be
318
+ sent a file (`openAiCompatible()` only with `pdfInput: true`). Pages stay in the request until
319
+ the answer, so a page is counted with the larger `pageTokens` of the two.
320
+ - **No stand-ins with Claude on either side.** When room runs short, older tool results are
321
+ replaced by short stand-ins only when neither model declares `appendOnly`. With Claude exploring
322
+ or answering, nothing is replaced, for either model: when the room runs out, the question is
323
+ answered with what it has.
324
+
325
+ The rules and what each adapter declares by default are in [Costs and limits](./costs-and-limits.md).
326
+ How the request is kept inside the budget is in [How it works](../how-it-works.md).
327
+
328
+ ## What changes for your application
329
+
330
+ - **One request more.** The answer is always a request of its own, after the explorer's last turn.
331
+ A question that one model settles in three requests takes four.
332
+ - **`text` and `onText` carry only the answer.** Without an explorer, a remark the model makes
333
+ before a tool call ("Let me search…") is part of `text`, and is streamed. With one, the first
334
+ text your user sees is the answer. `answered` is false when the answering model wrote no text,
335
+ also after the second try of its answer.
336
+ - **Progress still arrives.** `onActivity` and `onStep` report the explorer's tool calls, and
337
+ `steps` and `evidence` list them, as they do for one model. `onActivity` says "Writing the
338
+ answer…" when the answer is asked for. See [Web app](./web-app.md) for sending them over SSE.
339
+ - **The explorer's requests are not streamed** by `gemini()` and `openAiCompatible()`: nothing it
340
+ writes is shown, so each reply comes in one piece. `anthropic()` streams every request unless
341
+ `stream: false`, an explorer's too, and still shows none of it. A reply that is not streamed
342
+ comes whole at the end, so `firstByteTimeoutMs` (600 seconds by default) is the longest an
343
+ explorer's request may take before any of it arrives; one that takes longer is aborted and sent
344
+ again, once. Your user sees steps, not text, during that time: keep the connection to the browser
345
+ alive ([Deployment](./deployment.md)).
346
+ - **An explorer that fails is an error.** A model name that does not exist, or a quota that is
347
+ used up, throws the explorer's `ModelRequestError` (`GeminiRequestError` with `askGemini`), as an
348
+ error of `model` does. The question is not passed to `model` instead: that would hide the
349
+ mistake, and send the searching to the model you wanted to spare. `partialResultOf(error)` gives
350
+ what was found and paid for until then. See [Errors and reliability](./errors-and-reliability.md).
351
+ - **A PDF whose text is in the prompt** and whose pages can be looked at goes to the explorer
352
+ first: it decides whether to look. The text of such a PDF is then sent to both models, so you pay
353
+ for it twice.
354
+ - **Follow-up questions work as with one model.** `result.toolTurns` holds the explorer's rounds as
355
+ plain calls and results, which any model can be given. Keep them with the conversation as
356
+ [Conversations](./conversations.md) shows.
357
+
358
+ ## When an explorer is worth it
359
+
360
+ Every request of a question carries the conversation so far: the documents in the prompt, the
361
+ cards, and the tool results. With one model, each round of searching and reading is a request to
362
+ that model, each larger than the one before, and a whole-file read is one request per part, up to
363
+ `maxMapChunks` (30 by default), and more to merge their notes. With an explorer, all of those go to
364
+ the explorer, and `model` gets one request, about as large as the explorer's last.
365
+
366
+ An explorer pays off when:
367
+
368
+ - **The files are searched, not sent whole.** A question about a large log can take ten requests of
369
+ searching and reading. The more rounds and whole-file reads, the more of the cost moves to the
370
+ explorer.
371
+ - **The model that answers costs much more per token**, or is slow: a model that thinks long, for
372
+ example. Its quality goes into the answer, and its price into one request.
373
+ - **The explorer is faster.** The rounds of searching are most of the requests of a question about
374
+ a large file, and they run one after another.
375
+
376
+ It does not, or it costs more, when:
377
+
378
+ - **The documents fit whole.** With no searchable document and no pages to look at, the explorer
379
+ is not called: it changes nothing.
380
+ - **Questions end after a search or two.** With one model, two rounds and the answer are three
381
+ requests. With an explorer, they are three requests to the explorer (two rounds and `DONE`) and
382
+ one more to `model`, as large as the last: the saving is small, and the answer comes later.
383
+ - **The explorer misses what the answer needs.** The answering model cannot search again. An
384
+ explorer that stops early, or reads the wrong lines, gives an answer from less than the stronger
385
+ model would have found alone.
386
+ - **The two windows differ much.** The question runs inside the smaller one, for both models.
387
+
388
+ Try a pair on your own files and questions: `result.usage.byModel` shows what each model cost, and
389
+ `result.evidence` what was read. Live checks of explorer pairs on the Gemini API and Vertex AI,
390
+ with how each model behaved, are in [Measurements](../measurements.md).
391
+
392
+ ## Related
393
+
394
+ - [Costs and limits](./costs-and-limits.md): `limits`, the budget per model, usage totals.
395
+ - [Gemini and Vertex AI](./vertex-and-gemini.md), [OpenAI-compatible endpoints](./openai-compatible.md), [Claude](./anthropic.md): setting up each adapter.
396
+ - [PDFs](./pdfs.md): `view_pages` and where pages go.
397
+ - [How it works](../how-it-works.md): the tool loop end to end.
398
+ - [Ask reference](../reference/ask.md): `askWithFiles`, `AskOptions`, `AskLimits`, `AskResult`, `EXPLORER_INSTRUCTION`, `FINAL_ANSWER_*`.
399
+ - [Gemini reference](../reference/gemini.md): `askGemini`, `explorerGenerationConfig`, `geminiApi()`, `vertexAi()`, `gemini()`.