Code Mode for In-App SaaS AI Agents: Why 150 Tools Wreck Prompt Caching
Most write-ups on MCP code mode focus on external developer clients connecting to a remote server. Inside a real B2B SaaS product, there is an even bigger payoff that almost nobody talks about: running code mode for your
Most write-ups on MCP code mode focus on external developer clients connecting to a remote server. Inside a real B2B SaaS product, there is an even bigger payoff that almost nobody talks about: running code mode for your own in-app AI assistant so your prompt cache hit rate stays near its ceiling and your per-user LLM cost stops scaling linearly with your feature count.
When we built Mailbase (an email workflow SaaS covering transactional sending, campaigns, audiences, inbox triage, prospecting, and API keys), our tool catalog grew past 150 operations. That is the exact point where classic tool-calling breaks unit economics.
Why 150 tool definitions quietly wreck prompt caching
Modern LLM providers discount cached input tokens by 75% to 90% β only when the prompt prefix is byte-identical.
If you dynamically filter tool schemas per route, plan, or user role (if (user.isAdmin) tools.push(...)), every permutation invalidates the cache prefix for the entire tool block. On the flip side, if you pin all 150+ JSON Schemas on every turn so they cache, you still pay for 25,000+ prefix tokens on turn one, dilute tool-selection accuracy, and pay full price every time multi-step read loops dump raw JSON arrays into conversation history.
Ask an in-app assistant "Which of my last 10 campaigns had a bounce rate above 2%?" under classic tool calling: the model runs 11 sequential turns and stuffs every raw response into the context window just to output two lines.
The two-tool split: search_tools + QuickJS run_program
We collapsed the entire catalog into two stable tools whose definitions are byte-identical across every user, role, and page:
-
search_tools(query, family, detail)β progressive disclosure:-
detail: "names"returns only tool names -
detail: "descriptions"adds titles, read-only/destructive flags, and sandbox eligibility -
detail: "full"loads Zod-derived JSON Schemas for only the few tools needed right now
-
-
run_program(code)β executes a short async JavaScript function inside an isolated QuickJS sandbox (no network, no filesystem, no timers, no env vars, hard wall-clock/CPU/memory/call caps):
const campaigns = await mailbase.list_campaigns({});
const stats = await Promise.all(
campaigns.slice(0, 10).map((c) => mailbase.get_campaign_stats({ id: c.id }))
);
return stats
.filter((s) => s.bounce_rate > 0.02)
.map((s) => ({ id: s.id, name: s.name, bounce_rate: s.bounce_rate }));
One LLM turn. 200 bytes back into context instead of 40 KB of raw JSON.
Multi-tenant security inside the bridge
The obvious objection: if the model writes JS, can it bypass RBAC or send 500 emails in a loop?
Not if the host bridge refuses to be a shortcut:
-
Static read-only allowlist: write and send tools are rejected inside
run_program. To mutate state or send an email, the model must invoke the tool directly outside the sandbox so the UI asks the user to confirm. -
Same guarded handler on every inner call: every
await mailbase.<tool>(args)passes through Zod argument validation, OAuth/session scopes, workspace isolation, and the member's live role check.
Full deep-dive and comparison table originally published on the Mailbase engineering blog: Code Mode for In-App SaaS Agents: Prompt Caching & Cost Math.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.