Setup
-
Model:
gemini-3.1-flash-lite, Vertex AI,locations/global,generateContent(not streaming, not batch) -
Seen in two different projects, so not tied to one project’s settings
-
Text only: no images, files or tools are needed to reproduce. A request with 27 function declarations fails the same way.
What happens
Large requests sometimes return:
{"error":{"code":400,"message":"Request contains an invalid argument.","errors":[{"message":"Request contains an invalid argument.","domain":"global","reason":"badRequest"}],"status":"INVALID_ARGUMENT"}}
-
The exact same request body passes on one call and fails on the next, seconds apart.
-
It fails fast (about 1–3 s).
-
It fails mostly when the prompt is not in the implicit cache. Repeating an identical prompt mostly passes after the first few calls.
-
No field or reason is given.
-
The documented context window is 1,048,576 tokens.
Results. Each call gets a unique first line in the system instruction, so nothing is cached:
Prompt size (promptTokenCount)
Calls
400s
~99k
10
0
~114k – 128.5k (real app prompt)
22
0
~128.8k – 129k (real app prompt)
18
7
~166k
6
1
~183k (filler below, raw REST)
10
3
~183k (filler below, LangChain)
10
5
Repro (Node 18+; set PROJECT, and TOKEN=$(gcloud auth print-access-token)):
const url = `https://aiplatform.googleapis.com/v1/projects/${process.env.PROJECT}/locations/global/publishers/google/models/gemini-3.1-flash-lite:generateContent`;
const w = ['north','river','market','stone','quiet','amber','field','harbor','lantern','meadow','copper','signal'];
const lines = []; let size = 0;
for (let i = 0; size < 440000; i++) { const l = `Line ${i}: ${w[i%12]} ${w[(i*7)%12]} ${w[(i*5)%12]} item ${(i*31)%1000}.`; lines.push(l); size += l.length + 1; }
const filler = lines.join('\n');
(async () => {
for (let run = 1; run <= 10; run++) {
const body = {
systemInstruction: { parts: [{ text: `Request id ${Math.random().toString(36).slice(2)}\nYou are a helpful assistant. Reference text follows.\n${filler}` }] },
contents: [{ role: 'user', parts: [{ text: 'How many lines mention harbor? Answer in one short sentence.' }] }],
generationConfig: { temperature: 0.4, maxOutputTokens: 200 },
};
const res = await fetch(url, { method: 'POST', headers: { Authorization: `Bearer ${process.env.TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
const data = await res.json();
console.log(run, res.status, res.status === 200 ? data.usageMetadata?.promptTokenCount : data.error?.message);
}
})();
At size < 240000 (about 99k tokens), the same script gives 0 failures in 10.
Questions
-
Is there an undocumented input limit, around 128k / 131,072 tokens, on some of the backends serving this model? Others have reported a 131,072 cap on Gemini 3.1 Pro Preview on Vertex, but with an explicit “exceeds the maximum number of tokens allowed (131072)” message. Here the message is the bare one.
-
If so, can the error say so, and is a fix planned so large requests get the documented 1M window every time?
-
Meanwhile, is retrying the right workaround, or should we keep requests under ~128k?
Related threads: