Past 60 messages, every turn paid again for the whole chat history

A long cream paper scroll unrolls from a wooden roller over a pile of trimmed sheets, with a brass stamp beside it.

Cache never hits Fixed upstream

An AI agent that runs company work for a founder on Z.ai's GLM, with a prompt head built byte-stable and guarded by its own test.

≈ 2,000,000 tokens per 1,000 calls, prompt tokens the cache could have served, from a history window that slid every turn estimate

Estimates, not measurements. Every figure on this page is an estimate from reading the code, not a measurement of the app in use.

Repository
catalation/synapse-ceo, a private repository
Commit audited
03bbd01
Permission
Published with the maintainers’ written permission, given . The maintainers ran leanprompt on their own code and sent us the report.
leanprompt version
0.1.0-dev.15
Audit date
Status
The maintainers have changed the code since this audit.

leanprompt read the code at commit 03bbd01 and found 0 places where its LLM calls could use fewer tokens, 1 the project already knows about, and 5 already fixed, plus 1 other problem, not scored. Static score: 100 of 100 (an estimate from the findings, leaving out what the project already knows about). The code may have changed since.

Findings

No open findings.

Questions for the server’s administrator

Settings of the server or engine the app calls, not of the app’s code; left out of the static score.

  1. LP-LW-01 Turn on prefix caching and set the match unit

    LM Warden and the llama.cpp/vLLM engine behind LOCAL_BASE_URL · severity low · saves prefill · needs a fact to confirm

    What happens. Is prefix caching on for the local Qwen engine behind Warden, and with what block size? The head this service sends (system prompt and tools) is several thousand tokens, so a block-mode engine reuses it only if caching is on.

    The change. Turn on prefix caching on the engine; for a hybrid model with speculative decoding, set the prefix match unit.

    Why it helps. Without it every local call computes the whole prompt again.

    Estimate small for this app: the local model serves only model-hint turns and classifier fallback (estimate)

    Assumptions. the worker tier is configured but not used by the turn loop, so local traffic is a small share

    To confirm. whether prefix caching is on in the engine behind Warden and its block or match size (npx leanprompt profile detect --lmwarden reads it)

Already known to the project

The project’s own issues, pull requests or docs already cover these, so they are not counted above and are left out of the static score.

  1. LP-PD-01 Offer each route only the tools it can use

    src/synapse_ceo/tools/surface.py:130 · severity low · saves prefill

    What happens. Every call is offered all 33 tools, write tools included on read-only turns. The project plans to narrow this once it has failure data.

    The change. When narrowing, keep two fixed tool lists (read and write) so each stays cacheable, rather than a list chosen per turn.

    Why it helps. Write tool definitions are sent on turns that cannot use them; switching lists per turn would cost cache, so the gain is mostly in cached-read tokens.

    Already planned. Open in the project's own (private) tracker: narrow the tool surface using measured failures (progressive mode, M6). https://gitlab.com/catalation/synapse-ceo/-/issues/14

    Estimate about 1.15M prompt tokens per 1,000 calls, mostly cached reads (estimate)

    Assumptions. half of turns are read-only; the write tools are about 2,300 tokens of the roughly 5,000-token tool list

