Cap the hint prompt at 120 tokens: a full 223-token prompt garbles small models

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-10-29 23:51:47 +01:00
1 parent 28201bcd17
commit edb9b332b9
7 files changed
+31 -23

No files matched your search

+9 -5
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { join } from "node:path";
import { fitHintToBudget, normalizeHint } from "../src/lib/hint";
import { fitHintToBudget, normalizeHint, PROMPT_TOKEN_BUDGET } from "../src/lib/hint";
// D-WS2a: the server engine truncates the hint in Python (server-py/sidecar.py,
// with faster-whisper's tokenizer) and the browser engine in TypeScript
@@ -34,11 +34,11 @@ sidecar = importlib.util.module_from_spec(spec)
spec.loader.exec_module(sidecar)
encode = lambda text: [ord(c) for c in text]
cases = json.load(sys.stdin)
print(json.dumps([
print(json.dumps({"budget": sidecar.PROMPT_TOKEN_BUDGET, "results": [
{"normalized": sidecar.normalize_hint(c["hint"]),
"ids": sidecar.fit_prompt_ids(c["hint"], encode, c["budget"])}
for c in cases
]))
]}))
`;
describe("hint truncation parity: TypeScript vs sidecar.py", () => {
@@ -49,8 +49,12 @@ describe("hint truncation parity: TypeScript vs sidecar.py", () => {
stderr: "pipe",
});
expect(proc.stderr.toString()).toBe("");
const python = JSON.parse(proc.stdout.toString()) as { normalized: string; ids: number[] }[];
expect(python).toEqual(
const python = JSON.parse(proc.stdout.toString()) as {
budget: number;
results: { normalized: string; ids: number[] }[];
};
expect(python.budget).toBe(PROMPT_TOKEN_BUDGET);
expect(python.results).toEqual(
CASES.map(({ hint, budget }) => ({
normalized: normalizeHint(hint),
ids: fitHintToBudget(hint, charEncode, budget),
+4 -4
View File
@@ -96,13 +96,13 @@ describe("fitHintToBudget (D-WS2a truncation rule)", () => {
expect(ids.length).toBe(10);
});
test("a very long hint fits the 223-token budget and is a word prefix of the hint", () => {
test("a very long hint fits the 120-token budget and is a word prefix of the hint", () => {
const words = Array.from({ length: 5000 }, (_, i) => `term${i}`);
const hint = words.join(", ");
const ids = fitHintToBudget(hint, charEncode);
expect(PROMPT_TOKEN_BUDGET).toBe(223);
expect(ids.length).toBeLessThanOrEqual(223);
expect(ids.length).toBeGreaterThan(200);
expect(PROMPT_TOKEN_BUDGET).toBe(120);
expect(ids.length).toBeLessThanOrEqual(120);
expect(ids.length).toBeGreaterThan(100);
const kept = charDecode(ids);
// Ends on a whole term plus the full stop: the hint continues with a separator there.
expect(kept.endsWith(".")).toBe(true);
+2 -2
View File
@@ -117,8 +117,8 @@ describe("buildPromptPrefix", () => {
]);
});
test("a huge hint stays within the 223-token budget", () => {
test("a huge hint stays within the 120-token budget (D-WS2a)", () => {
const prefix = buildPromptPrefix(tokenizer, "Maxwell, ".repeat(500));
expect(prefix.length).toBeLessThanOrEqual(224);
expect(prefix.length).toBeLessThanOrEqual(121);
});
});