llm-large-files 0.5.0 → 0.8.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 (285) hide show
  1. package/CHANGELOG.md +1719 -466
  2. package/README.md +190 -60
  3. package/dist/analyze.d.ts +8 -1
  4. package/dist/analyze.d.ts.map +1 -1
  5. package/dist/analyze.js +198 -54
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/anthropic/adapter.d.ts +14 -3
  8. package/dist/anthropic/adapter.d.ts.map +1 -1
  9. package/dist/anthropic/adapter.js +194 -40
  10. package/dist/anthropic/adapter.js.map +1 -1
  11. package/dist/ask.d.ts +63 -7
  12. package/dist/ask.d.ts.map +1 -1
  13. package/dist/ask.js +1131 -214
  14. package/dist/ask.js.map +1 -1
  15. package/dist/decode.d.ts +19 -0
  16. package/dist/decode.d.ts.map +1 -0
  17. package/dist/decode.js +97 -0
  18. package/dist/decode.js.map +1 -0
  19. package/dist/encoding.d.ts +17 -0
  20. package/dist/encoding.d.ts.map +1 -0
  21. package/dist/encoding.js +852 -0
  22. package/dist/encoding.js.map +1 -0
  23. package/dist/errors.d.ts +59 -0
  24. package/dist/errors.d.ts.map +1 -1
  25. package/dist/errors.js +80 -0
  26. package/dist/errors.js.map +1 -1
  27. package/dist/events.d.ts +339 -0
  28. package/dist/events.d.ts.map +1 -0
  29. package/dist/events.js +163 -0
  30. package/dist/events.js.map +1 -0
  31. package/dist/evidence.d.ts +2 -0
  32. package/dist/evidence.d.ts.map +1 -1
  33. package/dist/evidence.js +9 -2
  34. package/dist/evidence.js.map +1 -1
  35. package/dist/extract.d.ts +26 -7
  36. package/dist/extract.d.ts.map +1 -1
  37. package/dist/extract.js +180 -314
  38. package/dist/extract.js.map +1 -1
  39. package/dist/gemini/adapter.d.ts +17 -8
  40. package/dist/gemini/adapter.d.ts.map +1 -1
  41. package/dist/gemini/adapter.js +117 -66
  42. package/dist/gemini/adapter.js.map +1 -1
  43. package/dist/gemini/chat.d.ts +23 -3
  44. package/dist/gemini/chat.d.ts.map +1 -1
  45. package/dist/gemini/chat.js +6 -2
  46. package/dist/gemini/chat.js.map +1 -1
  47. package/dist/gemini/endpoints.d.ts +5 -1
  48. package/dist/gemini/endpoints.d.ts.map +1 -1
  49. package/dist/gemini/endpoints.js +11 -2
  50. package/dist/gemini/endpoints.js.map +1 -1
  51. package/dist/gemini/errors.d.ts +9 -4
  52. package/dist/gemini/errors.d.ts.map +1 -1
  53. package/dist/gemini/errors.js +32 -0
  54. package/dist/gemini/errors.js.map +1 -1
  55. package/dist/gemini/generate.d.ts +10 -2
  56. package/dist/gemini/generate.d.ts.map +1 -1
  57. package/dist/gemini/generate.js +33 -10
  58. package/dist/gemini/generate.js.map +1 -1
  59. package/dist/gemini/index.d.ts +3 -3
  60. package/dist/gemini/index.d.ts.map +1 -1
  61. package/dist/gemini/index.js +2 -2
  62. package/dist/gemini/index.js.map +1 -1
  63. package/dist/gemini/sse.d.ts +1 -1
  64. package/dist/gemini/sse.d.ts.map +1 -1
  65. package/dist/gemini/sse.js +1 -1
  66. package/dist/gemini/sse.js.map +1 -1
  67. package/dist/groups.d.ts +54 -0
  68. package/dist/groups.d.ts.map +1 -0
  69. package/dist/groups.js +136 -0
  70. package/dist/groups.js.map +1 -0
  71. package/dist/html.d.ts +11 -0
  72. package/dist/html.d.ts.map +1 -0
  73. package/dist/html.js +1112 -0
  74. package/dist/html.js.map +1 -0
  75. package/dist/http.d.ts +116 -0
  76. package/dist/http.d.ts.map +1 -1
  77. package/dist/http.js +347 -1
  78. package/dist/http.js.map +1 -1
  79. package/dist/index.d.ts +13 -7
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +7 -5
  82. package/dist/index.js.map +1 -1
  83. package/dist/library.d.ts +67 -5
  84. package/dist/library.d.ts.map +1 -1
  85. package/dist/library.js +449 -93
  86. package/dist/library.js.map +1 -1
  87. package/dist/limiter.d.ts +2 -1
  88. package/dist/limiter.d.ts.map +1 -1
  89. package/dist/limiter.js +3 -2
  90. package/dist/limiter.js.map +1 -1
  91. package/dist/map-reduce.d.ts +28 -1
  92. package/dist/map-reduce.d.ts.map +1 -1
  93. package/dist/map-reduce.js +240 -52
  94. package/dist/map-reduce.js.map +1 -1
  95. package/dist/model.d.ts +84 -2
  96. package/dist/model.d.ts.map +1 -1
  97. package/dist/model.js +190 -2
  98. package/dist/model.js.map +1 -1
  99. package/dist/node/handlers.d.ts +20 -4
  100. package/dist/node/handlers.d.ts.map +1 -1
  101. package/dist/node/handlers.js +184 -28
  102. package/dist/node/handlers.js.map +1 -1
  103. package/dist/node/index.d.ts +4 -0
  104. package/dist/node/index.d.ts.map +1 -1
  105. package/dist/node/index.js +2 -0
  106. package/dist/node/index.js.map +1 -1
  107. package/dist/node/ingest-child.d.ts +38 -0
  108. package/dist/node/ingest-child.d.ts.map +1 -0
  109. package/dist/node/ingest-child.js +42 -0
  110. package/dist/node/ingest-child.js.map +1 -0
  111. package/dist/node/isolated.d.ts +88 -0
  112. package/dist/node/isolated.d.ts.map +1 -0
  113. package/dist/node/isolated.js +381 -0
  114. package/dist/node/isolated.js.map +1 -0
  115. package/dist/node/sse.d.ts +59 -0
  116. package/dist/node/sse.d.ts.map +1 -0
  117. package/dist/node/sse.js +174 -0
  118. package/dist/node/sse.js.map +1 -0
  119. package/dist/office.d.ts +105 -0
  120. package/dist/office.d.ts.map +1 -0
  121. package/dist/office.js +1092 -0
  122. package/dist/office.js.map +1 -0
  123. package/dist/openai/adapter.d.ts +29 -5
  124. package/dist/openai/adapter.d.ts.map +1 -1
  125. package/dist/openai/adapter.js +325 -37
  126. package/dist/openai/adapter.js.map +1 -1
  127. package/dist/output-limit.d.ts +14 -2
  128. package/dist/output-limit.d.ts.map +1 -1
  129. package/dist/output-limit.js +39 -10
  130. package/dist/output-limit.js.map +1 -1
  131. package/dist/pause.d.ts +24 -0
  132. package/dist/pause.d.ts.map +1 -0
  133. package/dist/pause.js +38 -0
  134. package/dist/pause.js.map +1 -0
  135. package/dist/pdf-pages.d.ts +32 -2
  136. package/dist/pdf-pages.d.ts.map +1 -1
  137. package/dist/pdf-pages.js +277 -20
  138. package/dist/pdf-pages.js.map +1 -1
  139. package/dist/pdf-pictures.d.ts +30 -3
  140. package/dist/pdf-pictures.d.ts.map +1 -1
  141. package/dist/pdf-pictures.js +50 -8
  142. package/dist/pdf-pictures.js.map +1 -1
  143. package/dist/pdf-work.d.ts +40 -0
  144. package/dist/pdf-work.d.ts.map +1 -0
  145. package/dist/pdf-work.js +734 -0
  146. package/dist/pdf-work.js.map +1 -0
  147. package/dist/peers.d.ts +8 -0
  148. package/dist/peers.d.ts.map +1 -0
  149. package/dist/peers.js +32 -0
  150. package/dist/peers.js.map +1 -0
  151. package/dist/plan.d.ts +22 -4
  152. package/dist/plan.d.ts.map +1 -1
  153. package/dist/plan.js +61 -12
  154. package/dist/plan.js.map +1 -1
  155. package/dist/ranged.d.ts.map +1 -1
  156. package/dist/ranged.js +4 -2
  157. package/dist/ranged.js.map +1 -1
  158. package/dist/retry.d.ts +23 -1
  159. package/dist/retry.d.ts.map +1 -1
  160. package/dist/retry.js +33 -3
  161. package/dist/retry.js.map +1 -1
  162. package/dist/search.d.ts +107 -24
  163. package/dist/search.d.ts.map +1 -1
  164. package/dist/search.js +803 -149
  165. package/dist/search.js.map +1 -1
  166. package/dist/sse.d.ts +35 -2
  167. package/dist/sse.d.ts.map +1 -1
  168. package/dist/sse.js +145 -31
  169. package/dist/sse.js.map +1 -1
  170. package/dist/stores.d.ts.map +1 -1
  171. package/dist/stores.js +5 -2
  172. package/dist/stores.js.map +1 -1
  173. package/dist/text-index.d.ts +9 -1
  174. package/dist/text-index.d.ts.map +1 -1
  175. package/dist/text-index.js +28 -9
  176. package/dist/text-index.js.map +1 -1
  177. package/dist/text.d.ts +87 -3
  178. package/dist/text.d.ts.map +1 -1
  179. package/dist/text.js +526 -24
  180. package/dist/text.js.map +1 -1
  181. package/dist/tools.d.ts +61 -1
  182. package/dist/tools.d.ts.map +1 -1
  183. package/dist/tools.js +461 -46
  184. package/dist/tools.js.map +1 -1
  185. package/dist/types.d.ts +103 -4
  186. package/dist/types.d.ts.map +1 -1
  187. package/dist/word-styles.d.ts +102 -0
  188. package/dist/word-styles.d.ts.map +1 -0
  189. package/dist/word-styles.js +419 -0
  190. package/dist/word-styles.js.map +1 -0
  191. package/dist/xlsx.d.ts +23 -0
  192. package/dist/xlsx.d.ts.map +1 -0
  193. package/dist/xlsx.js +511 -0
  194. package/dist/xlsx.js.map +1 -0
  195. package/dist/xml-stream.d.ts +55 -0
  196. package/dist/xml-stream.d.ts.map +1 -0
  197. package/dist/xml-stream.js +235 -0
  198. package/dist/xml-stream.js.map +1 -0
  199. package/dist/zip-directory.d.ts +43 -0
  200. package/dist/zip-directory.d.ts.map +1 -0
  201. package/dist/zip-directory.js +130 -0
  202. package/dist/zip-directory.js.map +1 -0
  203. package/docs/README.md +288 -0
  204. package/docs/api.md +37 -381
  205. package/docs/configuration.md +28 -558
  206. package/docs/getting-started.md +407 -223
  207. package/docs/guides/anthropic.md +590 -0
  208. package/docs/guides/conversations.md +629 -0
  209. package/docs/guides/costs-and-limits.md +728 -0
  210. package/docs/guides/deployment.md +720 -0
  211. package/docs/guides/errors-and-reliability.md +748 -0
  212. package/docs/guides/explorer-model.md +399 -0
  213. package/docs/guides/openai-compatible.md +602 -0
  214. package/docs/guides/own-adapter-and-loop.md +1169 -0
  215. package/docs/guides/pdfs.md +1024 -0
  216. package/docs/guides/storage.md +731 -0
  217. package/docs/guides/testing.md +1203 -0
  218. package/docs/guides/vertex-and-gemini.md +717 -0
  219. package/docs/guides/web-app.md +1590 -0
  220. package/docs/how-it-works.md +1035 -164
  221. package/docs/measurements.md +881 -0
  222. package/docs/publishing.md +274 -46
  223. package/docs/reference/anthropic.md +427 -0
  224. package/docs/reference/ask.md +910 -0
  225. package/docs/reference/errors-and-retries.md +760 -0
  226. package/docs/reference/events.md +1001 -0
  227. package/docs/reference/gemini.md +827 -0
  228. package/docs/reference/library.md +1598 -0
  229. package/docs/reference/node.md +706 -0
  230. package/docs/reference/openai.md +385 -0
  231. package/docs/reference/tools.md +1031 -0
  232. package/docs/security.md +940 -101
  233. package/docs/troubleshooting.md +1410 -0
  234. package/package.json +7 -3
  235. package/src/analyze.ts +204 -49
  236. package/src/anthropic/adapter.ts +200 -40
  237. package/src/ask.ts +1151 -210
  238. package/src/decode.ts +89 -0
  239. package/src/encoding.ts +850 -0
  240. package/src/errors.ts +125 -0
  241. package/src/events.ts +451 -0
  242. package/src/evidence.ts +11 -2
  243. package/src/extract.ts +191 -308
  244. package/src/gemini/adapter.ts +127 -70
  245. package/src/gemini/chat.ts +28 -4
  246. package/src/gemini/endpoints.ts +12 -2
  247. package/src/gemini/errors.ts +34 -1
  248. package/src/gemini/generate.ts +42 -13
  249. package/src/gemini/index.ts +3 -3
  250. package/src/gemini/sse.ts +1 -1
  251. package/src/groups.ts +162 -0
  252. package/src/html.ts +1071 -0
  253. package/src/http.ts +376 -1
  254. package/src/index.ts +29 -6
  255. package/src/library.ts +498 -93
  256. package/src/limiter.ts +6 -1
  257. package/src/map-reduce.ts +252 -51
  258. package/src/model.ts +244 -4
  259. package/src/node/handlers.ts +196 -31
  260. package/src/node/index.ts +4 -0
  261. package/src/node/ingest-child.ts +61 -0
  262. package/src/node/isolated.ts +464 -0
  263. package/src/node/sse.ts +198 -0
  264. package/src/office.ts +1047 -0
  265. package/src/openai/adapter.ts +319 -38
  266. package/src/output-limit.ts +44 -8
  267. package/src/pause.ts +41 -0
  268. package/src/pdf-pages.ts +270 -20
  269. package/src/pdf-pictures.ts +56 -9
  270. package/src/pdf-work.ts +722 -0
  271. package/src/peers.ts +29 -0
  272. package/src/plan.ts +70 -12
  273. package/src/ranged.ts +4 -2
  274. package/src/retry.ts +49 -5
  275. package/src/search.ts +800 -155
  276. package/src/sse.ts +153 -28
  277. package/src/stores.ts +5 -2
  278. package/src/text-index.ts +30 -9
  279. package/src/text.ts +524 -23
  280. package/src/tools.ts +445 -46
  281. package/src/types.ts +108 -5
  282. package/src/word-styles.ts +420 -0
  283. package/src/xlsx.ts +478 -0
  284. package/src/xml-stream.ts +224 -0
  285. package/src/zip-directory.ts +136 -0