Already fixed

  1. LP-PC-06 Append-only history

    src/synapse_ceo/storage/sessions.py:71 · severity high · saves prefill · how often is assumed

    What happens. Once a conversation passes 60 messages, every save drops the oldest messages so it is exactly 60 again. The start of the history then moves on every turn, so the provider can reuse nothing after the company context: the whole remaining history is computed again on the first call of each later turn.

    The change. Trim in steps instead of on every save: when the history passes 60 messages, cut it back to about 40 (keeping the tool-call boundary rule), so the history stays append-only for the next ten or so turns and the cache breaks once per step, not every turn. Later, a summary message at the cut (cache-aware compaction) keeps what the dropped turns said.

    Why it helps. A sliding window rewrites the front of the history on every turn, and a prefix cache only reuses an unchanged front. Long conversations, the ones with the most history, pay full price for all of it on every turn.

    Estimate about 2.0M prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. about 7 messages per turn, so the cap is reached after 8 to 9 turns; 30% of turns are in sessions past the cap; about 20,000 history tokens at the cap; only the first call of each turn is affected (later rounds append); about 3 calls per turn; the provider's cache outlives the pause between turns

    How often. how many turns run in sessions past 60 messages, and cached_tokens on the first round of those turns, from the turn_end account in the logs

  2. LP-PD-04 Trim tool results and retrieved context at the tool boundary

    src/synapse_ceo/loop/agent.py:687 · severity medium · saves prefill · size is assumed

    What happens. Every tool result goes into the conversation history whole and stays there for every later call of the session. Only the copy sent to the founder's screen is cut to 4,000 characters; the model gets the full text, including raw payloads from connected systems through mcpu__invoke.

    The change. Cut each tool result once, when it is added to the history (agent.py, _tool_message): keep the fields the model uses, cap the length (for example 8,000 characters with a note saying it was cut and how to ask for more), and never change it afterwards.

    Why it helps. A large result is paid for on every later call until it falls out of the 60-message window, mostly as cached reads, and it brings the per-turn token cap and the history trim closer.

    Estimate about 1.2M prompt tokens per 1,000 calls that need not be sent (estimate)

    Assumptions. an average call carries 2 tool results that could each lose about 600 tokens; most of these tokens are cached reads, so the money saved is smaller than the token count

    How big. the size distribution of tool results, from the tool-call log's result_excerpt per tool (task_list, knowledge_read, mcpu__invoke)

  3. LP-PC-09 Consistent reasoning handling; thinking fixed per session

    src/synapse_ceo/loop/agent.py:466 · severity medium · saves prefill · needs a fact to confirm

    What happens. GLM always thinks, but the stream reader keeps only content and tool calls, so the assistant message stored after a tool call has no reasoning. If the provider's template keeps the reasoning of the current turn's tool rounds, the next round's prompt differs from what it cached at that assistant message.

    The change. Read reasoning_content from the stream and store it on the assistant message that carries tool calls, sending it back on the next rounds of the same turn, as Z.ai's thinking docs describe; keep thinking settings the same within a session.

    Why it helps. Everything after the first changed byte is computed again; dropping reasoning between tool rounds also makes the model think again about what it had already worked out.

    Estimate about 1.0M prompt tokens per 1,000 calls not served from cache, if the template keeps reasoning (estimate)

    Assumptions. two thirds of calls are follow-up rounds of a tool turn; about 1,500 tokens recomputed on each from the last assistant message onwards

    To confirm. whether Z.ai's GLM 5.x template keeps reasoning_content of the current turn's tool rounds (preserved or interleaved thinking): compare cached_tokens on round 2 of a tool turn with the size of round 1's prompt plus its assistant message

  4. LP-PC-05 Tool lists grow only at the end

    src/synapse_ceo/loop/agent.py:275 · severity medium · saves prefill · how often is assumed

    What happens. On the last round after a held write, a queued proposal or a pending approval, the loop sends the request with no tools at all. GLM's chat template puts the tool list in the system block at the very top, so that request shares no prefix with the round before it and is computed again from the first token.

    The change. Keep the same tool list on the terminal round and stop tool use another way: send tool_choice "none" if Z.ai accepts it, otherwise keep the tools and the closing instruction and ignore any tool call the model makes on that round, as the loop already does.

    Why it helps. Removing the tools changes the start of the prompt, which throws away the cache for the whole prompt on that call, history included.

    Estimate about 530k prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. 10% of turns end in a terminal round; the prompt is about 16,000 tokens at that point; the provider renders tools at the top of the prompt (true for GLM and Qwen chat templates)

    How often. how many turns end in a held preview, a queued proposal or a pending approval, and cached_tokens on their final round, from the turn logs

  5. LP-GE-03 Output and thinking budgets that fit the task

    src/synapse_ceo/loop/agent.py:283 · severity medium · saves generation · size is assumed

    What happens. The agent loop's calls send no max_tokens, to a model that always thinks. A runaway answer or a long thinking loop runs to the provider's own maximum before anything stops it; the 150,000-token turn cap is only checked after the call returns.

    The change. Send a per-call max_tokens on the agent loop (a setting, for example 8,000 to 16,000, sized from the measured largest answers plus thinking), the same way the classifier already sends one.

    Why it helps. Output is the most expensive token, and with no cap a single bad call can spend more than a whole normal turn.

    Estimate about 12k output tokens per 1,000 calls (estimate)

    Assumptions. about 400 output tokens per call on average; runaway calls are rare and make up about 3% of output

    How big. the largest completion_tokens per call and how often a call ends with finish_reason length, from the turn_end logs

Other problems the audit found

Problems read in the code that cost no tokens of their own, such as a cost meter or an error path. Not scored.

  1. A cut-off answer is shown and stored as complete

    src/synapse_ceo/loop/agent.py:303 · LP-GE-12 Read the finish reason before using an answer · severity medium · the same problem as finding 7

    What happens. The stream reader records finish_reason, but the loop never reads it: an answer that stopped at the output limit (finish_reason length) is streamed to the founder, appended to the history and treated as the turn's answer. Tool-call arguments cut off the same way are only caught by argument validation.

    The change. After each call, check response.finish_reason; on length, tell the founder the answer was cut off (an error or status event) and mark the stored message, or retry once with a larger cap.

    To check. A test with a stub stream ending in finish_reason length produces a truncation event and no plain completed answer.

The maintainers’ response

The maintainers ran the audit on their own code, fixed every finding in it, and agreed to its publication here.

The maintainers,

How this was made

The llm-call-audit skill read the code and wrote findings.json; nothing was run against the project’s providers. The full audit page is what leanprompt report --html writes from the same findings.

Corrections and takedown: email info@podwarden.com or open an issue.