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
@@ -1,554 +1,98 @@
1
1
  # Configuration
2
2
 
3
+ The options are no longer on one page. Every option, with its default, is now in the reference,
4
+ one page per module of the package, and how to use them for a task is in the guides. This page
5
+ keeps the headings of the old page, so that links to them still work, and each one points to its
6
+ new home. All pages are listed in the [documentation index](./README.md).
7
+
3
8
  ## Budget
4
9
 
5
- The budget describes **your model connection**. Set the real numbers of your connector; the
6
- planner uses them to decide what goes into a request whole.
7
-
8
- | Setting | Default | What it controls |
9
- |---|---|---|
10
- | `contextTokens` | 1,000,000 | The model's context window. |
11
- | `maxRequestBytes` | 10,000,000 | The largest request body your connector accepts. Checked separately from tokens: non-English text and JSON escaping make bytes run ahead of tokens. |
12
- | `inlineTokens` | 200,000 | The most document text sent whole in one request. Lower it to cut cost and latency; raise it to rely on the long context more. |
13
- | `reservedTokens` | 64,000 | Kept free for the answer, tool results, and growing history. |
14
- | `mapChunkTokens` | 100,000 | Part size when a whole file is read in parts. Smaller parts give more detailed notes but more model calls. |
15
- | `maxMapChunks` | 30 | Refuse whole-file reads needing more model calls than this. 30 parts × 100K tokens = 3M tokens of input. |
16
- | `toolResultCharacters` | 24,000 | Cap on one tool result, in characters. For a log that can be 16,000 tokens, and every result is sent again on each later turn. |
17
-
18
- ```ts
19
- createLargeFiles({
20
- store,
21
- budget: { contextTokens: 128_000, maxRequestBytes: 4_000_000, inlineTokens: 60_000, mapChunkTokens: 40_000 },
22
- });
23
- ```
10
+ How to set the budget for your model is now in [Costs and limits](./guides/costs-and-limits.md); every field and its default is in [`Budget`](./reference/library.md#budget).
24
11
 
25
- ### How the plan is made
12
+ ### One library, several models
26
13
 
27
- For each request, `prepare(ids, base)` takes the size of everything else (`base.tokens`,
28
- `base.bytes`) and fills the room left, smallest documents first:
14
+ Now in [Costs and limits](./guides/costs-and-limits.md); what an adapter says about its model is [`ModelCapabilities`](./reference/ask.md#modelcapabilities).
29
15
 
30
- - token room = `min(inlineTokens, contextTokens − reservedTokens − base.tokens)`
31
- - byte room = `maxRequestBytes − 256 KB − base.bytes`
16
+ ### Limits and retries
32
17
 
33
- A document is sent whole if it fits both. Otherwise it becomes searchable.
18
+ The limits of a question are now in [Costs and limits](./guides/costs-and-limits.md) and [`AskLimits`](./reference/ask.md#asklimits); retries are in [Errors and reliability](./guides/errors-and-reliability.md) and [`RetryOptions`](./reference/errors-and-retries.md#retryoptions), and the waits on a silent model (`firstByteTimeoutMs`, `idleTimeoutMs`) are in the same guide and [`IdleWaits`](./reference/errors-and-retries.md#idlewaits).
34
19
 
35
- ### Token estimates
20
+ ### Room in the request
36
21
 
37
- `estimateTokens` prices each kind of character, because characters per token depends on the kind
38
- of text. As counted by Gemini:
22
+ Now in [How it works](./how-it-works.md).
39
23
 
40
- | Text | Characters per token |
41
- |---|---|
42
- | English prose | 4.3 to 5.0 |
43
- | Source code | 3.5 to 4.1 |
44
- | Hebrew, Arabic, Russian prose | 2.1 to 3.3 |
45
- | Chinese, Japanese, Korean prose | 1.4 to 1.7 |
46
- | Logs (timestamps, IDs, IP addresses) | 1.4 to 2.8 |
47
- | CSV of numbers, hex dumps, base64 | 1.0 to 1.4 |
24
+ ### How the plan is made
48
25
 
49
- Digits cost about one token each, which is why a log is three times denser than a novel of the
50
- same size. The estimate was fitted to Gemini's `countTokens` on 60 texts (Gemini 2.5, 3.5, and 3.8
51
- count identically) and lands between 0.98 and 1.7 times the real count. It leans high on purpose:
52
- overestimating only makes a file searchable a little sooner. The one kind of text it still counts
53
- low, at about 70%, is a long run of random letters in one case with no digits.
26
+ Now in [How it works](./how-it-works.md).
54
27
 
55
- Tokenizers differ between providers. These weights are fitted to Gemini, which counts digits one
56
- by one. A tokenizer that groups digits will count fewer tokens than this estimate for logs and
57
- data, which is the safe direction.
28
+ ### Token estimates
29
+
30
+ Now in [How it works](./how-it-works.md).
58
31
 
59
32
  ## An explorer model
60
33
 
61
- The model is yours to choose: it is the `model` you give `geminiApi()` or `vertexAi()`, or the URL
62
- of your own endpoint. The package has no default. That one model does both halves of the work on a
63
- large file: it explores the file (searches, reads, looks at pages), and it answers.
64
- `explorerModel` names a second model for the first half:
65
-
66
- ```ts
67
- const endpoint = geminiApi({
68
- apiKey,
69
- model: "gemini-3.8-flash", // writes the answer
70
- explorerModel: process.env.GEMINI_EXPLORER_MODEL, // optional: searches and reads
71
- });
72
- // vertexAi({ project, model, explorerModel, accessToken }) takes it the same way.
73
- ```
74
-
75
- Left out, empty, or the same as `model`, there is no explorer, and `askGemini` sends exactly the
76
- requests it sent before. So a setting that may be unset can be passed as it is.
77
-
78
- With URLs of your own, give the explorer's as `explorerUrl`. It is called with the same headers:
79
-
80
- ```ts
81
- const endpoint = {
82
- url: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:generateContent",
83
- streamUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:streamGenerateContent?alt=sse",
84
- explorerUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.5-flash-lite:generateContent",
85
- headers,
86
- };
87
- ```
88
-
89
- The reason to use one: a question about a large log can take ten requests of searching and
90
- reading, each larger than the one before, and a whole-file read adds up to 30 part reads. With an
91
- explorer those go to a cheaper or faster model, and the model you chose for its answers is called
92
- once.
34
+ Now in [An explorer model](./guides/explorer-model.md).
93
35
 
94
36
  ### Who does what
95
37
 
96
- | | The explorer | `model` |
97
- |---|---|---|
98
- | Requests with tools: `search_document`, `read_document`, `get_document_overview`, `view_pages` | All | None |
99
- | The part reads of `analyze_whole_document` | All | None |
100
- | The answer | | One request, without tools |
101
- | A question about files that are sent whole, with no pages to look at | Not called | One request, as before |
102
-
103
- The answering model gets the conversation, the explorer's tool calls, and what each call
104
- returned, pages included. It does not get the explorer's thoughts, its thought signatures, or
105
- anything it wrote: the answer is written from what the files say, not from what the explorer made
106
- of them. So the answering model can use only what the explorer fetched. It has no tools of its own
107
- to fetch more.
108
-
109
- The explorer is told its part, in a line added to its system instruction (`EXPLORER_INSTRUCTION`):
110
- gather what the answer needs, then reply `DONE`. `gemini-3.5-flash-lite` did exactly that, at one
111
- output token. A model that writes an answer anyway (`gemini-2.5-flash-lite` did) costs those output
112
- tokens, and its answer is dropped.
38
+ Now in [An explorer model](./guides/explorer-model.md).
113
39
 
114
40
  ### What changes for your application
115
41
 
116
- - **One request more.** The answer is always a request of its own, so a question that one model
117
- settles in three requests takes four. `maxToolTurns` still counts them all: the explorer has
118
- `maxToolTurns − 1` requests, and the last one is the answer.
119
- - **`text` and `onText` carry only the answer.** Without an explorer, a remark that the model makes
120
- before a tool call ("Let me search…") is part of `text`. The explorer's words are not shown.
121
- `onActivity` and `onStep` report its tool calls as before, and `turns` counts its requests too.
122
- `usage` and `finishReason` are those of the answer.
123
- - **The explorer's requests are not streamed.** Nothing it writes is shown, so each goes to
124
- `explorerUrl` in one piece. The answer streams as before.
125
- - **Settings are per model.** `explorerGenerationConfig` is the explorer's `generationConfig`, and
126
- defaults to yours. Set it when the two models do not take the same settings: given a
127
- `thinkingLevel`, `gemini-2.5-flash-lite` on Vertex AI answers HTTP 400, "thinking_level is not
128
- supported by this model". `analysisConfig` is for the part reads, which the explorer does, so
129
- write it for the explorer.
130
- - **The budget has to fit both models.** Both get the same conversation and the same tool
131
- results. Set `contextTokens` and `maxRequestBytes` to the smaller of the two.
132
- - **Pages have to be readable by both models.** `toolFilePlacement: "inside"` is read by Gemini 3
133
- models only. If either model is Gemini 2.5, use `"beside"`, which both read
134
- ([below](#where-the-pages-go-toolfileplacement)).
135
- - **An explorer that fails is an error.** A model name that does not exist, or a quota that is
136
- used up, throws `GeminiRequestError`, as it does for `model`. The question is not passed to
137
- `model` instead: that would hide the mistake, and send the searching to the model you wanted to
138
- spare.
139
- - **A PDF whose text is in the prompt**, and whose pages can be looked at, goes to the explorer
140
- first: it decides whether to look. The text of such a PDF is then sent twice, once to each model.
141
-
142
- ```ts
143
- await askGemini({
144
- files, endpoint, messages, documentIds,
145
- generationConfig: { thinkingConfig: { thinkingLevel: "high" } }, // for `model`
146
- explorerGenerationConfig: { thinkingConfig: { thinkingBudget: 0 } }, // for a Gemini 2.5 explorer
147
- toolFilePlacement: "beside", // one of the two is Gemini 2.5
148
- });
149
- ```
42
+ Now in [An explorer model](./guides/explorer-model.md).
150
43
 
151
44
  ### Live results with an explorer
152
45
 
153
- Flash-Lite models, November 2026, through `ingest` and `askGemini`. The log has 6,000 lines, 24
154
- deadlocks and one fatal error. The PDF is the one with a chart and a photo from
155
- [Pictures in PDFs](#live-results).
156
-
157
- | Explorer → `model` | Question | Result |
158
- |---|---|---|
159
- | `gemini-3.5-flash-lite` → `gemini-3.1-flash-lite`, Gemini API | How many deadlocks, and the line of the first | Two searches, then `DONE`. Answer right. Four requests, 3 s. |
160
- | | What shut the gateway down, its error code, the warning before it | One search, then `DONE`. Four of four facts, with the right lines. |
161
- | | The name of the file and its line count, which the card shows | `DONE` at once. Answered from the card, in two requests. |
162
- | | Every kind of error, from a whole-file read | The explorer read the four parts and wrote up the notes, then searched three times. Answer right. |
163
- | | The chart and the photo, with the text sent whole | The explorer looked at two pages. The other model got them and had five of five facts right. |
164
- | | The same, with the PDF searched | A read and a look in one turn. Five of five. |
165
- | | The gateway and the chart again, both models thinking (`thinkingLevel: "high"`) | Both right. |
166
- | `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Gemini API | The deadlocks | Answer right. The explorer repeated one search until its eleven requests were used up, as it does when it works alone. |
167
- | `gemini-2.5-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI | The deadlocks, the gateway, and the chart with pages `"beside"` | All right. This explorer wrote an answer each time instead of `DONE`. |
168
- | `gemini-3.5-flash-lite` → `gemini-2.5-flash-lite`, Vertex AI | The deadlocks, the chart with pages `"beside"`, and the card | All right. |
169
- | `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI, both thinking | The gateway | Right. |
170
-
171
- What made the form of the handoff: a Gemini 3 model refuses a request that ends with the result of
172
- a function call it did not sign (HTTP 400, "Function call is missing a thought_signature"), and a
173
- thought signature belongs to the model that made the call. When a user message follows the
174
- results, the same calls are accepted without a signature: by Gemini 3.1 and 3.5 Flash-Lite on the
175
- Gemini API and on Vertex AI, and by Gemini 2.5 Flash-Lite on Vertex AI. So the answering model's
176
- request always ends with a user message, and holds no signatures.
177
-
178
- Not checked: models larger than Flash-Lite, and Gemini 4. The two models have to be reachable with
179
- the same headers, so one cannot be on the Gemini API and the other on Vertex AI.
46
+ Now in [Measurements](./measurements.md); what they mean for your setup is in [An explorer model](./guides/explorer-model.md).
180
47
 
181
48
  ## Stores
182
49
 
183
- | Store | Use it for |
184
- |---|---|
185
- | `MemoryDocumentStore` | Tests, scripts, one-instance demos. Lost on restart. |
186
- | `FileDocumentStore(dir)` | One server or a shared volume. Survives restarts. |
187
- | Your own | Several instances: implement the interface below over Redis, S3, or a database. |
188
-
189
- ```ts
190
- interface DocumentStore {
191
- put(document: { meta: DocumentMeta; text: string }): Promise<void>;
192
- get(id: string): Promise<{ meta: DocumentMeta; text: string } | undefined>; // undefined if expired
193
- getMeta(id: string): Promise<DocumentMeta | undefined>;
194
- delete(id: string): Promise<boolean>; // the original file and the index too
195
- sweep?(now?: Date): Promise<number>;
196
- putOriginal?(id: string, bytes: Uint8Array): Promise<void>; // optional: see below
197
- getOriginal?(id: string): Promise<Uint8Array | undefined>; // undefined if not kept, or expired
198
- // Optional: read large documents by range instead of loading them whole (see below).
199
- readTextRange?(id: string, start: number, end: number): Promise<Uint8Array | undefined>; // UTF-8 bytes
200
- putIndex?(id: string, index: Uint8Array): Promise<void>;
201
- getIndex?(id: string): Promise<Uint8Array | undefined>;
202
- }
203
- ```
204
-
205
- Keep `getMeta` cheap (store the metadata separately from the text): it is called on every tool
206
- call. A document ready to search is kept in memory between tool calls, within `cacheBytes`
207
- (default 256 MB, the least recently used dropped first; `cacheSize` caps the number of documents too). Every
208
- use of a kept document asks the store first, so a document deleted or expired anywhere is gone
209
- everywhere at once.
50
+ Now in [Storage](./guides/storage.md) and, for several servers, in [Deployment](./guides/deployment.md); the interface is [`DocumentStore`](./reference/library.md#documentstore).
210
51
 
211
52
  ### Large files, many users, several servers
212
53
 
213
- With a store that has `readTextRange`, as `FileDocumentStore` does, a document whose text is larger
214
- than `wholeDocumentBytes` (default 2 MB) is never loaded whole:
215
-
216
- - At ingest, a line index is written next to the text (`<id>.idx`, four bytes a line: 3 MB for a
217
- file of 730,000 lines). `read_document`, the card and the overview read only the lines they show.
218
- - A search reads the text 1 MB at a time (`scanChunkBytes`), reading the next piece while it searches
219
- the current one, so a search holds a few MB whatever the size of the file. At most
220
- `maxConcurrentScans` searches (default 4) run at once in a process; the others wait their turn,
221
- and those waiting for the same file share one pass over it: it is read, decoded and lowercased
222
- once for all of them. Results are the same as from the whole text: the tests compare them call by call.
223
- - Decoded pieces of a file are kept in the same `cacheBytes` budget, so the next search of a file in
224
- use reads nothing. A file whose decoded text would take more than a quarter of the budget is never
225
- kept: it would push out everything else. A larger `cacheBytes` spends memory to save CPU.
226
- - A document from an older version, without an index, gets one the first time it is used: it is
227
- built from the text in one pass and saved for the other servers. A damaged index, or one that
228
- does not match its document, is noticed and built again.
229
-
230
- Smaller documents are loaded whole and kept in the cache: for them that is faster, and they are small.
231
- `wholeDocumentBytes: Infinity` loads every document whole, as before 0.4; `0` reads every one by range.
232
-
233
- `FileDocumentStore` on a volume that several servers share:
234
-
235
- - Every file is written under a temporary name and then renamed, so no server reads half a file.
236
- - `sweep` removes files of no document (an upload that failed, a server that stopped while writing)
237
- and temporary files once they are an hour old (`orphanAgeMs`), never younger ones: they may be an
238
- upload in progress on another server. Run `sweep` from one place, such as a scheduled job.
239
- - A document whose metadata could not be read during a sweep (the volume did not answer) is left
240
- alone, whatever its age. Only metadata that is not valid is taken for a broken document.
241
- - A read that fails for another reason than "no such file" is thrown, and the library passes it
242
- on as `DocumentStoreError`: a busy volume is not a deleted document. A store of your own should
243
- do the same: return `undefined` only for a document that is not there.
244
- - Servers of an older version on the same volume (during a rolling update) read the same files and
245
- ignore the index. An index left behind when one of them deletes a document is swept an hour later.
246
-
247
- A store of your own can do the same over S3 (`Range` requests), Redis (`GETRANGE`) or a database:
248
- add `readTextRange`, and `putIndex` with `getIndex` to keep the index. Without `putIndex` the index
249
- is built in memory each time the document is opened.
54
+ Reading large documents by range and the cache are now in [Storage](./guides/storage.md); several servers, memory and `sweep` are in [Deployment](./guides/deployment.md).
250
55
 
251
56
  ### Keeping the uploaded file
252
57
 
253
- By default a store holds only the text of a document. `keepOriginals` also keeps the uploaded file,
254
- which is what [showing pages of a PDF](#pictures-in-pdfs) needs:
255
-
256
- ```ts
257
- createLargeFiles({ store: new FileDocumentStore(dir), keepOriginals: ["pdf"] });
258
- ```
259
-
260
- - It is off by default because it costs disk: a kept file is as large as the upload (up to
261
- `maxUploadBytes`, 100 MB) and stays until the document expires or is deleted.
262
- - `FileDocumentStore` writes it as `<id>.bin` next to `<id>.txt` and `<id>.json`.
263
- `MemoryDocumentStore` keeps a copy in memory.
264
- - A store of your own keeps originals when it has `putOriginal` and `getOriginal`. `putOriginal` is
265
- called before `put` for the same ID, and `delete` and `sweep` must remove the file. A store
266
- without the two methods works as before: nothing is kept, and `view_pages` is not offered.
267
- - `DocumentMeta.originalKept` says whether a document's file is kept. `files.getOriginal(id, { owner })`
268
- returns it.
269
- - Documents ingested before you turned it on have no original. Upload them again to get one.
58
+ Now in [Storage](./guides/storage.md); what `view_pages` does with the kept file is in [PDFs and other formats](./guides/pdfs.md).
270
59
 
271
60
  ## Formats
272
61
 
273
- | Format | Detected by | Notes |
274
- |---|---|---|
275
- | `log` | `.log`, `.out`, `.err`, or text whose early lines mostly start with a timestamp or level | Gets a log profile |
276
- | `markdown` | `.md`, `.markdown`, `text/markdown` | Headings form the outline |
277
- | `json` | `.json`, `.jsonl`, `.ndjson` | Minified JSON is wrapped into addressable lines |
278
- | `csv` | `.csv`, `.tsv` | |
279
- | `pdf` | `.pdf` | Needs `unpdf`. Pages are marked `--- Page N ---`, tables are rebuilt (below), and a page without text says so. With `@cantoo/pdf-lib`, a page that shows a picture says so too, and the model can be shown the page itself ([Pictures in PDFs](#pictures-in-pdfs)). A scanned PDF (no text layer at all) is rejected, unless its original is kept and it has at most 30 pages. |
280
- | `docx`, `pptx` | extension or MIME type | Needs `jszip`. Slides are marked `## Slide N`. A text box is read once, after the paragraph it is anchored in. A file that unpacks to more than 100 times its size (past the first 16 MB), or to more than 128 MB of XML, is rejected as a possible zip bomb. |
281
- | `text` | anything else that decodes as text | UTF-8 or UTF-16 (with or without BOM); binary is rejected |
282
-
283
- Lines longer than 2,000 characters are split, so every line can be read by number.
62
+ Now in [PDFs and other formats](./guides/pdfs.md), which also says how a text file that is not in UTF-8 is read; the fields that record it are `encoding` and `replacedCharacters` of [`DocumentMeta`](./reference/library.md#documentmeta).
284
63
 
285
64
  ### PDF tables
286
65
 
287
- A PDF stores no tables, only text at positions. Read in file order, a row with an empty cell looks
288
- like a row without that column: `EMEA 80 95 101` under the headings `Q1 Q2 Q3 Q4`, and a model
289
- reads 95 as Q2. The PDF extractor finds tables by the position of their text and writes each row
290
- with its cells marked, so the same row becomes:
291
-
292
- ```
293
- | Region | Q1 | Q2 | Q3 | Q4 |
294
- | EMEA | 80 | | 95 | 101 |
295
- ```
296
-
297
- In a live test (`gemini-3.5-flash-lite`, 110 questions about single cells of seven PDFs made by
298
- Word and Chrome, half of them about empty cells), 44 answers were right from the text in file
299
- order and 110 from the rebuilt rows.
300
-
301
- What it handles: empty cells, tables inside one column of a two-column page, right-to-left tables
302
- (the first column is the rightmost), tables printed sideways, rotated pages, footnote marks,
303
- headings that span columns, and cells whose text wraps onto several lines when the rows are
304
- visibly spaced apart. Text that is not a table (paragraphs, lists, columns of prose, justified
305
- text) is left as pdf.js gives it.
306
-
307
- It is cautious: where the evidence for a table is weak, the text stays as it was. So:
308
-
309
- - Two-column tables (key and value, a glossary) are left as lines of text. They read correctly
310
- that way.
311
- - When a cell wraps and the rows have no extra space between them, each line of the cell is a row
312
- of its own, with the other cells empty.
313
- - Columns that are closer together than a word space on every line of the table stay together in
314
- one cell, on every row alike.
315
- - A table whose text is stored column by column in the file is not rebuilt.
316
- - Pictures and charts are not in the text. A page with no text at all is written as
317
- `[No text on this page. It may be a scan, a photo, or a chart.]`, so the model can say that it
318
- cannot see it. To let the model look at such pages, see [Pictures in PDFs](#pictures-in-pdfs).
319
-
320
- The cell marks cost tokens: about one per cell. A bank statement came to 27% more tokens than
321
- its text in file order, a report with one table in it to 9% more. Reading 600 pages with 24,600
322
- table rows took 1.6 seconds instead of 1.3.
323
-
324
- The rules were tuned on PDFs made by Chrome and by Word. To get the text of version 0.1.0 instead:
325
-
326
- ```ts
327
- import { createLargeFiles, createPdfExtractor } from "llm-large-files";
328
-
329
- createLargeFiles({ store, extractors: { pdf: createPdfExtractor({ tables: false }) } });
330
- ```
331
-
332
- Small PDFs are still better sent to Gemini as the file itself, when your payload limit allows:
333
- it then sees every page, including pictures. For a PDF that is too large for that, the next section
334
- gives the model the pages it asks for.
66
+ Now in [PDFs and other formats](./guides/pdfs.md).
335
67
 
336
68
  ## Pictures in PDFs
337
69
 
338
- The text of a PDF does not hold its photos, charts and diagrams. Asked about a bar chart from the
339
- text alone, a model can only say that it cannot see it, or guess. Two things, both optional and
340
- both for PDFs only, close that gap:
341
-
342
- 1. **A line in the text that says where the pictures are.**
343
- 2. **A tool, `view_pages`, that hands the model the real pages** as a small PDF, when it asks.
344
-
345
- ```bash
346
- npm install @cantoo/pdf-lib # optional dependency, needed for both
347
- ```
348
-
349
- ```ts
350
- const files = createLargeFiles({
351
- store: new FileDocumentStore("./data/large-files"),
352
- keepOriginals: ["pdf"], // needed for view_pages: it cuts pages out of the uploaded file
353
- });
354
- // askGemini then offers view_pages by itself. Nothing else changes in your code.
355
- ```
70
+ Now in [PDFs and other formats](./guides/pdfs.md).
356
71
 
357
72
  ### The line that marks a page
358
73
 
359
- At ingest, a page that shows a picture or a drawing gets one line under its header:
360
-
361
- ```
362
- --- Page 12 ---
363
- [Picture on this page. This text cannot show it.]
364
- Figure 3: sales by region.
365
- ```
366
-
367
- The words hold whether or not the model can look at the page later. A search for
368
- `[Picture on this page` lists every such page.
369
-
370
- What counts:
371
-
372
- - **A picture:** the images on the page cover 3% of it or more, measured by the size they are
373
- shown at, not by their pixels. A logo covers about 1%. An image that is on 30% of the pages (and
374
- on three at least) is taken for a logo, a letterhead or a watermark, and does not count,
375
- whatever its size.
376
- - **A drawing:** shapes filled in three or more colours, outside the tables. Greys, rules, dots
377
- and page backgrounds do not count, and neither does the shading of a table's cells.
378
- - A page whose drawing instructions are larger than 2 MB: that is a map or a technical drawing.
379
- So is a page that paints more than 20,000 images, or whose instructions come to more than 16 MB
380
- once each form (a drawing kept apart and painted by name) is counted every time it is painted.
381
- Reading such a page stops there, so a few kilobytes of forms painted inside forms cannot hold
382
- the server.
383
-
384
- What it gets wrong:
385
-
386
- - Missed: a chart in one or two colours, and a chart or diagram drawn only with lines.
387
- - Marked by mistake: a table with cells shaded in three colours, when the table itself was not
388
- recognised (a table of two columns, for example).
389
-
390
- The mark is a hint. `view_pages` works on any page, marked or not, and in a live test the model
391
- looked at a one-colour chart that had only a caption (below).
392
-
393
- The pages are found from the structure of the file, without decoding any image, so it is cheap:
394
- a PDF of 200 photos (85 MB) took 0.3 s to ingest without marks and 0.8 s with them. No page is
395
- marked, and the text is exactly that of version 0.2.0, when:
396
-
397
- - `@cantoo/pdf-lib` is not installed;
398
- - you turn it off: `createPdfExtractor({ pictures: false })`;
399
- - the file declares more than 150,000 objects (reading it would take over two seconds), reading
400
- took more than five seconds, or the file does not have the pages the text was read from.
401
-
402
- One cost to know: a PDF whose streams do not state their length up front (Ghostscript writes them
403
- so) is scanned byte by byte, about 30 ms per MB. A file of 100 MB written that way took 3 s to
404
- ingest instead of 0.1 s.
74
+ Now in [PDFs and other formats](./guides/pdfs.md).
405
75
 
406
76
  ### `view_pages`
407
77
 
408
- `view_pages(document_id, from_page, to_page)` cuts those pages out of the kept original and
409
- returns them as one PDF. `askGemini` puts it inside the function response, and Gemini reads it
410
- like any PDF: text, pictures and layout.
411
-
412
- It is offered for every PDF whose original is kept, also for one whose text is already in the
413
- prompt: a slide deck of photos has little text, so its text fits, and without the tool the model
414
- would see none of its slides. Such a PDF gets `view_pages` and no search tools.
415
-
416
- For a PDF that the model can look at, search and read results also say which page a line is on
417
- (`"page": 12`), since a page number is what `view_pages` takes.
418
-
419
- Limits:
420
-
421
- | Limit | Value | When it is reached |
422
- |---|---|---|
423
- | Pages per call | 5 | The result says where to continue (`next_page`). |
424
- | Bytes | What is left of `maxRequestBytes`, less a tenth kept free (256 KB at least), times 3/4 for base64 | Pages are left off the end of the call. A page that does not fit by itself gives an error, and the model answers from what it has. |
425
- | Pages per question | `reservedTokens / 2 / 530`: 60 with the default budget | The tool says that no more pages can be looked at. |
426
-
427
- Pages stay in the request until the question is answered, so every look makes the later requests
428
- of that question larger. They are not sent again with the next question.
429
-
430
- A page costs Gemini about 530 tokens, whatever is on it: 532 for each of 1, 2, 3 and 5 A4 pages,
431
- and 527 for a 16:9 slide (`gemini-3.5-flash-lite`).
432
-
433
- A cut holds the pages asked for and little else. Copying pages the simple way does not: a contents
434
- page links to every section, and those pages came along, so the cut was 89% of the file. Here every
435
- other page is emptied before copying, and a page that shares its resources with the whole file
436
- (FPDF, TCPDF and jsPDF write PDFs that way) gets a list of its own.
437
-
438
- | File | Pages cut | Size of the cut |
439
- |---|---|---|
440
- | Chrome, 72 photos, 100 MB | 5 | 7.0 MB (7%) |
441
- | PowerPoint deck, 40 photos, 9.9 MB | 5 | 1.2 MB (13%) |
442
- | Word report with a contents page, 7.5 MB | the contents page | 55 KB (0.7%) |
443
- | All images in one shared list, 12 MB | 2 | 0.6 MB (5%) |
444
-
445
- What a cut costs the server: the whole file is read into memory on every call.
446
-
447
- | File | Time | Memory at the peak |
448
- |---|---|---|
449
- | 100 MB of photos, 900 objects | 0.06 s | about 300 MB |
450
- | 100 MB written the way Ghostscript writes | 3 s | about 300 MB |
451
- | 54 MB, 330,000 objects (a tagged table of 591 pages) | 4 to 5 s | about 700 MB |
452
-
453
- A file that declares more than a million objects, or more than the free memory can hold, is not
454
- cut: the tool tells the model that the pages cannot be shown.
455
-
456
- Also handled: PDFs encrypted only to restrict printing or copying (they open without a password),
457
- hidden layers (they stay hidden), rotated pages, and links. Not shown: a PDF that needs a password
458
- to open, a damaged file, and a file in which this reader counts other pages than the text
459
- extractor did. In each case the model gets a message and answers from the text.
78
+ Now in [PDFs and other formats](./guides/pdfs.md); the tool's arguments and limits are in [Tools](./reference/tools.md).
460
79
 
461
80
  ### Scanned PDFs
462
81
 
463
- A PDF with no text at all has nothing to search, and is refused: `This PDF has no text layer`. When
464
- its original is kept, the model can read it by looking at its pages, so a scan of **up to 30
465
- pages** is accepted. Every page of its text says that it has no text, and the model gets
466
- `view_pages`. A longer scan is still refused: it could only be read five pages at a time, and it
467
- could not be searched.
468
-
469
- ```ts
470
- createLargeFiles({ store, keepOriginals: ["pdf"], scannedPdfPages: 30 }); // 30 is the default; 0 refuses all scans
471
- ```
472
-
473
- A PDF with text on any page was always accepted and still is, however many of its pages are scans.
82
+ Now in [PDFs and other formats](./guides/pdfs.md).
474
83
 
475
84
  ### Live results
476
85
 
477
- `gemini-3.5-flash-lite` on the Gemini API, November 2026, through `ingest` and `askGemini`:
478
-
479
- | Check | Result |
480
- |---|---|
481
- | Which bar of a chart is tallest and shortest, and their colours; a number on a sign in a photo. Text sent whole. | Looked at 2 pages unasked. All five facts right. From the text alone, the model said it could not see the chart. |
482
- | The same, with the PDF searched instead | One look, all five right. |
483
- | Ten questions that the text answers, about a PDF with a photo on each of its 60 pages | 0 of 10 looked at a page, with the text sent whole and with the PDF searched. 10 of 10 right both times. |
484
- | A chart in one colour: a caption, but no mark | The model looked at the page and read the chart correctly. |
485
- | Five pages in one look (five numbers on five signs) | All five right. 2,660 tokens for the pages. |
486
- | A slide deck of 41 slides: a number on slide 8, a bar on slide 1 | Two looks, both right. |
487
- | A PDF encrypted to restrict printing | One look, all right. |
488
- | A scan of three pages (pictures of text) | One look at the three pages. Four of four facts right. |
489
- | The request without tools that ends a question, after a look | Accepted, answered correctly, with the pages still in it. |
490
-
491
- **Vertex AI**, with the same model (locations `global` and `eu`, November 2026): one look, two
492
- looks in one model turn, one look that decides the next, the request without tools that ends a
493
- question, and a PDF that is searched. All were accepted and answered correctly, with `vertexAi()`
494
- and through an application's own URLs, streaming and not. Real files went through too: a number
495
- on a photo in a 7.5 MB report written by Word, and five numbers from one look at five pages of a
496
- 25 MB file (a 2.2 MB PDF in the request). A page costs 532 tokens there as well.
86
+ Now in [Measurements](./measurements.md).
497
87
 
498
88
  ### Where the pages go: `toolFilePlacement`
499
89
 
500
- | `toolFilePlacement` | What is sent | Gemini 3.5 Flash-Lite, on the Gemini API and on Vertex AI | Gemini 2.5 Flash-Lite, on Vertex AI |
501
- |---|---|---|---|
502
- | `"inside"` (default) | The PDF in the `parts` of its function response, which names it as `{ "$ref": "pages-12-14.pdf" }` | Works | Accepted and counted, but the model does not see the pages |
503
- | `"beside"` | The function responses in one `user` turn, then the PDFs in a `user` turn of their own, each after a line that names it | Works | Works |
504
-
505
- ```ts
506
- await askGemini({ ..., toolFilePlacement: "beside" }); // for Gemini 2.5
507
- ```
508
-
509
- Keep the default for Gemini 3 models. Use `"beside"` for Gemini 2.5: it takes a file inside a
510
- function response without an error, counts its tokens, and then answers that it cannot see the
511
- page. Gemini 2.5 on the Gemini API was not checked (that model is closed to new users there), and
512
- neither was Gemini 4.
513
-
514
- With `"beside"` the files have a turn of their own because Vertex AI refuses a turn in which
515
- anything follows a function response (HTTP 400, "Requests ending with a model turn are not
516
- supported", seen with Gemini 3.5).
90
+ Now in [PDFs and other formats](./guides/pdfs.md); the option is in the [`llm-large-files/gemini`](./reference/gemini.md) and [`llm-large-files/anthropic`](./reference/anthropic.md) references.
517
91
 
518
92
  ### Your own loop
519
93
 
520
- The pages are a file, and a loop that drops the file would tell the model that pages are attached
521
- with nothing attached. So `view_pages` is declared only when you say that your loop delivers files:
522
-
523
- ```ts
524
- const prepared = await files.prepare(documentIds, base, { owner, viewPages: true });
525
- const tools = files.tools(prepared.plan.searchable, { viewPages: prepared.viewable });
526
-
527
- const { result, files: attached } = await tools.executeWithFiles(call.name, call.args, {
528
- maxFileBytes: Math.floor(roomLeftInYourRequest * 3 / 4), // base64 turns three bytes into four
529
- maxPages: pagesYouStillAllowForThisQuestion,
530
- });
531
- // attached: [{ name: "pages-12-14.pdf", mimeType: "application/pdf", bytes }]
532
- // Send `result` as the tool result and each file as a PDF with it, in your provider's form.
533
- // For Gemini, once every call of the model's turn is answered:
534
- contents.push(...geminiFunctionResponseTurns(answers)); // answers: [{ call, result, files: attached }]
535
- ```
536
-
537
- `tools.execute("view_pages", ...)` returns only JSON, so it answers with an error instead of
538
- pretending. `prepared.viewable` also lists PDFs that are not searched, because their text is in
539
- the prompt: pass it as it is.
94
+ Now in [Your own adapter and loop](./guides/own-adapter-and-loop.md).
540
95
 
541
96
  ### Custom extractors
542
97
 
543
- Replace or add one per format, for example OCR for scanned PDFs, or HTML to text:
544
-
545
- ```ts
546
- createLargeFiles({
547
- store,
548
- extractors: {
549
- pdf: async (bytes, { name }) => ({ text: await myOcrService(bytes), pages: undefined }),
550
- },
551
- });
552
- ```
553
-
554
- Formats come from `detectFormat(name, mimeType)`; unknown extensions use the `text` extractor.
98
+ Now in [PDFs and other formats](./guides/pdfs.md); the type is [`Extractor`](./reference/library.md#extractor), and what it returns is [`ExtractedText`](./reference/library.md#extractedtext).