@@ -0,0 +1,1031 @@
1
+ # Tools
2
+
3
+ This page lists the tools the model gets for documents too large for its prompt and for the
4
+ pages of PDFs: their names, arguments, results and limits, and the tool exports of `llm-large-files`. Read it when you write a
5
+ loop of your own, show `steps` to users, keep `toolTurns`, or want to know exactly what the model
6
+ was told and what it read.
7
+
8
+ ```ts
9
+ import { MAX_VIEW_PAGES, MAX_WHOLE_DOCUMENT_READS, REGEX_BUDGET_MS, TOOL_NAMES, toolResultSummary } from "llm-large-files";
10
+ import type { JsonSchema, ToolCallLimits, ToolDeclaration, ToolFile, ToolFileLimits, ToolOutput, ToolResult, ToolSet } from "llm-large-files";
11
+ ```
12
+
13
+ Contents:
14
+
15
+ - [The tools](#the-tools) and [which tools are offered](#which-tools-are-offered)
16
+ - [Rules for every tool](#rules-for-every-tool)
17
+ - [`search_document`](#search_document), [`read_document`](#read_document),
18
+ [`get_document_overview`](#get_document_overview),
19
+ [`analyze_whole_document`](#analyze_whole_document), [`view_pages`](#view_pages)
20
+ - [The tool guide](#the-tool-guide)
21
+ - [What `askWithFiles` adds to results](#what-askwithfiles-adds-to-results)
22
+ - Exports: [`TOOL_NAMES`](#tool_names), [`MAX_VIEW_PAGES`](#max_view_pages),
23
+ [`MAX_WHOLE_DOCUMENT_READS`](#max_whole_document_reads), [`REGEX_BUDGET_MS`](#regex_budget_ms),
24
+ [toolResultSummary(label, result)](#toolresultsummarylabel-result), [`ToolSet`](#toolset),
25
+ [`ToolCallLimits`](#toolcalllimits), [`ToolFileLimits`](#toolfilelimits),
26
+ [`ToolOutput`](#tooloutput), [`ToolFile`](#toolfile), [`ToolResult`](#toolresult),
27
+ [`ToolDeclaration`](#tooldeclaration), [`JsonSchema`](#jsonschema)
28
+ - [Stability](#stability)
29
+
30
+ Related exports on other pages: [`LargeFiles`](./library.md#largefiles) (its `tools()` method),
31
+ [`ToolOptions`](./library.md#tooloptions), [`ToolCallHooks`](./library.md#toolcallhooks) (the
32
+ fourth argument of `executeWithFiles`), [`PreparedContext`](./library.md#preparedcontext) and
33
+ [`Budget`](./library.md#budget) are in the [library reference](./library.md). `ToolCall`,
34
+ `ToolAnswer`, `ToolTurn`, `evidenceOf`, `stubResult` and [`AskLimits`](./ask.md#asklimits) are in
35
+ the [loop reference](./ask.md).
36
+
37
+ ## The tools
38
+
39
+ | Tool | What it does |
40
+ |---|---|
41
+ | `search_document` | Finds lines that contain a text or match a regex, with an exact count, the first of them and the last one, the last ones, or a count for each group (hour, code, message shape); or the passages that match keywords best. In all of a file or in a range of its lines. |
42
+ | `read_document` | Reads a range of lines, numbered, up to 600 a call. |
43
+ | `get_document_overview` | The outline, the log profile, the first 30 and last 20 lines, and what a whole-file read would take. |
44
+ | `analyze_whole_document` | Reads the whole file in parts with other model calls (map-reduce) and answers a task that needs all of it. |
45
+ | `view_pages` | Up to 5 pages of a PDF, cut out of the uploaded file, as one small PDF the model looks at. |
46
+
47
+ The model finds a document's `document_id` on its card in the prompt, or in the
48
+ [tool guide](#the-tool-guide) for a PDF whose text is in the prompt. How the card, the tools and
49
+ the loop work together is in [How it works](../how-it-works.md).
50
+
51
+ ### Which tools are offered
52
+
53
+ In [askWithFiles(options)](./ask.md#askwithfilesoptions) (and `askGemini`, which runs it):
54
+
55
+ - No tool is offered when every document fits in the prompt and none has pages to look at. The
56
+ question is then one request.
57
+ - `search_document`, `read_document` and `get_document_overview` are offered when at least one
58
+ document is too large for the prompt (`plan.searchable`). They reach only those documents: a
59
+ document whose text is in the prompt is not searched.
60
+ - `analyze_whole_document` is offered with them when all of these hold: the model that explores
61
+ (the explorer, or else the model) has `generateText`, `wholeDocumentAnalysis` is not `false`, and
62
+ at least one searchable document is estimated, from its metadata, to need no more than
63
+ `maxMapChunks` parts (5% leeway).
64
+ - `view_pages` is offered for each PDF whose uploaded file is kept (`keepOriginals` includes
65
+ `"pdf"` and the store keeps originals), when `viewPages` is not `false` and every model of the
66
+ question can be sent a file (`capabilities.toolFiles` is not `"none"`). An OpenAI-compatible
67
+ model needs `pdfInput: true`; Claude with `toolFilePlacement: "none"` turns it off. It is also
68
+ offered for a PDF whose text is in the prompt; when no document is searchable, it is the only
69
+ tool. See [PDFs](../guides/pdfs.md).
70
+
71
+ With an [explorer model](../guides/explorer-model.md), the explorer gets the tools and makes the
72
+ calls; the answering model gets what they returned.
73
+
74
+ Through `files.tools(documents, options)` (a loop of your own, see [`ToolSet`](#toolset)):
75
+
76
+ - The three text tools are declared unless `documents` is empty and there are PDFs to look at.
77
+ With no documents and no PDFs they are still declared, and every call answers that no attached
78
+ file is read with this tool.
79
+ - `analyze_whole_document` is declared when `options.generate` is given and at least one of
80
+ `documents` is estimated within `maxMapChunks` parts (5% leeway). With `generate` and no such
81
+ document it is not declared, but a call of it is still answered, with the reason. Without
82
+ `generate`, pass `wholeDocumentAnalysis: false` to `prepare`: the tool guide then does not name
83
+ it, and the cards do not offer a whole-file read.
84
+ - `view_pages` is declared when `options.viewPages` names at least one PDF whose `originalKept` is
85
+ true and the store has `getOriginal`: `true` means every PDF in `documents`, and a list (such as
86
+ `prepared.viewable`) means exactly those.
87
+
88
+ ## Rules for every tool
89
+
90
+ **Arguments.** Integers may arrive as numbers or as numeric strings (`"12"`); fractions are cut to
91
+ whole numbers; a value that is neither a number nor a numeric string takes the default (an empty or
92
+ blank string counts as 0, so it is clamped to the minimum); line numbers and counts out of range
93
+ are clamped, not refused. Page numbers are the exception: `view_pages` refuses a page that is not a
94
+ number of 1 or more. A `mode` other than `"exact"` or `"regex"` is `"keywords"`, and
95
+ `case_sensitive` and `whole_word` are on only when they are `true`, `1`, or the same as text
96
+ (`"true"` in any case, or `"1"`), and off for any other value. Until 0.8.0, `case_sensitive: "true"`
97
+ was off. An `order` other than `"last"` is `"first"`. A `group_by` that is missing, `null`, `""` or `"none"` counts no groups; any other
98
+ value that is not one of its five is refused.
99
+
100
+ **Scope.** A call reaches only the documents its tool set was made for. A `document_id` outside
101
+ them is refused with the IDs the model may use, so that the next call is right (`view_pages`
102
+ refuses it with the reason instead, see [its errors](#view_pages)). The tool set does not know who
103
+ asks: it compares the owner stored with each document with the `owner` of the metadata it was
104
+ given, and answers a document whose stored owner is set and differs as one that is gone. A
105
+ document stored without an owner is read whatever the metadata holds: as everywhere, it is open to
106
+ anyone who has its ID. Make it from documents you looked up with the user's `owner`, as `prepare`
107
+ and `askWithFiles` do (see [Security](../security.md#tool-scoping)).
108
+
109
+ **Errors.** A call that fails returns `{ "error": "..." }` for the model to read, and the loop goes
110
+ on. It never names a server path: a Node system error becomes "The file could not be read from
111
+ storage (EIO). Try the call again.", and a store that could not be read gives the message of
112
+ [`DocumentStoreError`](./library.md#documentstoreerror), "The document store could not be read
113
+ (CODE). Try again." Errors every tool can return, unless a row names the tools:
114
+
115
+ | Error | When |
116
+ |---|---|
117
+ | `document_id is required.` | `document_id` is missing or blank. |
118
+ | `Unknown document_id ID. Use one of: doc_… ("app.log"), ….` | `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`: the ID is not one of the tool set's documents. Up to 20 usable IDs are listed, each with its file name, and `…` when there are more. |
119
+ | `Unknown document_id ID. No attached file is read with this tool in this conversation.` | `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`: the tool set has no document for these tools. |
120
+ | `ID is no longer available on the server. Ask the user to attach the file again.` | The document was deleted or expired after the request was planned. |
121
+ | `Unknown tool “NAME”.` | The name is not a tool of this tool set. The name is cut at 64 characters, never in the middle of a character. Until 0.6.0: `Unknown tool NAME.`, with the whole name. |
122
+
123
+ **Size.** One result is at most `toolResultCharacters` characters of JSON (a field of
124
+ [`Budget`](./library.md#budget): 24,000 with the default budget, less for a model with a small
125
+ window). The answer of `analyze_whole_document` may have up to the larger of that and 40,000.
126
+ `view_pages` returns a short JSON result and is limited in pages and bytes instead. A loop can
127
+ lower the cap of one call with [`ToolCallLimits.maxResultCharacters`](#toolcalllimits), never below
128
+ 600 characters. A result that is too large is cut, and says so:
129
+
130
+ - A list (the matches or passages of a search, the outline of an overview) loses entries from its
131
+ end, keeping at least one, and its `note` becomes "Showing K of N results to stay within the size
132
+ limit. Narrow the query or read specific lines." The note of a search that stopped early is kept
133
+ before it: "The scan stopped early; the total is a lower bound, and the last match in the file is
134
+ not known. Showing K of N results to stay within the size limit. Narrow the query or read
135
+ specific lines."
136
+ - An exact or regex search that read the whole file and has 2 matches or more is cut its own way:
137
+ it loses matches from the end of the first ones, and keeps the last one. With `order: "last"` it
138
+ loses the earliest of the last ones, and keeps the very last; with `group_by` it loses the
139
+ smallest groups, and keeps at least one, and by time the groups it keeps stay in time order (see
140
+ [`search_document`](#search_document)).
141
+ - When even one entry is too large, the result becomes
142
+ `{ "truncated": true, "partial": "<the start of the result as JSON text>", "note": "Result cut to the size limit. Ask for a smaller range." }`.
143
+ - `read_document` shortens its range instead, and `analyze_whole_document` cuts the end of its
144
+ answer. See their sections.
145
+
146
+ **Page numbers.** For a PDF whose pages `view_pages` can show in this tool set, search and read
147
+ results also carry page numbers, since a page number is what `view_pages` takes: `page` on a
148
+ matching line, `pages` (`"12"` or `"12-14"`) on a passage and on a read. They come from the
149
+ `--- Page N ---` lines the PDF extractor writes, counting up from 1. Other documents get no page
150
+ fields.
151
+
152
+ ## `search_document`
153
+
154
+ The description the model sees:
155
+
156
+ > Search a large attached file. mode "keywords" returns the most relevant passages for a question;
157
+ > "exact" finds the lines that contain the text; "regex" finds the lines that match a JavaScript
158
+ > regular expression (\w and \b know the letters of every language). For exact and regex,
159
+ > total_matching_lines counts every matching line, and the result shows the first max_results of
160
+ > them and the last one; search again from next_from_line for the ones after those shown, or with
161
+ > order "last" for the last ones. To count by group (per hour or day, per error code, per message
162
+ > shape), pass group_by: the result then counts each group, and shows no lines. from_line and
163
+ > to_line search part of the file. Results include line numbers to read further with read_document.
164
+
165
+ Until 0.6.0 it said that `exact` "returns every line containing the text", while the result showed
166
+ the first lines only: a model asked when something last happened named the last line it was shown.
167
+ 0.8.0 added `from_line`, `to_line`, `order`, `group_by` and `whole_word`, and the sentences that
168
+ name them.
169
+
170
+ | Argument | Type | Required | Default and limits | Meaning |
171
+ |---|---|---|---|---|
172
+ | `document_id` | string | yes | | The `document_id` from the file's card. |
173
+ | `query` | string | yes | Not blank (`query is required.`) | Keywords, exact text, or a regex, by `mode`. |
174
+ | `mode` | `"keywords"` \| `"exact"` \| `"regex"` | no | `"keywords"` | How to search. |
175
+ | `max_results` | integer | no | keywords: 5, from 1 to 10. exact and regex: 20, from 1 to 50. With `group_by`: 50 groups, from 1 to 200 | Passages, matching lines or groups returned. It does not limit the count. exact and regex also show the last matching line after them, when it is not among them. |
176
+ | `context_lines` | integer | no | 0, from 0 to 5 | exact and regex: lines shown before and after each match. None with `group_by`. |
177
+ | `case_sensitive` | boolean | no | `false` | exact and regex: match case. Keywords never do. |
178
+ | `from_line` | integer | no | 1. Past the last line: refused | Search only from this line on, in every mode. Pass a result's `next_from_line` to see the matches after those it shows. New in 0.8.0. |
179
+ | `to_line` | integer | no | The last line. Past it: the last line. Before `from_line`: refused | Search only up to this line, in every mode. New in 0.8.0. |
180
+ | `order` | `"first"` \| `"last"` | no | `"first"` | exact and regex: `"last"` shows the last `max_results` matching lines, in file order, in place of the first ones. New in 0.8.0. |
181
+ | `group_by` | `"capture"` \| `"minute"` \| `"hour"` \| `"day"` \| `"shape"` | no | none | exact and regex: count the matching lines by group, and show none ([below](#counts-by-group-group_by)). `"capture"` needs mode `regex`. `order` and `context_lines` are ignored with it. New in 0.8.0. |
182
+ | `whole_word` | boolean | no | `false` | exact only: match the text only as a whole word, not inside a longer one. Ignored in regex mode, where `\b` does it. New in 0.8.0. |
183
+
184
+ With `from_line` or `to_line`, the counts are of the lines searched, and the result says which in
185
+ `searched_lines` (`"1941-3000"`). In keywords mode, the passages ranked are those with at least one
186
+ line in the range, each with all its lines. The errors of these arguments:
187
+
188
+ | Error | When |
189
+ |---|---|
190
+ | `from_line 9000 is past the end of the file, which has 3000 lines.` | `from_line` is past the last line. |
191
+ | `to_line 20 is before from_line 100.` | `to_line` is before `from_line` (1 when it is not given). |
192
+ | `group_by counts the lines of an exact or regex search: use mode "exact" or "regex".` | `group_by` in keywords mode. |
193
+ | `group_by "capture" counts by the first group of a regex: use mode "regex" with a group, as in PAY-(\d+).` | `group_by: "capture"` in exact mode. |
194
+ | `group_by must be one of "capture", "minute", "hour", "day", "shape".` | Any other `group_by`. |
195
+
196
+ **`keywords`** ranks passages with BM25, computed at query time from substring hits: no index, any
197
+ language.
198
+
199
+ - The query is composed (NFC) and folded as exact search folds it (see below), then split at
200
+ anything that is not a letter, a mark, a digit or `_`, so a mark stays with its letter (Hindi,
201
+ Bengali, Tamil, Thai, pointed Hebrew). A run of Chinese, Japanese, Thai, Lao, Khmer or Myanmar
202
+ text is split into words with `Intl.Segmenter`. Terms of fewer than 2 characters (a Han or kana
203
+ word may have one) and common English and Hebrew words are dropped; at most 12 distinct terms are
204
+ kept. A term also matches inside longer words: "timeout" matches "timeouts".
205
+ - A Hebrew term loses its points. One that starts with prefix letters (ו ה ב כ ל מ ש: בחוזה,
206
+ והחוזה) also searches its stem without them (חוזה), up to three letters off while three are
207
+ left, so passages with the word as written rank first. One that ends in a final letter also
208
+ matches its plain form, as inside a longer word: כסף finds כספים. New in 0.8.0.
209
+ - A passage is a run of whole lines of about 1,500 characters, or 40 lines at most. A passage that
210
+ contains the whole query as a phrase (more than one term, up to 200 characters) is ranked higher.
211
+ - Passages come best first. `score` is rounded to two decimals and only orders the results.
212
+ - A query with no usable term, or with no hit, returns no passage and the note "No passage contains
213
+ these keywords. Try other words, synonyms, or exact mode."
214
+
215
+ **`exact`** finds every line that contains the text. A line counts once, however many times it
216
+ contains the text. A text with a line break matches across lines and counts on each line where it
217
+ starts, overlapping ones included: "a\na" counts 2 lines of "a\na\na" (1 until 0.8.0). The scan
218
+ stops after 1,000,000 matching lines, and with `whole_word` or `group_by` after 3 seconds. The query is composed (NFC), as the text is at ingest, so
219
+ "é" typed as "e" and an accent finds "é". Without `case_sensitive`, both sides are folded: lower
220
+ case, Greek final sigma as σ, curly quotes and primes as `'` and `"`, dashes and the minus sign as
221
+ `-`, no-break and thin spaces as a space, Hebrew geresh, gershayim and maqaf as `'`, `"` and `-`,
222
+ and full-width forms as ASCII. So "don't" finds "don’t", and צה"ל finds צה״ל (new in 0.8.0). With
223
+ `whole_word`, a match must not touch a letter, mark, digit or `_` of any script where the text
224
+ starts or ends with one.
225
+
226
+ **`regex`** tests a JavaScript regular expression against each line on its own, so a match cannot
227
+ span lines. It runs with the `i` flag unless `case_sensitive` is `true`, and with the `u` flag when
228
+ the pattern is valid with it (so `\p{L}` means a letter); a pattern valid only without it, such as
229
+ `[\w-.]`, runs without it. With the `u` flag, `\w`, `\W`, `\b` and `\B` know the letters, marks and
230
+ digits of every script: `\bחוזה\b` finds the word, and `\w+` does not stop at "é" (new in 0.8.0;
231
+ a pattern with `\W` inside a class is run as written). The pattern is composed (NFC) first. The
232
+ folds of exact search do not apply. Limits:
233
+
234
+ | Limit | Value | When it is reached |
235
+ |---|---|---|
236
+ | Pattern length | 300 characters | Refused: `Regex is longer than 300 characters.` |
237
+ | Nested quantifier, such as `(a+)+` | Refused up front, unless each repeat ends at a literal character that none of its quantified parts can match, as in `(\d{1,3}\.){3}\d{1,3}` or `(\w+\.)+com` (accepted since 0.8.0) | `Regex has a nested quantifier like (a+)+, which can hang on large files. Simplify it.` |
238
+ | Invalid pattern | | `Invalid regex: ` and the engine's message. |
239
+ | One search | 3,000 ms of matching, or what is left of the tool set's time when that is less | The scan stops; the result has the lines found so far and the note below. |
240
+ | A line the pattern cannot get past | Two whole slices of 250 ms on the same line | The scan stops there, the same way. |
241
+ | All regex searches of one tool set | [`REGEX_BUDGET_MS`](#regex_budget_ms), 6,000 ms | Each later regex search fails: `The time for regex searches is used up for this answer. Search with mode "exact" or "keywords" instead.` |
242
+
243
+ A regex scan runs in slices of 250 ms with other work of the server in between, and the regex
244
+ searches of one tool set run one after another, even when the calls of a turn run together. See
245
+ [Security](../security.md) for why.
246
+
247
+ Results of `exact` and `regex`:
248
+
249
+ | Field | Type | Meaning |
250
+ |---|---|---|
251
+ | `document` | string | The file name. |
252
+ | `mode` | `"exact"` \| `"regex"` | The mode that ran. |
253
+ | `searched_lines` | string, optional | With `from_line` or `to_line`: the lines searched, as `"1941-3000"`. The counts are of those lines. Keyword results have it too. New in 0.8.0. |
254
+ | `total_matching_lines` | number | Every matching line in the file (or in `searched_lines`), not only those shown. This is the number to count with. |
255
+ | `order` | `"last"`, optional | With `order: "last"` ([below](#the-last-matches-order-last)). |
256
+ | `note` | string, optional | When more lines match than are shown: "N of the M matching lines are shown: the first K and the last (line L). To see the ones in between, search again with from_line F, or count them with group_by." ("the first and the last" when K is 1). Until 0.8.0 it ended "narrow the search." When the scan stopped (1,000,000 lines, 3 seconds of a `whole_word` or `group_by` search, or a regex limit): "The scan stopped early; the total is a lower bound, and the last match in the file is not known." Or a size-limit note (below). Absent when every match is shown. |
257
+ | `next_from_line` | number, optional | When matches between the first ones and the last one are not shown: the line after the last of the first ones (F above). A search with `from_line` set to it shows the ones after. Absent when every match is shown, when the scan stopped early, with `order: "last"` and with `group_by`. New in 0.8.0. |
258
+ | `matches` | array | The first `max_results` matching lines, in file order, then the last matching line of the file when it is not among them. A scan that stopped early shows no last line, since it did not reach the end. With `order: "last"`, the last `max_results` matching lines, in file order. |
259
+ | `matches[].line` | number | The line number. |
260
+ | `matches[].page` | number, optional | The page of the line, for a PDF whose pages can be shown. |
261
+ | `matches[].text` | string | The line. A line longer than 400 characters is cut to 400 around the match, with `…` where it was cut. |
262
+ | `matches[].before`, `matches[].after` | string[], optional | With `context_lines` above 0: the lines around the match, as `"1202\| text"`, each cut to 400 characters. |
263
+
264
+ A search with `max_results: 1` and `context_lines: 1`:
265
+
266
+ ```json
267
+ {
268
+ "document": "app.log",
269
+ "mode": "exact",
270
+ "total_matching_lines": 51,
271
+ "note": "2 of the 51 matching lines are shown: the first and the last (line 4987). To see the ones in between, search again with from_line 1204, or count them with group_by.",
272
+ "next_from_line": 1204,
273
+ "matches": [
274
+ {
275
+ "line": 1203,
276
+ "text": "2026-05-01 12:00:03 ERROR payment-service: timeout after 1203ms calling ledger",
277
+ "before": ["1202| 2026-05-01 12:00:02 INFO request 1202 ok"],
278
+ "after": ["1204| 2026-05-01 12:00:04 INFO request 1204 ok"]
279
+ },
280
+ {
281
+ "line": 4987,
282
+ "text": "2026-05-01 13:22:47 ERROR payment-service: timeout after 4987ms calling ledger",
283
+ "before": ["4986| 2026-05-01 13:22:46 INFO request 4986 ok"],
284
+ "after": ["4988| 2026-05-01 13:22:48 INFO request 4988 ok"]
285
+ }
286
+ ]
287
+ }
288
+ ```
289
+
290
+ When the result is too large for its room, an exact or regex search that read the whole file and
291
+ has 2 matches or more loses matches from the end of the first ones, and keeps the last one. Its
292
+ note is then one of:
293
+
294
+ - "Showing N of the M matching lines to stay within the size limit: the first K and the last (line
295
+ L). To see the ones in between, search again with from_line F, or count them with group_by."
296
+ (N is K + 1; "the first and the last" when K is 1), with `next_from_line` F;
297
+ - "Showing 1 of the M matching lines to stay within the size limit: the last (line L). Narrow the
298
+ query or read specific lines.", when only the last one fits;
299
+ - "Showing 1 of the M matching lines: the last (line L). Narrow the search.", when the last one
300
+ fits only with this shorter note.
301
+
302
+ When not even that fits, the result is cut as text, as any other (see
303
+ [Size](#rules-for-every-tool)), and its `partial` holds the note before the match. A note that starts with
304
+ "Showing " marks a result that was cut: a call made again runs again. A result with the note "N of
305
+ the M matching lines are shown" is whole, and a call made again gets a pointer to it.
306
+
307
+ ### The last matches: `order: "last"`
308
+
309
+ New in 0.8.0. With `order: "last"`, an exact or regex search shows the last `max_results` matching
310
+ lines, in file order, and no others: unlike a search in the default order, it adds no match from
311
+ the other end of the file. When not every match is shown, the note names the line of the last
312
+ match and of the first. A search with `max_results: 2`:
313
+
314
+ ```json
315
+ {
316
+ "document": "app.log",
317
+ "mode": "exact",
318
+ "total_matching_lines": 51,
319
+ "order": "last",
320
+ "note": "These are the last 2 of the 51 matching lines, in the order of the file: the last match is on line 4987, and the first on line 1203.",
321
+ "matches": [
322
+ { "line": 4410, "text": "2026-05-01 13:12:30 ERROR payment-service: timeout after 4410ms calling ledger" },
323
+ { "line": 4987, "text": "2026-05-01 13:22:47 ERROR payment-service: timeout after 4987ms calling ledger" }
324
+ ]
325
+ }
326
+ ```
327
+
328
+ To fit its room, it loses the earliest of them first, and keeps the very last. The note is then
329
+ "Showing the last N of the M matching lines to stay within the size limit, in the order of the
330
+ file: the last match is on line L, and the first on line F. Narrow the query or read specific
331
+ lines."
332
+
333
+ When the scan stopped early (1,000,000 lines, 3 seconds of a `whole_word` or `group_by` search, or a regex limit), the lines shown are the last of
334
+ those it found, and the line of the first match is not named. The note is then "The scan stopped
335
+ early; the total is a lower bound. These are the last N matching lines it found, in the order of
336
+ the file. There may be later ones, after line L: search again with from_line F." (F is L + 1;
337
+ " and to_line T" follows it when the call gave `to_line`, T being the last line searched). Cut to
338
+ its room, the second sentence is "Showing the last N matching lines it found to stay within the
339
+ size limit, in the order of the file." When L is the last line searched, the last sentence is left
340
+ out. When the scan found no matching line before it stopped (a regex out of time), the note is
341
+ "The scan stopped early, out of time, before it found a matching line: the lines it did not reach
342
+ may have some. Try a simpler pattern, mode "exact", or fewer lines with from_line and to_line."
343
+ Like every note that starts with "The scan stopped early", it marks a result that a call made
344
+ again runs again: a later question has time of its own.
345
+
346
+ ### Counts by group: `group_by`
347
+
348
+ New in 0.8.0. With `group_by`, an exact or regex search counts its matching lines by group, and
349
+ shows no line. One call answers "how many errors per hour" or "which codes, how often":
350
+
351
+ | `group_by` | The key of a line |
352
+ |---|---|
353
+ | `"capture"` | The text of the regex's first group: `PAY-(\d+)` counts each code. A line where that group took no part has no key. Regex mode only. |
354
+ | `"minute"`, `"hour"`, `"day"` | The first timestamp on the line, as written there (an offset such as `+02:00` or `Z` is not applied): `"2026-11-10 14:03"`, `"2026-11-10 14:00"`, `"2026-11-10"`. Read: ISO 8601 (`2026-11-10T14:03:17Z`, `2026-11-10 14:03:17,123`), nginx's error log (`2026/11/10 14:03:17`), the access logs of Apache and nginx (`10/Nov/2026:14:03:17 +0000`), epoch times in a JSON field `time`, `ts`, `timestamp` or `@timestamp` (10 or 13 digits, as UTC), `10.11.2026 14:03` (day first), `10/11/2026 14:03` (kept as `"10/11/2026"`, since day and month cannot be told apart), syslog (`Nov 10 14:03:17`) and klog (`I1110 14:03:17.123456`), the last two without a year (`"11-10 14:00"`). A line without one has no key. |
355
+ | `"shape"` | The line with its timestamp taken out, and its UUIDs, IP addresses, hex, quoted values and numbers replaced by `<id>`, `<ip>`, `<hex>`, `<s>` and `<n>`, as the log profile groups problems. |
356
+
357
+ Keys are cut at 120 characters. At most 10,000 groups are counted one by one; the lines of further
358
+ groups are counted together in `other_count`. Results, besides `document`, `mode`, `searched_lines`
359
+ and `total_matching_lines`:
360
+
361
+ | Field | Type | Meaning |
362
+ |---|---|---|
363
+ | `group_by` | string | The grouping that ran. |
364
+ | `total_groups` | number | The groups counted, at most 10,000. |
365
+ | `other_count` | number, optional | Matching lines of groups past the first 10,000, with `other_note`: "Lines of groups past the first 10,000 found." |
366
+ | `lines_without_group` | number, optional | Matching lines with no key: no timestamp, or a regex group that took no part. |
367
+ | `note` | string, optional | When not every group is shown: "K of the N groups are shown, the largest first. Narrow the search, or use from_line and to_line, for the others." By time, "the largest, in time order". A result cut to its room says "Showing K of the N groups to stay within the size limit, …". After the note of a scan that stopped early, when there is one. |
368
+ | `groups` | array | Up to `max_results` groups (50 by default, 200 at most). By `capture` and `shape`, the largest first. By time, in time order; when there are more groups than are shown, the largest of them, in time order. A result cut to its room keeps the largest groups as well: by `capture` and `shape` it loses the end of its list, and by time the smallest groups, keeping the others in time order. |
369
+ | `groups[].key` | string | The group. |
370
+ | `groups[].count` | number | Its matching lines. |
371
+ | `groups[].first_line`, `groups[].last_line` | number | Its first and last matching line, to read with `read_document`. |
372
+
373
+ A search for `PAY-5031` with `group_by: "hour"`:
374
+
375
+ ```json
376
+ {
377
+ "document": "payments.log",
378
+ "mode": "exact",
379
+ "total_matching_lines": 214,
380
+ "group_by": "hour",
381
+ "total_groups": 3,
382
+ "groups": [
383
+ { "key": "2026-11-10 13:00", "count": 9, "first_line": 40211, "last_line": 41876 },
384
+ { "key": "2026-11-10 14:00", "count": 187, "first_line": 42003, "last_line": 47950 },
385
+ { "key": "2026-11-10 15:00", "count": 18, "first_line": 48012, "last_line": 48990 }
386
+ ]
387
+ }
388
+ ```
389
+
390
+ Its step reads `Search payments.log (exact) for “PAY-5031”, counted by hour — 214 matching lines in
391
+ 3 groups`.
392
+
393
+ Results of `keywords`:
394
+
395
+ | Field | Type | Meaning |
396
+ |---|---|---|
397
+ | `document` | string | The file name. |
398
+ | `mode` | `"keywords"` | |
399
+ | `searched_lines` | string, optional | With `from_line` or `to_line`: the lines searched, as `"1941-3000"`. New in 0.8.0. |
400
+ | `passages` | array | The best passages, best first. |
401
+ | `passages[].lines` | string | `"412-436"`: the passage's first and last line. |
402
+ | `passages[].pages` | string, optional | `"37"` or `"37-38"`, for a PDF whose pages can be shown. |
403
+ | `passages[].score` | number | The BM25 score, rounded to two decimals. |
404
+ | `passages[].text` | string | The passage's lines, numbered (`"412\| text"`), one a line. |
405
+ | `note` | string, optional | The no-passage note, or the size-limit note. |
406
+
407
+ ## `read_document`
408
+
409
+ The description the model sees:
410
+
411
+ > Read a range of lines from a large attached file, with line numbers. Up to 600 lines per call;
412
+ > long results are cut and tell you where to continue.
413
+
414
+ | Argument | Type | Required | Default and limits | Meaning |
415
+ |---|---|---|---|---|
416
+ | `document_id` | string | yes | | The document to read. |
417
+ | `start_line` | integer | yes | Clamped to 1 through the last line; 1 when it is missing | First line to read, 1-based. |
418
+ | `end_line` | integer | no | `start_line + 199`; clamped to `start_line` through `start_line + 599` and the last line | Last line to read. |
419
+
420
+ The numbered text must fit in the cap less 500 characters, as JSON. When it does not, the range is
421
+ shortened (to 70% of its length, step by step) until it fits, and `next_start_line` says where to
422
+ go on. When one line alone is longer than that, its text is cut to half of that room and
423
+ `text_cut` is `true`.
424
+
425
+ | Field | Type | Meaning |
426
+ |---|---|---|
427
+ | `document` | string | The file name. |
428
+ | `lines` | string | `"1200-1399"`: the lines shown, which may be fewer than asked. |
429
+ | `pages` | string, optional | The pages those lines are on, for a PDF whose pages can be shown. |
430
+ | `total_lines` | number | The lines of the whole file. |
431
+ | `text` | string | The lines, numbered (`"1200\| text"`), one a line. |
432
+ | `text_cut` | `true`, optional | The one line shown was cut: its end is missing. |
433
+ | `next_start_line` | number, optional | The first line not shown, when the file goes on. |
434
+ | `note` | `"End of file."`, optional | Instead of `next_start_line`, when the last line shown is the file's last. |
435
+
436
+ ```json
437
+ { "document": "app.log", "lines": "1200-1399", "total_lines": 5000, "text": "1200| ...\n1201| ...", "next_start_line": 1400 }
438
+ ```
439
+
440
+ ## `get_document_overview`
441
+
442
+ The description the model sees:
443
+
444
+ > Get the full outline (headings with line numbers), log profile (levels, time range, most
445
+ > frequent errors), and the first and last lines of a large attached file.
446
+
447
+ | Argument | Type | Required | Meaning |
448
+ |---|---|---|---|
449
+ | `document_id` | string | yes | The document. |
450
+
451
+ | Field | Type | Meaning |
452
+ |---|---|---|
453
+ | `document` | string | The file name. |
454
+ | `format` | string | The [`DocumentFormat`](./library.md#documentformat): `"log"`, `"pdf"`, `"csv"`, and so on. |
455
+ | `size` | string | The size of the uploaded file: `"512 B"`, `"340 KB"`, `"61.2 MB"`. |
456
+ | `lines` | number | The lines of the extracted text. |
457
+ | `estimated_tokens` | number | The library's token estimate of the text. |
458
+ | `pages` | number, optional | The page count of a PDF, or the slide count of a PPTX. |
459
+ | `parts_for_whole_document_analysis` | number | About how many parts a whole-file read would take, estimated from the metadata, to within a part or two. The read itself counts them. |
460
+ | `whole_document_analysis` | string, optional | "Not available for this file: it would take about N parts, and the limit is M. Use search_document and read_document." when the estimate is above `maxMapChunks`. |
461
+ | `log_profile` | [`LogProfile`](./library.md#logprofile), optional | For a log: the count of each level, the first and last timestamps, and up to 15 problems (WARN, ERROR and FATAL message shapes: `template`, `level`, `count`, `firstLine`), FATAL first, then ERROR, then WARN, the most frequent first within each. |
462
+ | `first_lines` | string | The first 30 lines, numbered. |
463
+ | `last_lines` | string | The last 20 lines, numbered; `""` when the file has 30 lines or fewer. |
464
+ | `outline` | string[] | One entry a heading: `"L12 Title"`, with two more spaces before the title for each level below the shallowest heading (`"L40 Install"`). It is the outline stored at ingest: at most 400 headings. When a file has more, the outline keeps its top-level headings alone if there are 20 or more of them, and all its headings otherwise. With 20 to 400 top-level headings, each one is kept once, in order. More than 400 are cut to 400, taken at even steps across the file (see [`OutlineEntry`](./library.md#outlineentry)). Cut from its end to fit the cap. |
465
+
466
+ ## `analyze_whole_document`
467
+
468
+ The description the model sees:
469
+
470
+ > Read an entire large file in parts and answer a task that needs all of it, such as a full
471
+ > summary, a timeline, or items that must be judged by meaning. To count or list lines that
472
+ > contain a word, an ID or a pattern, use search_document in exact or regex mode instead: its
473
+ > count is exact, and this can miss items. Slow and costly: prefer search_document and
474
+ > read_document for specific questions.
475
+
476
+ | Argument | Type | Required | Meaning |
477
+ |---|---|---|---|
478
+ | `document_id` | string | yes | The document. |
479
+ | `task` | string | yes | What to find or produce from the whole file, stated completely. Not blank (`task is required.`). |
480
+
481
+ The file is split into parts of up to `mapChunkTokens` tokens (and 80% of `maxRequestBytes`), 4 are
482
+ read at a time with the `generate` function of the tool set, their notes are merged, and one answer
483
+ is written from them. A part the model refuses as too large is read as two halves, while
484
+ `maxMapChunks` and [`ToolCallLimits.maxRequests`](#toolcalllimits) allow it. The steps, output
485
+ limits and retries are in [How it works](../how-it-works.md); what a read costs is in
486
+ [Costs and limits](../guides/costs-and-limits.md).
487
+
488
+ Each document may be read whole [`MAX_WHOLE_DOCUMENT_READS`](#max_whole_document_reads) times (1)
489
+ by one tool set, which is one question in `askWithFiles`. Set it with `limits.wholeDocumentReads`
490
+ in [`AskLimits`](./ask.md#asklimits), or `maxWholeDocumentReads` in
491
+ [`ToolOptions`](./library.md#tooloptions). A read is counted when it starts, so two calls in one
492
+ turn do not both pass.
493
+
494
+ | Field | Type | Meaning |
495
+ |---|---|---|
496
+ | `document` | string | The file name. |
497
+ | `parts_read` | number | The parts the file was read in, those that could not be read included; a part read as two halves counts as two. |
498
+ | `parts_with_relevant_content` | number | The parts that were read and held something for the task. |
499
+ | `parts_that_failed` | number, optional | Parts that could not be read. The notes name each one: "[This part could not be read: reason]". |
500
+ | `notes_cut_short` | number, optional | Notes of parts, and merges of notes, that stopped at the output limit or were cut off by the provider. |
501
+ | `answer_cut_short` | `true`, optional | The final answer stopped at its output limit. |
502
+ | `notes_only` | `true`, optional | The last step, which writes one answer from the notes, failed: `answer` is the notes of the parts, with their line citations. |
503
+ | `note` | string, optional | With `notes_only`: "The last step of this read, which writes one answer from the notes of the parts, failed (REASON). The answer is those notes: answer from them.", plus "Some of them were cut off at the output limit, so they are incomplete: tell the user." when some were. Otherwise, when notes or the answer were cut short: "Incomplete: the model's output limit was reached, so some relevant items are missing from this answer. Tell the user it is incomplete. For a complete list or an exact count, use search_document in exact or regex mode (it returns total_matching_lines), or ask about a narrower part of the file." |
504
+ | `answer` | string | The answer. When no part held anything relevant: "No part of NAME contained anything relevant to: TASK", with the count of parts that could not be read, if any. |
505
+ | `answer_shortened` | string, optional | "The rest of this answer did not fit in the room left in this conversation." The answer was cut at its end, which then reads `…`. |
506
+
507
+ ```json
508
+ { "document": "novel.txt", "parts_read": 4, "parts_with_relevant_content": 3, "answer": "Ada appears first in chapter 2 (L412) ..." }
509
+ ```
510
+
511
+ Errors:
512
+
513
+ | Error | When | Counted as a read |
514
+ |---|---|---|
515
+ | `NAME was already read whole for this question, and is not read whole again. Use the answer of that read, or search_document and read_document for the details.` | The document was read whole as often as it may be (" N times" is added when that is more than 1). | no |
516
+ | `Whole-file reads are turned off for this question. Use search_document and read_document on NAME.` | `maxWholeDocumentReads` (or `limits.wholeDocumentReads`) is 0. | no |
517
+ | `The whole-file read of NAME failed earlier in this question (REASON), and is not tried again. Use search_document and read_document.` | An earlier read of it could read no part. | no |
518
+ | `Reading all of NAME needs N model calls, over the limit of M. Use search_document to narrow the question first.` | The read counted more parts than `maxMapChunks`. Nothing was sent. | no |
519
+ | `Could not read any part of NAME: REASON` | Every part failed at the model. | yes |
520
+ | The store's error | No part could be read from the store. Nothing was sent. | no |
521
+
522
+ REASON, in these errors, in the notes of a part that could not be read, and in the note of
523
+ `notes_only`, says why a model request failed. Since 0.6.0 it is built from the error's kind, its
524
+ HTTP status or its connection code, never from its message, which can name a project, a host, a
525
+ key or a URL. It is one of:
526
+
527
+ | REASON | For an error |
528
+ |---|---|
529
+ | `The request was larger than the model takes (HTTP 400).` | of kind `context_overflow` |
530
+ | `The model's quota has run out (HTTP 429).` | of kind `quota` |
531
+ | `The model was busy (HTTP 429).` | of kind `rate_limit` |
532
+ | `The model took too long to answer.` | of kind `timeout`; named `TimeoutError`, on the error or its cause (an `AbortSignal.timeout`, or a built-in part reader past its `timeoutMs`); or with undici's code `UND_ERR_HEADERS_TIMEOUT` or `UND_ERR_BODY_TIMEOUT` |
533
+ | `The model could not be reached.` | of kind `unreachable`; or whose code, or that of a cause up to 3 levels down, is that of a connection that failed: `ECONNREFUSED`, `ECONNRESET`, `ECONNABORTED`, `ENOTFOUND`, `EAI_AGAIN`, `ETIMEDOUT`, `EHOSTUNREACH`, `ENETUNREACH`, `ENETDOWN`, `EPIPE`, `UND_ERR_CONNECT_TIMEOUT`, `UND_ERR_SOCKET`, `UND_ERR_CLOSED`, `UND_ERR_DESTROYED` |
534
+ | `The model failed on its side (HTTP 503).` | of kind `server` |
535
+ | `The model refused the key or token it was sent (HTTP 401).` | of kind `auth` |
536
+ | `The model refused the request (HTTP 400).` | of kind `invalid` |
537
+ | `The model's reply could not be read (HTTP 200).` | of kind `invalid` with a status from 200 to 299: a reply that was not JSON, or a streamed event too long |
538
+ | `The model returned no text (SAFETY).` or `The model returned no text.` | the error a built-in part reader throws for a reply with no text. The provider's reason is given only when it is a plain word (letters and `_`, up to 40). |
539
+ | `The model request failed.` | any other: a plain `Error` or a string thrown by a `generate` of your own, an error whose fields throw when read. `The model request failed (HTTP 302).` for a status from 100 to 399. |
540
+
541
+ The status in brackets is there when the error has one: its `status`, else `statusCode`, else
542
+ `response.status`, as the errors of provider SDKs, got and axios carry it, when that is a whole
543
+ number from 100 to 599, or 0 (which is not shown). An error with a status and no `kind` has its kind
544
+ told from the status and its message, as [`classifyModelError`](./errors-and-retries.md#classifymodelerrorstatus-message-signals)
545
+ does. A failure to read a part's lines from the store is told as every tool tells a store's error
546
+ ([Rules for every tool](#rules-for-every-tool)):
547
+ `[This part could not be read: The document store could not be read (EIO). Try again.]`. Until
548
+ 0.6.0, REASON was the error's own message.
549
+
550
+ ## `view_pages`
551
+
552
+ The description the model sees:
553
+
554
+ > Look at pages of an attached PDF as they are printed. Use it when the answer depends on
555
+ > something the text of the file cannot show: a picture, photo, chart, diagram, map, handwriting,
556
+ > a stamp or signature, or how a page is laid out. It is not for reading text. Up to 5 pages per
557
+ > call.
558
+
559
+ | Argument | Type | Required | Default and limits | Meaning |
560
+ |---|---|---|---|---|
561
+ | `document_id` | string | yes | | The `document_id` of the PDF. |
562
+ | `from_page` | integer | yes | 1 or more; not clamped | First page, 1-based: the N of the `--- Page N ---` lines in the text. |
563
+ | `to_page` | integer | no | `from_page`. A page past the end is taken as the last page | Last page. One call shows at most [`MAX_VIEW_PAGES`](#max_view_pages) (5) pages, fewer when the call's `maxPages` is lower. |
564
+
565
+ The pages are cut out of the kept original into one small PDF, and returned as a file next to a
566
+ short JSON result. Only [`ToolSet.executeWithFiles`](#toolset) returns the file; `execute` answers
567
+ with an error. Pages are left off the end until the file fits the call's `maxFileBytes`. Cutting
568
+ needs the optional dependency `@cantoo/pdf-lib`. How pages are cut and counted, and what they cost,
569
+ is in [PDFs](../guides/pdfs.md).
570
+
571
+ | Field | Type | Meaning |
572
+ |---|---|---|
573
+ | `document` | string | The file name. |
574
+ | `pages` | string | The pages shown: `"12"` or `"12-14"`. |
575
+ | `total_pages` | number | The pages of the PDF. |
576
+ | `file` | string | The name of the file returned with the result: `"pages-12-14.pdf"`. |
577
+ | `next_page` | number, optional | The first page asked for and not shown, when it can still be looked at. |
578
+ | `note` | string, optional | Why fewer pages were shown than asked (see below). |
579
+
580
+ When fewer pages were shown than asked, the note is one of:
581
+
582
+ - "Showing S of the W pages asked for: the others did not fit in this request. Call view_pages from
583
+ page N if you need them." (with `next_page`)
584
+ - "Showing S of the W pages asked for: one call shows at most 5. Call view_pages from page N if you
585
+ need them." (with `next_page`)
586
+ - "Showing S of the W pages asked for: no more pages can be looked at for this question." (no
587
+ `next_page`: the call's `maxPages` was the limit)
588
+
589
+ The file is `{ name: "pages-12-14.pdf", mimeType: "application/pdf", bytes }` (a
590
+ [`ToolFile`](#toolfile)).
591
+
592
+ ```json
593
+ { "document": "report.pdf", "pages": "2-3", "total_pages": 6, "file": "pages-2-3.pdf" }
594
+ ```
595
+
596
+ Errors, besides those of [every tool](#rules-for-every-tool):
597
+
598
+ | Error | When |
599
+ |---|---|
600
+ | `Unknown document_id ID. Use the document_id of an attached PDF.` | The ID is not a PDF this tool set can show. |
601
+ | `The pages of NAME cannot be shown: only its text was kept. Answer from the text.` | A document the text tools can read whose pages cannot be shown: not a PDF, or its original is not kept. |
602
+ | `from_page must be a page number: 1 or higher.` (or `to_page`) | Not a number, or below 1. |
603
+ | `to_page (T) is before from_page (F).` | |
604
+ | `NAME has N pages. There is no page F.` | `from_page` is past the last page. |
605
+ | `No more pages can be looked at for this question. Answer from the pages and the text you already have.` | The call's `maxPages` is below 1. |
606
+ | `There is no room left in this request to show more pages. Answer from the pages and the text you already have.` | The call's `maxFileBytes` is below 1,024. |
607
+ | `The original file of NAME is no longer on the server, so its pages cannot be shown. Answer from the text.` | The store no longer has the original. |
608
+ | `NAME: Page F is 2.1 MB as a file, more than the 1.5 MB that fit in this request.` | The first page alone does not fit `maxFileBytes`. |
609
+ | `NAME: ` and the reason | The file cannot be cut: it needs a password, is damaged, declares too many objects to cut in memory, has another page count than its text, has no page `from_page` (when its page count was not recorded), or `@cantoo/pdf-lib` is not installed. |
610
+ | `The pages could not be shown: this application does not pass files from tools to the model. Answer from the text.` | The call was run with `execute`, which has no place for a file. |
611
+
612
+ ## The tool guide
613
+
614
+ When any document is searchable or has pages to look at, the planned request carries a tool guide
615
+ for the system instruction (`PreparedContext.instruction`; `askWithFiles` adds it, and a loop of
616
+ your own adds it to its system prompt). In short, it tells the model:
617
+
618
+ - which files are too large for its context, by name (quoted, so a name cannot read as an
619
+ instruction) and `document_id`, and that it sees only a card of each;
620
+ - what each text tool is for, and this rule: "To count or list what can be matched as text (a word,
621
+ an ID, an error code, a log level, a pattern), use search_document in exact or regex mode: its
622
+ total_matching_lines is exact.";
623
+ - since 0.8.0, how to count by group and see the rest of a list: "To count per group (per hour or
624
+ day, per error code or ID with a regex group, per message shape), pass group_by: one call gives
625
+ every count. For more matches than a result shows, search again from its next_from_line, or with
626
+ order "last" for the last ones.";
627
+ - since 0.8.0, what to search for: "Search for the bare form of a word: a search matches inside
628
+ longer words, so "timeout" also finds "timeouts", and a Hebrew word without its prefix letters
629
+ (חוזה, not בחוזה) finds every form of it.";
630
+ - to read around search hits before relying on them;
631
+ - to use `analyze_whole_document` only when the task needs all of the file and cannot be done by
632
+ matching text (a full summary, a timeline, items judged by meaning), and that it is slow, costly,
633
+ and can miss items of a long list. This line is left out when `prepare` gets
634
+ `wholeDocumentAnalysis: false` (`askWithFiles` passes it when its own `wholeDocumentAnalysis` is
635
+ `false` or the model that explores has no `generateText`), or when no searchable file can be read
636
+ whole;
637
+ - that its tool turns are limited: call several tools in one turn when they do not depend on each
638
+ other, and answer as soon as it has enough evidence;
639
+ - to base the answer on text it read, cite line numbers like `(app.log L1203)`, and say so when the
640
+ tools did not find something.
641
+
642
+ For PDFs whose pages can be shown, it names each one with its `document_id` and page count, and
643
+ says to use `view_pages` when the answer depends on a picture, chart, diagram, map, handwriting, a
644
+ stamp or signature, or the layout of a page; that a page is worth a look when its text says it has
645
+ a picture, has no text, or mentions a figure; that page numbers are the N of the `--- Page N ---`
646
+ lines; not to read text with it; and not to describe a picture it has not looked at.
647
+
648
+ The counting rule is also in the description of `analyze_whole_document`, and in the note of a
649
+ whole-file read that came out incomplete.
650
+
651
+ ## What `askWithFiles` adds to results
652
+
653
+ The tools above answer one call. The loop of [askWithFiles(options)](./ask.md#askwithfilesoptions)
654
+ adds to their results, and answers some calls without running them. A loop of your own does none
655
+ of this unless you write it.
656
+
657
+ - **`tool_turns_left`.** Every result the loop sends has this field when it is first sent: the tool
658
+ turns left after this one, or 0 when the request has no room left. A stand-in that later
659
+ replaces the result, and the stand-in kept in `toolTurns` for a look at pages, do not have it.
660
+ `toolTurns` keep it, but the rounds of an earlier question passed back in `messages` are sent
661
+ without it, since it counted the turns of their own question (since 0.6.0). `geminiToolTurns`,
662
+ `openAiToolTurns` and `anthropicToolTurns` leave it out too.
663
+ - **A call made again.** A call whose result is still in the conversation in full (the same
664
+ search with the same arguments as the tool reads them, the same overview, the same read, or a
665
+ read of lines inside a read shown in full), in this question or in the tool rounds of an earlier
666
+ one passed back in `messages`, is not run (new in 0.6.0). Its result is a pointer, and its step
667
+ ends " — already shown above":
668
+ - `{ "document": "app.log", "already_shown": true, "note": "Not run: this exact call was answered earlier in this conversation, and its result is still shown above in full. The file has not changed, so it would return the same. Use that result. For more, read other lines, or search with other words or another mode." }`
669
+ - For a read inside an earlier read: `{ "document": "app.log", "lines": "150-200", "already_shown": true, "note": "Not run: lines 150-200 are already shown above in full, in the result that has lines 100-299 of this file, and the file has not changed. Use them. To see more, read other lines." }`
670
+ - For the same call twice in one turn: `{ "already_shown": true, "note": "Not run: call 1 of this turn is the same call, and its result is next to this one. Use it." }`
671
+
672
+ A result of an earlier turn that failed, was cut (`truncated`, `text_cut`, a note that starts
673
+ "Showing "), came from a search that stopped early, or is itself a pointer or a stand-in is never
674
+ pointed to: the call runs, and may get more. A search whose note only says how many of its
675
+ matches are shown ("21 of the 442 matching lines are shown: …") was not cut, and is pointed to.
676
+ The same call made twice in one turn gets the pointer to the first whatever the first returns (an
677
+ error, a cut result, a pointer); only when the first is refused for lack of room is the second
678
+ refused the same way. Whole-file reads and looks at pages are never pointed to.
679
+
680
+ Some calls run again all the same:
681
+ - every call, with a model that declares
682
+ [`clearsToolResults`](./ask.md#modelcapabilities) (Claude with `clear_tool_uses` or compaction
683
+ in its `body`), which may no longer have the earlier result; the same call twice in one turn
684
+ runs twice too;
685
+ - a read of one line from the tool rounds of an earlier question: 0.5.0 cut the text of such a
686
+ read without saying so (no `text_cut`), and the stored rounds do not tell which version wrote
687
+ them;
688
+ - an exact or regex search that 0.5.0 stored: fewer `matches` than `total_matching_lines`, and
689
+ no note. Run now, it shows the last match too.
690
+ - **Stand-ins.** When the request runs short of room, the results of older tool rounds are replaced
691
+ by the short stand-ins of [`stubResult`](./ask.md#stubresultcall-result-hadfiles), with
692
+ `"dropped": true`; when and how is in [How it works](../how-it-works.md), and nothing is replaced
693
+ when any model of the question is append-only (Claude). A pointer whose result was replaced
694
+ becomes
695
+ `{ "document": "app.log", "dropped": true, "note": "Not run then: it repeated an earlier call whose result was later dropped from the conversation to make room. Call it again if you need it." }`.
696
+ So does every pointer of a stored round when a model of the question declares
697
+ `clearsToolResults`. In `toolTurns`, the result of a look at pages is kept as its stand-in,
698
+ since the file is not kept.
699
+ - **A hint after two failed searches.** When two `search_document` calls in a row fail in the
700
+ same mode, the error of the second (and of any after it) ends with a hint. For `regex`: "Two
701
+ regex searches in a row failed. Try mode "exact" with a plain word or ID from the file, or mode
702
+ "keywords"." For the other modes: "This search failed twice in a row. Check the arguments against
703
+ the tool's description, or answer from what you have." A call of another tool, or a search that
704
+ works, starts the count again.
705
+ - **A share of the room.** Before the calls of a turn run, the room left in the request is shared
706
+ among those that run and return text, and each gets its share as `maxResultCharacters`. A
707
+ whole-file read is the exception: its answer is not cut up front; once the other results of the
708
+ turn are in, it is cut to the room they left. A look at pages is not in the share: it gets
709
+ `maxFileBytes` and `maxPages` instead (see [`ToolCallLimits`](#toolcalllimits)). How the room
710
+ is shared is in [How it works](../how-it-works.md#room-in-the-request).
711
+ - **Calls that are not run**, each answered with an `error`:
712
+ - past `limits.callsPerTurn` (8): "Not run: one turn runs at most 8 tool calls. Send this call
713
+ again if you still need it.";
714
+ - when there is not enough room left for the call: "Not run: there is no room left in this
715
+ conversation for another tool result. Answer now, from what the tools returned so far.";
716
+ - a call whose arguments did not arrive whole (`ToolCall.problem`, set by the adapter), such as
717
+ "The arguments of this call were not valid JSON, and it was not run: … Send the call again.",
718
+ which quotes up to 300 characters of the arguments, never cut in the middle of a character;
719
+ - a whole-file read that would pass `limits.inputTokens` or `limits.requests`: "Not run: reading
720
+ all of NAME takes about N input tokens, and M are left of the L this question may use. Use
721
+ search_document and read_document." (or "model requests ... this question may make").
722
+ - **Steps.** Each call that runs starts an `onActivity` line, its `describe()` label and `…`.
723
+ Each call up to `callsPerTurn` ends with a step, in `onStep` and `steps`: the label and
724
+ [toolResultSummary(label, result)](#toolresultsummarylabel-result), or the label and
725
+ " — already shown above" for a pointer. The calls past it share one step: "N more tool calls in
726
+ this turn were not run (at most 8 a turn)", or "1 more tool call in this turn was not run (at
727
+ most 8 a turn)" for one. A call of a tool that does not exist has the label `Call “NAME”`, the
728
+ name cut at 64 characters, and the step `Call “do_magic” — failed: Unknown tool “do_magic”.`;
729
+ until 0.6.0 the label was the name itself. Labels and steps are one line of plain text: a line
730
+ break or a control character the model sent becomes a space, the invisible characters that file
731
+ text loses are dropped, and `](` becomes `] (` (see [`AskResult`](./ask.md#askresult)). See
732
+ [Web app](../guides/web-app.md) for showing them.
733
+
734
+ The regex time of a question is always [`REGEX_BUDGET_MS`](#regex_budget_ms) in `askWithFiles`: it
735
+ has no option for it.
736
+
737
+ ## Exports
738
+
739
+ ### `TOOL_NAMES`
740
+
741
+ The names of the five tools, as constants.
742
+
743
+ ```ts
744
+ const TOOL_NAMES = {
745
+ search: "search_document",
746
+ read: "read_document",
747
+ overview: "get_document_overview",
748
+ analyze: "analyze_whole_document",
749
+ view: "view_pages",
750
+ } as const;
751
+ ```
752
+
753
+ Compare a `ToolCall.name` with these rather than with string literals. See
754
+ [Your own adapter and loop](../guides/own-adapter-and-loop.md).
755
+
756
+ ### `MAX_VIEW_PAGES`
757
+
758
+ `5`. The most pages one `view_pages` call shows. [`ToolCallLimits.maxPages`](#toolcalllimits)
759
+ can only lower it. See [PDFs](../guides/pdfs.md).
760
+
761
+ ### `MAX_WHOLE_DOCUMENT_READS`
762
+
763
+ `1`. How many times one tool set reads each document whole, unless `maxWholeDocumentReads` in
764
+ [`ToolOptions`](./library.md#tooloptions) (or `limits.wholeDocumentReads` in
765
+ [`AskLimits`](./ask.md#asklimits)) says otherwise. One read of a large file is up to
766
+ `maxMapChunks` part requests, plus the requests that merge their notes and the one that writes its
767
+ answer. See [Costs and limits](../guides/costs-and-limits.md).
768
+
769
+ ### `REGEX_BUDGET_MS`
770
+
771
+ `6_000`. The milliseconds the regex searches of one tool set may take together, unless
772
+ `regexBudgetMs` in [`ToolOptions`](./library.md#tooloptions) says otherwise. One search still stops
773
+ after 3,000 ms of matching, and after two 250 ms slices that cannot get past the same line (see
774
+ [the regex limits](#search_document)). See [Security](../security.md).
775
+
776
+ ### toolResultSummary(label, result)
777
+
778
+ `toolResultSummary(label: string, result: ToolResult): string`
779
+
780
+ What a finished call found, as a suffix for its `describe()` label in a list of steps. It never
781
+ throws.
782
+
783
+ | The result has | The suffix |
784
+ |---|---|
785
+ | `error` | ` — failed:` and the error, as in ` — failed: Unknown tool “do_magic”.` |
786
+ | `total_matching_lines` (exact, regex) | ` — 1,203 matching lines` (numbers in en-US form), ` — 1 matching line` for one. With `total_groups` (`group_by`): ` — 214 matching lines in 3 groups`, ` in 1 group` for one |
787
+ | `passages` (keywords) | ` — 5 passages`, ` — 1 passage` for one |
788
+ | `parts_read` (whole-file read) | ` — 9 parts, 3 relevant` (` — 1 part, 1 relevant` for one), then `, notes only` with `notes_only`, or `, incomplete` with `notes_cut_short` or `answer_cut_short` |
789
+ | `lines` and `total_lines` (read) | ` — got 1200-1342` when the label does not end with the lines shown, else `""` |
790
+ | `pages` and `total_pages` (view_pages) | ` — got page 2` or ` — got pages 2-3` when the label does not end with the pages shown, else `""` |
791
+ | anything else (an overview, a cut result) | `""` |
792
+
793
+ ```ts
794
+ import { toolResultSummary } from "llm-large-files";
795
+
796
+ const label = "Search app.log (exact) for “timeout”";
797
+ const step = label + toolResultSummary(label, { document: "app.log", mode: "exact", total_matching_lines: 1203, matches: [] });
798
+ // "Search app.log (exact) for “timeout” — 1,203 matching lines"
799
+ ```
800
+
801
+ Until 0.6.0 a count of one was plural too (` — 1 matching lines`). `askWithFiles` makes its steps
802
+ this way, and then makes each one line of plain text. See [Web app](../guides/web-app.md).
803
+
804
+ ### `ToolSet`
805
+
806
+ The tools of one question, scoped to a set of documents. You get one from `files.tools(documents,
807
+ options)` (see [`LargeFiles`](./library.md#largefiles) and [`ToolOptions`](./library.md#tooloptions));
808
+ `askWithFiles` makes one per question. Make one per question in your own loop too: the regex time
809
+ and the whole-file reads are counted per tool set.
810
+
811
+ ```ts
812
+ type ToolSet = {
813
+ declarations: ToolDeclaration[];
814
+ has(name: string): boolean;
815
+ execute(name: string, args: Record<string, unknown>): Promise<ToolResult>;
816
+ executeWithFiles(name: string, args: Record<string, unknown>, limits?: ToolCallLimits, hooks?: ToolCallHooks): Promise<ToolOutput>;
817
+ describe(name: string, args: Record<string, unknown>): string;
818
+ resultCharacters(name: string): number;
819
+ };
820
+ ```
821
+
822
+ | Member | What it does |
823
+ |---|---|
824
+ | `declarations` | The tools to declare to the model, in this order: `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`, `view_pages`, those that are [offered](#which-tools-are-offered). |
825
+ | `has(name)` | `true` for each declared tool. Also `true` for `analyze_whole_document` when `generate` was given and the text tools are declared, even if it is not declared because no document is small enough: a call then gets the reason. Use it to tell the calls of this tool set from others. |
826
+ | `execute(name, args)` | Runs a call and returns its JSON result, within the tool's own cap. Failures come back as `{ error }`, as listed above. `view_pages` answers with an error here: it returns a file. |
827
+ | `executeWithFiles(name, args, limits?, hooks?)` | Runs any call with [`ToolCallLimits`](#toolcalllimits), and returns the result with the files it produced (`files` is empty for every tool but `view_pages`). Use it in a loop that can send files to the model. `hooks` ([`ToolCallHooks`](./library.md#toolcallhooks), new in 0.7.0) are listeners for this call alone: the progress of a whole-file read, in place of `ToolOptions.onProgress`, and each of its model requests with their retries. Only `analyze_whole_document` calls them. |
828
+ | `describe(name, args)` | A short line for a progress UI, read from the arguments the way the tool reads them: `Search app.log (exact) for “timeout”` (the query cut to 80 characters; since 0.8.0 followed by ` in lines 1941-end` with `from_line` or `to_line`, and by `, counted by hour` with `group_by` or `, the last ones` with `order: "last"` in exact and regex mode), `Read app.log lines 1200-1260` (`-1260` only when `end_line` is a number not below `start_line`), `Overview of app.log`, `Read all of novel.txt for “list every character”` (the task cut to 100 characters), `Look at report.pdf pages 2-3`. A cut never splits an emoji. A file not yet known is "the file"; an unknown tool is its name, as it came (`askWithFiles` labels it `Call “NAME”` instead). The line quotes the model's words as they are: show it as plain text, or clean it as `askWithFiles` does its steps. In `askWithFiles`, this line, made one line of plain text, is the `label` of the call's `tool.start` event, the line `onActivity` gets with "…", and the start of its step. |
829
+ | `resultCharacters(name)` | The most characters of a result of this tool: the larger of `toolResultCharacters` and 40,000 for `analyze_whole_document`, 600 for `view_pages` (its file is counted in bytes), and `toolResultCharacters` for any other name. What a loop sets aside in its request before it runs a call. |
830
+
831
+ `execute` and `executeWithFiles` throw only when the tool set's `signal` was aborted: the error
832
+ that ended the call is thrown again instead of being returned to the model.
833
+
834
+ A turn of your own loop, run with the tool set. `files` is your
835
+ [`LargeFiles`](./library.md#largefiles), created once with
836
+ [createLargeFiles(options)](./library.md#createlargefilesoptions):
837
+
838
+ ```ts
839
+ import { DocumentsNotFoundError, toolResultSummary } from "llm-large-files";
840
+ import type { ToolAnswer, ToolCall } from "llm-large-files";
841
+
842
+ const documentIds = ["doc_0123456789abcdef0123456789abcdef"];
843
+ // The size of everything else in the request: system prompt, messages.
844
+ const base = { tokens: 2_000, bytes: 8_000 };
845
+ const prepared = await files.prepare(documentIds, base, {
846
+ owner: "user-42",
847
+ viewPages: true,
848
+ // The tool set below has no generate. This keeps the instruction from naming
849
+ // analyze_whole_document, and the cards from offering a whole-file read.
850
+ wholeDocumentAnalysis: false,
851
+ });
852
+ // A document that is gone (expired, or another owner's) is left out of the plan: say so, as askWithFiles does.
853
+ if (prepared.missing.length > 0) throw new DocumentsNotFoundError(prepared.missing);
854
+ const tools = files.tools(prepared.plan.searchable, { viewPages: prepared.viewable });
855
+ // Send prepared.instruction with your system prompt, and tools.declarations as the tools.
856
+
857
+ async function runCalls(calls: ToolCall[], roomCharacters: number, roomBytes: number): Promise<ToolAnswer[]> {
858
+ const answers: ToolAnswer[] = [];
859
+ for (const call of calls) {
860
+ // Arguments that did not arrive whole: tell the model, and do not run the call.
861
+ if (call.problem) {
862
+ answers.push({ call, result: { error: call.problem } });
863
+ continue;
864
+ }
865
+ const label = tools.has(call.name) ? tools.describe(call.name, call.args) : call.name;
866
+ const { result, files: pages } = await tools.executeWithFiles(call.name, call.args, {
867
+ maxResultCharacters: roomCharacters,
868
+ maxFileBytes: Math.floor(roomBytes * 3 / 4), // base64 turns three bytes into four
869
+ });
870
+ console.log(`${label}${toolResultSummary(label, result)}`);
871
+ answers.push({ call, result, files: pages });
872
+ }
873
+ return answers;
874
+ }
875
+ ```
876
+
877
+ Send each answer back in your provider's form, its files with it. The whole loop, with sizing,
878
+ limits and each provider's converters, is in
879
+ [Your own adapter and loop](../guides/own-adapter-and-loop.md).
880
+
881
+ ### `ToolCallLimits`
882
+
883
+ What one call may use of the request its result will be sent in: the third argument of
884
+ `executeWithFiles`. Every field is optional.
885
+
886
+ ```ts
887
+ type ToolCallLimits = {
888
+ maxFileBytes?: number;
889
+ maxPages?: number;
890
+ maxResultCharacters?: number;
891
+ maxRequests?: number;
892
+ };
893
+ ```
894
+
895
+ | Field | Type | Default | What it does |
896
+ |---|---|---|---|
897
+ | `maxFileBytes` | number | 3/8 of (`maxRequestBytes` − 262,144): 3,651,696 bytes with the default budget | `view_pages`: the most bytes its file may have. Pages are left off the end until the cut fits. Below 1,024 the call fails. Give the room left in your request, times 3/4 when you send files as base64. |
898
+ | `maxPages` | number | [`MAX_VIEW_PAGES`](#max_view_pages) (5) | `view_pages`: the most pages this call may show, never more than 5. Below 1 the call fails. Lower it as the pages already shown use up your token budget. |
899
+ | `maxResultCharacters` | number | The tool's own cap ([`resultCharacters`](#toolset)) | The most characters of the JSON result, when that is less than the tool's cap: the room left in the request. The result is cut to it and says where to go on. Never below 600. `view_pages` ignores it. |
900
+ | `maxRequests` | number | No limit | `analyze_whole_document`: the most model requests the read may make. It does not stop the read: a part the model refuses as too large is read as two halves only while this and `maxMapChunks` allow it (the parts, a merge and the answer must fit). New in 0.6.0. |
901
+
902
+ In `askWithFiles`, `maxFileBytes` is what is left of `maxRequestBytes` after the request so far
903
+ and a tenth kept free (256 KiB at least), times 3/4; `maxPages` is what is left of
904
+ `reservedTokens / 2 / pageTokens` pages for the question (60 with a `reservedTokens` of 64,000 and
905
+ Gemini's 530 tokens a page); `maxRequests`, when `limits.requests` is set, is the read's own
906
+ requests and a share of what the question has left of it. See
907
+ [Your own adapter and loop](../guides/own-adapter-and-loop.md).
908
+
909
+ ### `ToolFileLimits`
910
+
911
+ ```ts
912
+ type ToolFileLimits = ToolCallLimits;
913
+ ```
914
+
915
+ The name of [`ToolCallLimits`](#toolcalllimits) before 0.5.0, kept so that older code compiles.
916
+ Use `ToolCallLimits`.
917
+
918
+ ### `ToolOutput`
919
+
920
+ ```ts
921
+ type ToolOutput = { result: ToolResult; files: ToolFile[] };
922
+ ```
923
+
924
+ What `executeWithFiles` returns: a call's JSON result and the files that belong to it.
925
+
926
+ | Field | Type | Meaning |
927
+ |---|---|---|
928
+ | `result` | [`ToolResult`](#toolresult) | The JSON result, or `{ error }` for a call that failed. |
929
+ | `files` | [`ToolFile`](#toolfile)`[]` | The files of the result. Empty for every tool but `view_pages`, and for a `view_pages` call that failed. |
930
+
931
+ See [Your own adapter and loop](../guides/own-adapter-and-loop.md).
932
+
933
+ ### `ToolFile`
934
+
935
+ ```ts
936
+ type ToolFile = {
937
+ name: string;
938
+ mimeType: string;
939
+ bytes: Uint8Array;
940
+ };
941
+ ```
942
+
943
+ | Field | Type | Meaning |
944
+ |---|---|---|
945
+ | `name` | string | A short ASCII name, unique among the files of one result: `"pages-12-14.pdf"`. The result's `file` field names it. |
946
+ | `mimeType` | string | `"application/pdf"` for `view_pages`. |
947
+ | `bytes` | `Uint8Array` | The file. Send it to the model in your provider's form; it is not kept in `toolTurns`. |
948
+
949
+ See [PDFs](../guides/pdfs.md).
950
+
951
+ ### `ToolResult`
952
+
953
+ ```ts
954
+ type ToolResult = Record<string, unknown>;
955
+ ```
956
+
957
+ A tool's JSON result: an object whose fields are listed for each tool above, or `{ error: string }`
958
+ for a call that failed. Send it to the model as it is. To read one in code, test for the fields
959
+ (`error`, `already_shown`, `dropped`, `truncated`, `total_matching_lines`) rather than the wording
960
+ of notes. See [Your own adapter and loop](../guides/own-adapter-and-loop.md).
961
+
962
+ ### `ToolDeclaration`
963
+
964
+ ```ts
965
+ type ToolDeclaration = {
966
+ name: string;
967
+ description: string;
968
+ parameters: JsonSchema & { type: "object" };
969
+ };
970
+ ```
971
+
972
+ A tool as it is declared to the model. Every provider takes this form, or one made from it by its
973
+ adapter (`toGeminiTools` in the [Gemini reference](./gemini.md)).
974
+
975
+ | Field | Type | Meaning |
976
+ |---|---|---|
977
+ | `name` | string | The tool's name: for the tools of a tool set, one of [`TOOL_NAMES`](#tool_names). |
978
+ | `description` | string | The description the model sees, quoted in the tool's section above. |
979
+ | `parameters` | [`JsonSchema`](#jsonschema) with `type: "object"` | The arguments: `properties` and `required`, as in the tool's argument table. |
980
+
981
+ See [Your own adapter and loop](../guides/own-adapter-and-loop.md).
982
+
983
+ ### `JsonSchema`
984
+
985
+ ```ts
986
+ type JsonSchema = {
987
+ type: "string" | "integer" | "number" | "boolean" | "object" | "array";
988
+ description?: string;
989
+ enum?: string[];
990
+ properties?: Record<string, JsonSchema>;
991
+ required?: string[];
992
+ items?: JsonSchema;
993
+ };
994
+ ```
995
+
996
+ The subset of JSON Schema that Gemini, OpenAI and Anthropic function declarations all accept, and
997
+ the only one the tools use.
998
+
999
+ | Field | Type | Meaning |
1000
+ |---|---|---|
1001
+ | `type` | `"string"` \| `"integer"` \| `"number"` \| `"boolean"` \| `"object"` \| `"array"` | The JSON type of the value. |
1002
+ | `description` | string, optional | What the value is, for the model. |
1003
+ | `enum` | string[], optional | The values allowed, such as `mode`'s `"keywords"`, `"exact"` and `"regex"`. |
1004
+ | `properties` | `Record<string, JsonSchema>`, optional | For an object: its fields. |
1005
+ | `required` | string[], optional | For an object: the fields that must be given. |
1006
+ | `items` | `JsonSchema`, optional | For an array: the schema of each item. |
1007
+
1008
+ See [Your own adapter and loop](../guides/own-adapter-and-loop.md).
1009
+
1010
+ ## Stability
1011
+
1012
+ [Publishing](../publishing.md) counts the tool names, their arguments and their result fields as
1013
+ public API, like the documented exports and the HTTP responses of `llm-large-files/node`: changing them is
1014
+ a breaking change, and the [changelog](../../CHANGELOG.md) names it. While the version is `0.x`, a
1015
+ minor version (0.7.0) may make breaking changes. Descriptions, notes, error texts and the tool
1016
+ guide are written for the model and have changed between versions: do not parse them; test for
1017
+ result fields instead.
1018
+
1019
+ 0.6.0 changed the guide, the descriptions of `search_document` and `analyze_whole_document`, and
1020
+ the frame of every file sent whole ([`PreparedContext`](./library.md#preparedcontext)), so the
1021
+ provider's prompt cache misses once after upgrading to it: the first request of each conversation
1022
+ after the upgrade reads from the cache at most what comes before the first text that changed. For
1023
+ a question with files to search, that is nothing where the tools come first (Claude,
1024
+ OpenAI-compatible endpoints), and at most your own system text on Gemini, where the guide follows
1025
+ it in the system instruction. For a question whose files are all sent whole, it is your own system
1026
+ text, and `view_pages` with its guide when the pages of a PDF can be looked at.
1027
+
1028
+ 0.8.0 changed the description of `search_document` (its new arguments), the guide (two lines: counts
1029
+ by group and paging; searching the bare form of a word) and the closing requests
1030
+ ([`FINAL_ANSWER_REQUEST`](./ask.md#final_answer_request) and the two others), so the cache misses
1031
+ once after upgrading to it too, the same way.