llm-large-files 0.8.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +981 -0
- package/README.md +22 -17
- package/dist/access.d.ts +128 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +234 -0
- package/dist/access.js.map +1 -0
- package/dist/aggregate.d.ts +6 -0
- package/dist/aggregate.d.ts.map +1 -1
- package/dist/aggregate.js +86 -13
- package/dist/aggregate.js.map +1 -1
- package/dist/analyze.d.ts +4 -0
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +2 -2
- package/dist/analyze.js.map +1 -1
- package/dist/answer-info.d.ts +26 -0
- package/dist/answer-info.d.ts.map +1 -0
- package/dist/answer-info.js +32 -0
- package/dist/answer-info.js.map +1 -0
- package/dist/anthropic/adapter.d.ts +5 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +35 -10
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +159 -14
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +743 -113
- package/dist/ask.js.map +1 -1
- package/dist/cache.d.ts +12 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +18 -5
- package/dist/cache.js.map +1 -1
- package/dist/citations.d.ts +36 -0
- package/dist/citations.d.ts.map +1 -0
- package/dist/citations.js +291 -0
- package/dist/citations.js.map +1 -0
- package/dist/encoding.js +5 -3
- package/dist/encoding.js.map +1 -1
- package/dist/error-info.d.ts +70 -0
- package/dist/error-info.d.ts.map +1 -0
- package/dist/error-info.js +253 -0
- package/dist/error-info.js.map +1 -0
- package/dist/errors.d.ts +74 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +104 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +57 -13
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +42 -11
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts +4 -2
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +35 -5
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +32 -8
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +52 -36
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +54 -7
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +152 -41
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +38 -53
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +20 -25
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +59 -11
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +33 -7
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/errors.d.ts +39 -5
- package/dist/gemini/errors.d.ts.map +1 -1
- package/dist/gemini/errors.js +70 -9
- package/dist/gemini/errors.js.map +1 -1
- package/dist/gemini/generate.d.ts +53 -2
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +100 -50
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +4 -4
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/http.d.ts +41 -3
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +40 -2
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +13 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -5
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +78 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +893 -0
- package/dist/ingest.js.map +1 -0
- package/dist/library.d.ts +295 -33
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +458 -650
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +176 -10
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +807 -61
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +79 -6
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +80 -4
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +247 -14
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +491 -227
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +3 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +7 -1
- package/dist/node/ingest-child.d.ts.map +1 -1
- package/dist/node/ingest-child.js +19 -4
- package/dist/node/ingest-child.js.map +1 -1
- package/dist/node/isolated.d.ts +3 -1
- package/dist/node/isolated.d.ts.map +1 -1
- package/dist/node/isolated.js +11 -4
- package/dist/node/isolated.js.map +1 -1
- package/dist/node/originals.d.ts +19 -0
- package/dist/node/originals.d.ts.map +1 -0
- package/dist/node/originals.js +82 -0
- package/dist/node/originals.js.map +1 -0
- package/dist/node/spool.d.ts +106 -0
- package/dist/node/spool.d.ts.map +1 -0
- package/dist/node/spool.js +378 -0
- package/dist/node/spool.js.map +1 -0
- package/dist/node/sse.d.ts +6 -4
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +9 -7
- package/dist/node/sse.js.map +1 -1
- package/dist/openai/adapter.d.ts +5 -1
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +53 -12
- package/dist/openai/adapter.js.map +1 -1
- package/dist/output-limit.d.ts +2 -1
- package/dist/output-limit.d.ts.map +1 -1
- package/dist/output-limit.js +5 -2
- package/dist/output-limit.js.map +1 -1
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +71 -12
- package/dist/pdf-layout.js.map +1 -1
- package/dist/pdf-outline.d.ts +71 -0
- package/dist/pdf-outline.d.ts.map +1 -0
- package/dist/pdf-outline.js +414 -0
- package/dist/pdf-outline.js.map +1 -0
- package/dist/plan.d.ts +137 -7
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +248 -26
- package/dist/plan.js.map +1 -1
- package/dist/prepare.d.ts +50 -0
- package/dist/prepare.d.ts.map +1 -0
- package/dist/prepare.js +207 -0
- package/dist/prepare.js.map +1 -0
- package/dist/ranged.d.ts +16 -11
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +56 -12
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +3 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +6 -2
- package/dist/retry.js.map +1 -1
- package/dist/scope.d.ts +25 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +60 -0
- package/dist/scope.js.map +1 -0
- package/dist/search.d.ts +7 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +72 -10
- package/dist/search.js.map +1 -1
- package/dist/source.d.ts +60 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +170 -0
- package/dist/source.js.map +1 -0
- package/dist/stores.d.ts +126 -31
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +655 -141
- package/dist/stores.js.map +1 -1
- package/dist/testing/fake-fetch.d.ts +31 -0
- package/dist/testing/fake-fetch.d.ts.map +1 -0
- package/dist/testing/fake-fetch.js +66 -0
- package/dist/testing/fake-fetch.js.map +1 -0
- package/dist/testing/fake-model.d.ts +38 -0
- package/dist/testing/fake-model.d.ts.map +1 -0
- package/dist/testing/fake-model.js +62 -0
- package/dist/testing/fake-model.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/store-checks.d.ts +72 -0
- package/dist/testing/store-checks.d.ts.map +1 -0
- package/dist/testing/store-checks.js +619 -0
- package/dist/testing/store-checks.js.map +1 -0
- package/dist/tools.d.ts +47 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +440 -71
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +165 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/whole-cost.d.ts +37 -0
- package/dist/whole-cost.d.ts.map +1 -0
- package/dist/whole-cost.js +46 -0
- package/dist/whole-cost.js.map +1 -0
- package/docs/README.md +21 -8
- package/docs/getting-started.md +18 -13
- package/docs/guides/anthropic.md +17 -10
- package/docs/guides/conversations.md +112 -47
- package/docs/guides/costs-and-limits.md +147 -42
- package/docs/guides/deployment.md +380 -86
- package/docs/guides/errors-and-reliability.md +317 -67
- package/docs/guides/explorer-model.md +4 -1
- package/docs/guides/openai-compatible.md +47 -7
- package/docs/guides/own-adapter-and-loop.md +60 -16
- package/docs/guides/pdfs.md +219 -36
- package/docs/guides/storage.md +676 -145
- package/docs/guides/testing.md +149 -129
- package/docs/guides/vertex-and-gemini.md +169 -57
- package/docs/guides/web-app.md +318 -80
- package/docs/how-it-works.md +155 -30
- package/docs/measurements.md +215 -13
- package/docs/publishing.md +185 -37
- package/docs/reference/anthropic.md +12 -7
- package/docs/reference/ask.md +415 -47
- package/docs/reference/errors-and-retries.md +194 -14
- package/docs/reference/events.md +77 -27
- package/docs/reference/gemini.md +252 -87
- package/docs/reference/library.md +886 -178
- package/docs/reference/node.md +659 -111
- package/docs/reference/openai.md +21 -10
- package/docs/reference/testing.md +311 -0
- package/docs/reference/tools.md +285 -30
- package/docs/security.md +404 -78
- package/docs/troubleshooting.md +151 -70
- package/docs/upgrading-0.10.md +472 -0
- package/docs/upgrading-0.9.md +890 -0
- package/package.json +16 -3
- package/src/access.ts +267 -0
- package/src/aggregate.ts +92 -14
- package/src/analyze.ts +2 -2
- package/src/answer-info.ts +45 -0
- package/src/anthropic/adapter.ts +40 -11
- package/src/ask.ts +866 -115
- package/src/cache.ts +20 -5
- package/src/citations.ts +294 -0
- package/src/encoding.ts +6 -4
- package/src/error-info.ts +290 -0
- package/src/errors.ts +131 -15
- package/src/events.ts +112 -24
- package/src/evidence.ts +35 -5
- package/src/extract.ts +56 -34
- package/src/gemini/adapter.ts +196 -43
- package/src/gemini/chat.ts +63 -78
- package/src/gemini/endpoints.ts +89 -16
- package/src/gemini/errors.ts +74 -11
- package/src/gemini/generate.ts +147 -57
- package/src/gemini/index.ts +4 -4
- package/src/http.ts +65 -4
- package/src/index.ts +13 -8
- package/src/ingest.ts +958 -0
- package/src/library.ts +711 -651
- package/src/map-reduce.ts +858 -70
- package/src/model.ts +113 -9
- package/src/node/handlers.ts +709 -221
- package/src/node/index.ts +14 -2
- package/src/node/ingest-child.ts +24 -6
- package/src/node/isolated.ts +11 -4
- package/src/node/originals.ts +78 -0
- package/src/node/spool.ts +379 -0
- package/src/node/sse.ts +9 -7
- package/src/openai/adapter.ts +56 -13
- package/src/output-limit.ts +5 -2
- package/src/pdf-layout.ts +69 -11
- package/src/pdf-outline.ts +400 -0
- package/src/plan.ts +287 -36
- package/src/prepare.ts +253 -0
- package/src/ranged.ts +48 -14
- package/src/retry.ts +6 -2
- package/src/scope.ts +63 -0
- package/src/search.ts +69 -13
- package/src/source.ts +178 -0
- package/src/stores.ts +702 -142
- package/src/testing/fake-fetch.ts +80 -0
- package/src/testing/fake-model.ts +78 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/store-checks.ts +660 -0
- package/src/tools.ts +461 -73
- package/src/types.ts +151 -14
- package/src/whole-cost.ts +64 -0
|
@@ -52,8 +52,9 @@ export async function ask(userId: string, documentIds: string[], question: strin
|
|
|
52
52
|
toolTurns: 8, // at most 8 chat requests, the answer included, plus one when the answer comes back without text
|
|
53
53
|
callsPerTurn: 6, // at most 6 tool calls run in one turn
|
|
54
54
|
wholeDocumentReads: 1, // each document is read whole once at most (the default)
|
|
55
|
-
inputTokens: 1_500_000, // no more tools once the
|
|
55
|
+
inputTokens: 1_500_000, // no more tools once the next request and the answer would pass this many input tokens
|
|
56
56
|
requests: 30, // chat requests and part reads together
|
|
57
|
+
wholeReadsPerQuestion: 2, // at most 2 whole-file reads in all (the default)
|
|
57
58
|
},
|
|
58
59
|
});
|
|
59
60
|
console.log(result.turns, result.usage);
|
|
@@ -66,7 +67,8 @@ export async function ask(userId: string, documentIds: string[], question: strin
|
|
|
66
67
|
| `toolTurns` | 12 | Chat requests of the question: the answer and the second tries after an empty or broken turn included. The second try of the answer itself, when it came back without text, can be one request past it. | The last request must be answered in text (`toolChoice: "none"`; Gemini gets no tools declared, the other adapters keep them listed). |
|
|
67
68
|
| `callsPerTurn` | 8 | Tool calls of one model turn that are run. | Each call past it is answered "Not run: one turn runs at most 8 tool calls. Send this call again if you still need it." |
|
|
68
69
|
| `wholeDocumentReads` | 1 | Whole-file reads of each document. | A read past it is answered with an error that tells the model to use the answer it already has. 0 refuses every read. |
|
|
69
|
-
| `
|
|
70
|
+
| `wholeReadsPerQuestion` | 2 | Whole-file reads of the question, over all its documents. New in 0.10.0. | A read past it is refused: "Not run: one question makes at most 2 whole-file reads, and this one has made them. Use search_document and read_document on app.log." |
|
|
71
|
+
| `inputTokens` | [the default ceiling](#the-default-ceiling) | Input tokens of the question: every chat request and part read, of every model, by the provider's count, or by the library's estimate when a request reports none. | When the next tool request and the answer would pass it, no more tools run: the next request is the answer. A whole-file read that would pass it is refused before it starts. |
|
|
70
72
|
| `requests` | none | Answered requests of the question: chat requests and part reads together. | Once one request is left, no more tools run, and that request is the answer. A whole-file read that would pass it is refused before it starts. |
|
|
71
73
|
|
|
72
74
|
**No limit throws.** A question that reaches a limit ends with a request for the answer, written
|
|
@@ -81,38 +83,78 @@ true when the reply has text and was not refused.
|
|
|
81
83
|
the model was told not to make. The note "Your last reply had no text. No tool can be called in
|
|
82
84
|
this reply: write the answer now, in text, from what is above." goes at the end of that one
|
|
83
85
|
request. It is one of the two second tries a question may make, and it can be one request past
|
|
84
|
-
`toolTurns`. It is not made after a refusal, a filter
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
`toolTurns`. It is not made after a refusal, a filter, another reason that asking again does
|
|
87
|
+
not mend (`finish: "other"`, such as Gemini's `OTHER` or `LANGUAGE`) or a reply cut off at the
|
|
88
|
+
output limit with nothing in it, nor when it would pass `requests` or the ceiling of input
|
|
89
|
+
tokens: it is sent only while a request is left of `requests`, and while what the question spent
|
|
90
|
+
and that request, as the loop estimates it, stay within the ceiling. With `toolTurns: 8`, a
|
|
91
|
+
question makes at most 9 answered chat requests; to cap them exactly, set `requests`.
|
|
88
92
|
- Each tool result carries `tool_turns_left`, so the model can see its turns run out and stop
|
|
89
93
|
searching in time. The results of an earlier question, passed back in `messages`, are sent
|
|
90
94
|
without it ([Conversations](./conversations.md#what-toolturns-holds)).
|
|
91
95
|
- A model that answers in text before a limit is reached ends the question there.
|
|
92
|
-
- The result does not say that a limit ended the question, and `steps` has no line for it.
|
|
93
|
-
`
|
|
94
|
-
|
|
96
|
+
- The result does not say that a limit ended the question, and `steps` has no line for it. A
|
|
97
|
+
`budget_reached` notice says it once when `inputTokens` or `requests` stopped the tools
|
|
98
|
+
([events](../reference/events.md#noticecode-and-noticedisplay)). When `result.turns` reaches
|
|
99
|
+
`toolTurns`, the model was most likely still calling tools when its turns ran out.
|
|
100
|
+
`result.usage` shows how close the question came to `inputTokens` and `requests`.
|
|
95
101
|
- With an explorer model, the limits count the work of both models together. The answer is always
|
|
96
102
|
a request of its own, so the explorer has at most `toolTurns − 1` requests
|
|
97
103
|
([explorer model](./explorer-model.md)).
|
|
98
104
|
- `maxToolTurns` and `maxToolCallsPerTurn` of `askGemini` are `toolTurns` and `callsPerTurn` under
|
|
99
105
|
their names from before 0.5.0. When both are given, `limits` wins.
|
|
100
106
|
|
|
101
|
-
###
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
### The default ceiling
|
|
108
|
+
|
|
109
|
+
New in 0.10.0: a question that sets no `inputTokens` still has a ceiling. One runaway question, a
|
|
110
|
+
model that reads eight more pieces of a log in every turn and never answers on its own, made 13
|
|
111
|
+
requests and about 6.9 million input tokens with no ceiling. A default bounds what one question can
|
|
112
|
+
cost when nobody set a ceiling. It is the larger of these two, with room for whole-file reads added:
|
|
113
|
+
|
|
114
|
+
- [`DEFAULT_INPUT_TOKENS`](../reference/ask.md#default_input_tokens): 1,000,000;
|
|
115
|
+
- `toolTurns` times the estimate of the question's first request. A long conversation makes the
|
|
116
|
+
first request large, and every tool request carries it again: the ceiling grows with it.
|
|
117
|
+
|
|
118
|
+
**The room for whole-file reads** is as many reads as the question may make (`wholeReadsPerQuestion`,
|
|
119
|
+
and `wholeDocumentReads` of each document that can be read whole), each at the price of the dearest
|
|
120
|
+
of those documents (see [below](#the-ceiling-looks-ahead)). It is 0 when no document of the question
|
|
121
|
+
can be read whole, or `analyze_whole_document` is not offered. So a read of a 1.77 MB log in parts of
|
|
122
|
+
60,000 tokens, about 1.5 million input tokens, runs under the default; a third read in one question
|
|
123
|
+
does not (`wholeReadsPerQuestion`).
|
|
124
|
+
|
|
125
|
+
Typical questions are far below it: four tool turns over a 3 MB log use less than half of
|
|
126
|
+
1,000,000, and make the same requests as with no ceiling. `limits: { inputTokens: Infinity }` gives
|
|
127
|
+
the behavior of 0.9: no ceiling.
|
|
128
|
+
|
|
129
|
+
### The ceiling looks ahead
|
|
130
|
+
|
|
131
|
+
- **It is checked before each tool request**, looking ahead: what the question has spent, the tool
|
|
132
|
+
request about to be sent and the request for the answer after it, as the loop estimates them now
|
|
133
|
+
(raised by what the provider reported for the requests before). When the three would pass the
|
|
134
|
+
ceiling, no tool request is made, and the next request is the answer. So a question spends at most
|
|
135
|
+
about one request past its ceiling: the answer's, with the results of the last tool turn in it.
|
|
136
|
+
The answer's second try, after a reply without text, looks ahead the same way: it is not made when
|
|
137
|
+
it would pass the ceiling. In 0.9 the ceiling was checked after the fact: a ceiling of 500,000 let
|
|
138
|
+
1,219,683 tokens through.
|
|
139
|
+
- **A request that reports no input is counted by its estimate.** An OpenAI-compatible server that
|
|
140
|
+
sends no usage, a provider that reports an input of 0, or a part reader of your own that does not
|
|
141
|
+
call `onUsage`: the loop adds its estimate of that request to `usage.estimatedInputTokens`, and
|
|
142
|
+
counts it in `usage.unmeasuredRequests`. The ceiling counts both: `usage.inputTokens +
|
|
143
|
+
usage.estimatedInputTokens`. In 0.9 such a question never reached its ceiling.
|
|
108
144
|
- **`requests` keeps one request for the answer**, so the answer stays within it.
|
|
109
|
-
- **A whole-file read is
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
145
|
+
- **A whole-file read is priced before it starts.** In input tokens, by what its requests send: the
|
|
146
|
+
file's lines with their numbers, the prompt of each part, the merges of their notes and the read's
|
|
147
|
+
own answer, in the tokens of the model that reads it. That is about 10% more than the file itself
|
|
148
|
+
for a log, and more again when its notes need merges. In requests, it is its parts, one merge of
|
|
149
|
+
their notes, its answer, and the answer of the question. A read that does not fit what is left is
|
|
150
|
+
not run, and costs nothing. The model gets the reason, for example: "Not run: reading all of
|
|
151
|
+
app.log takes about 2,400,000 input tokens, and 900,000 are left of the 1,500,000 this question may
|
|
152
|
+
use. Use search_document and read_document." Reads let through in the same turn share what is
|
|
153
|
+
left. A read whose notes need more than one merge can pass `requests` by those merges.
|
|
154
|
+
- **A read that failed is not priced again.** When a whole-file read of a document sent its parts
|
|
155
|
+
and could read none of them, a second call for it is answered by the tool with that reason ("The
|
|
156
|
+
whole-file read of app.log failed earlier in this question (…), and is not tried again."), not
|
|
157
|
+
with its price.
|
|
116
158
|
- **A request that failed and was sent again counts once**, as it does in `usage`.
|
|
117
159
|
|
|
118
160
|
### Tools you can leave out
|
|
@@ -375,7 +417,10 @@ What each provider's numbers become:
|
|
|
375
417
|
have billed it: a stream cut off half-way, or a wait on a silent model that ran out. Your
|
|
376
418
|
provider's bill is the record; `usage` is a close account of it.
|
|
377
419
|
- **A request without usage counts with 0 tokens**: an OpenAI-compatible server that sends no
|
|
378
|
-
usage, or a part reader of your own that does not call `onUsage`.
|
|
420
|
+
usage, or a part reader of your own that does not call `onUsage`. Since 0.10.0 its input is
|
|
421
|
+
counted apart, by the library's estimate: `estimatedInputTokens`, in each model's own tokens,
|
|
422
|
+
and `unmeasuredRequests`. Both are absent when every request reported its input. The ceiling of
|
|
423
|
+
the question counts them; your cost accounting may too, as an estimate.
|
|
379
424
|
- **`byModel` is keyed by the adapter's `id`.** For `gemini()` that is the model named in the URL
|
|
380
425
|
(`"gemini-3.8-flash"`); for `openAiCompatible()` and `anthropic()`, the `model` option; for any of
|
|
381
426
|
them, the `id` option when you set it. A part read counts under the model that read it: the
|
|
@@ -509,7 +554,8 @@ export async function answer(sessions: Sessions, userId: string, sessionId: stri
|
|
|
509
554
|
### An allowance per user
|
|
510
555
|
|
|
511
556
|
To keep a user within an allowance, turn what the user has left into `limits.inputTokens` of the
|
|
512
|
-
next question. Keep a margin: a question can pass its ceiling by about
|
|
557
|
+
next question. Keep a margin: a question can pass its ceiling by about one request, the answer's
|
|
558
|
+
([The ceiling looks ahead](#the-ceiling-looks-ahead)).
|
|
513
559
|
|
|
514
560
|
```ts
|
|
515
561
|
import { askGemini } from "llm-large-files/gemini";
|
|
@@ -517,7 +563,7 @@ import { askGemini } from "llm-large-files/gemini";
|
|
|
517
563
|
// files and endpoint as in the first sample. spentThisMonth: the user's input tokens this month,
|
|
518
564
|
// from your records. userId, documentIds and messages are yours.
|
|
519
565
|
const allowance = 50_000_000; // input tokens a month: your own figure
|
|
520
|
-
const margin =
|
|
566
|
+
const margin = 1_000_000; // about one request of a 1M-token window
|
|
521
567
|
const left = allowance - spentThisMonth;
|
|
522
568
|
if (left <= margin) throw new Error("You have used this month's allowance.");
|
|
523
569
|
|
|
@@ -588,8 +634,10 @@ totals do not have. More on wrapping an adapter: [your own adapter and loop](./o
|
|
|
588
634
|
|
|
589
635
|
What bounds it:
|
|
590
636
|
|
|
591
|
-
- `limits.wholeDocumentReads` (1 per document by default), `
|
|
592
|
-
|
|
637
|
+
- `limits.wholeDocumentReads` (1 per document by default), `limits.wholeReadsPerQuestion` (2 in
|
|
638
|
+
all by default, new in 0.10.0), `wholeDocumentAnalysis: false`, and `limits.inputTokens` and
|
|
639
|
+
`limits.requests`, which refuse a read that would pass them. The default ceiling leaves room for
|
|
640
|
+
the reads the question may make ([the default ceiling](#the-default-ceiling)).
|
|
593
641
|
- With your own loop, the same cap is `maxWholeDocumentReads` of `files.tools()`
|
|
594
642
|
(`MAX_WHOLE_DOCUMENT_READS`, 1, by default), counted per tool set: make one for each question
|
|
595
643
|
([your own adapter and loop](./own-adapter-and-loop.md)).
|
|
@@ -607,12 +655,15 @@ What bounds it:
|
|
|
607
655
|
Each chat request sends everything before it. One tool result may be up to `toolResultCharacters`
|
|
608
656
|
(24,000 characters, about 12,000 tokens of a log), up to 8 run in a turn, and every result stays in
|
|
609
657
|
the requests until the answer. Text tool results may fill the window up to
|
|
610
|
-
`contextTokens − reservedTokens / 2`: 968,000 tokens with the default budget. So
|
|
611
|
-
|
|
612
|
-
whole-file read of each document searched.
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
658
|
+
`contextTokens − reservedTokens / 2`: 968,000 tokens with the default budget. So with no ceiling
|
|
659
|
+
the bound is 12 requests of up to about 968,000 tokens each, about 11.6 million input tokens, plus a
|
|
660
|
+
whole-file read of each document searched. Since 0.10.0 the default ceiling stops the tools near
|
|
661
|
+
1,000,000 input tokens (one request past it at most, and room for the whole-file reads the question
|
|
662
|
+
may make). A question of a few searches and reads is far from either: [measurements](../measurements.md)
|
|
663
|
+
has live numbers.
|
|
664
|
+
|
|
665
|
+
What bounds it: `limits.toolTurns` and `limits.callsPerTurn`; `limits.inputTokens`, by default
|
|
666
|
+
since 0.10.0;
|
|
616
667
|
`budget.toolResultCharacters` (smaller results, though the model may then need more reads); and an
|
|
617
668
|
explorer model, which makes these requests at its own price.
|
|
618
669
|
|
|
@@ -646,6 +697,46 @@ repeats cheaper ([below](#prompt-caching)).
|
|
|
646
697
|
sent whole can take several when the document is searched.
|
|
647
698
|
- Name in `documentIds` only the documents a question is about. Each one named is planned: sent
|
|
648
699
|
whole, or given a card.
|
|
700
|
+
- Since 0.10.0 their lines are numbered (`numberInline`), so that the answer can cite them: about
|
|
701
|
+
8% to 16% more tokens for each file sent whole (15.5% for a CSV of short rows, 9.4% for a service
|
|
702
|
+
log, 8.4% for Hebrew prose). A file at the edge of `inlineTokens` may become a card for it.
|
|
703
|
+
`numberInline: false` sends them as 0.9 did.
|
|
704
|
+
|
|
705
|
+
### Tables sent whole
|
|
706
|
+
|
|
707
|
+
Since 0.10.0 a table sent whole (a CSV, an XLSX workbook, a file with `| cell |` rows) gets
|
|
708
|
+
`search_document`, `read_document` and `aggregate_table` (`inlineTables: "tools"`, the default), so
|
|
709
|
+
that counts and sums over it are exact, not made by eye. What a question costs then:
|
|
710
|
+
|
|
711
|
+
- **A question that needs no count** stays at one request when the model answers at once, as it
|
|
712
|
+
does when the answer is in the table it sees. The request is a little larger, by the three tool
|
|
713
|
+
declarations and one line of the system text (about 1,500 tokens). Some models look the row up
|
|
714
|
+
with a tool anyway, which takes 2 requests: in the live checks of 0.10.0, Flash-Lite did so for
|
|
715
|
+
about half of these questions, and Flash rarely
|
|
716
|
+
([measurements](../measurements.md#tables-sent-whole-p1s-live-gate)). A reply that ends with no
|
|
717
|
+
text and no call, and not by a filter or the output limit, is asked again once with a note, as in
|
|
718
|
+
any question: one request more, with the table.
|
|
719
|
+
- **A count** takes about 3 requests: one that calls `aggregate_table` (or an exact search), one
|
|
720
|
+
that answers from its result, and sometimes one more call. Each sends the table again. For a CSV
|
|
721
|
+
of 3,000 invoices (113,000 estimated tokens, 131,000 with its lines numbered), one request of 0.9
|
|
722
|
+
becomes about three of 131,000 tokens each: about 400,000 input tokens by the library's estimate.
|
|
723
|
+
Requests two and three start as the first did, so a provider whose cache matches that start
|
|
724
|
+
(Gemini's implicit cache on Vertex AI) bills most of them at the cached price.
|
|
725
|
+
- **With an explorer**, a question in which no file is searched and a table sent whole gets the
|
|
726
|
+
tools is run by the answering model alone: the explorer would send every file sent whole again,
|
|
727
|
+
to a model that does not share the other's cache.
|
|
728
|
+
- **At most**, a model that goes on calling tools uses the question's ceiling: by default about
|
|
729
|
+
`toolTurns` times its first request (`DEFAULT_INPUT_TOKENS`, 1,000,000, at least). For the CSV of
|
|
730
|
+
3,000 invoices that is 11 requests of about 134,000 tokens, about 1.5M input tokens, where
|
|
731
|
+
`"text"` makes one request of 133,000. `limits.inputTokens` or `limits.toolTurns` bound it.
|
|
732
|
+
- **Limits sized for 0.9.** A count needs room for about three requests with the table. A ceiling
|
|
733
|
+
(`limits.inputTokens`) or `limits.requests` sized for 0.9's one request leaves no tool turn, and
|
|
734
|
+
so does `limits.toolTurns: 1`: such a question is then the one request of `"text"`, without the
|
|
735
|
+
tools, and its counts are made by eye again.
|
|
736
|
+
|
|
737
|
+
To keep the cost of 0.9 for such questions, at the price of counts made by eye, pass
|
|
738
|
+
`inlineTables: "text"`. A lower `inlineTokens` sends the table as a card instead: its requests are
|
|
739
|
+
small, and the model reads it only through the tools.
|
|
649
740
|
|
|
650
741
|
### An explorer model
|
|
651
742
|
|
|
@@ -685,15 +776,27 @@ What the library changes:
|
|
|
685
776
|
|
|
686
777
|
- A document that went whole can become a card as the history grows (above). The tool guide, which
|
|
687
778
|
names the files that are searched, changes with it.
|
|
779
|
+
- The tools declared follow the files of the question. Since 0.10.0 a table sent whole gets three
|
|
780
|
+
of them and a line of the system text ([Tables sent whole](#tables-sent-whole)), so a table
|
|
781
|
+
added to a chat whose files were all sent whole adds tools to its requests.
|
|
688
782
|
- Within a question, when room runs short, older tool results are replaced by short stand-ins. The
|
|
689
783
|
request changes from the first round replaced. A model whose conversation may only grow (Claude)
|
|
690
|
-
has nothing replaced.
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
784
|
+
has nothing of the question replaced. Since 0.9.0 the stored rounds of earlier questions are
|
|
785
|
+
replaced before the first request when they make it larger than its room in tokens (by the
|
|
786
|
+
library's estimate, as after a refused request; not for its bytes alone), for every model: the
|
|
787
|
+
request changes from the first stored round replaced, as it did in 0.8 after the provider refused
|
|
788
|
+
the request, but without the refused request.
|
|
789
|
+
- The tool rounds of an earlier question are not sent as they were in their own question, for two
|
|
790
|
+
reasons. Their results go without `tool_turns_left`. And their model turns go as the calls alone:
|
|
791
|
+
a stored round keeps no thought signatures (Gemini) or thinking blocks (Claude), which the
|
|
792
|
+
requests of its own question sent back. So the first request of a follow-up question differs
|
|
793
|
+
from the requests of the question before it from that question's first tool round on, and the
|
|
794
|
+
cache misses from there, once in each follow-up question: those rounds are billed at the full
|
|
795
|
+
price in that request (with Claude's `cache_control`, as a cache write). The rounds of older
|
|
796
|
+
questions were already sent that way, and still match. The documents and cards before them are
|
|
797
|
+
still read from the cache. Pages up to 0.8.2 named `tool_turns_left` alone; with a model that
|
|
798
|
+
thinks, the signatures make the miss on their own. Keeping the model's own turns is planned for
|
|
799
|
+
a later version.
|
|
697
800
|
- An upgrade that changes the library's prompts misses the cache once in each conversation. 0.6.0
|
|
698
801
|
changed the tool guide, the descriptions of `search_document` and `analyze_whole_document`, and
|
|
699
802
|
the frame lines of a file sent whole. The first request after the upgrade shares with what 0.5.0
|
|
@@ -703,7 +806,9 @@ What the library changes:
|
|
|
703
806
|
instruction. For a question whose files are all sent whole, it is your own system text, and
|
|
704
807
|
`view_pages` with its guide when the pages of a PDF can be looked at. From then on the frame of a
|
|
705
808
|
document is the same in every request
|
|
706
|
-
([Deployment](./deployment.md#servers-of-two-versions)).
|
|
809
|
+
([Deployment](./deployment.md#servers-of-two-versions)). 0.10.0 changes the text again: files
|
|
810
|
+
sent whole are numbered, a table sent whole gets tools, and `analyze_whole_document` is declared
|
|
811
|
+
with two more arguments ([Upgrading to 0.10](../upgrading-0.10.md)).
|
|
707
812
|
|
|
708
813
|
The adapters:
|
|
709
814
|
|