Senpai
Sales copilot on Otsuka's SPR schema: deterministic deal health, grounded tool calling
The outcome
- 5× inference throughput — 11 → 55 tok/s — via prefix caching, persistent context caching and parallel execution.
- Removed a wasted full generation per turn with the finish-tool loop; smoke-tested across all seven workflows with no 500s.
- Routing all synthesis to a Q4 8B cut the create_quote workflow from 334s to 152s; the frozen-context bench measured grounding parity (0.969 vs 0.969).
- Segment intelligence over 44 category × industry segments: 22 kept their gated LLM narrative, 22 fell back to the deterministic template, and no published figure comes from the model.
- Product recommendations grounded in a 9,826-page index of Otsuka's public site, including 3,285 product pages and 257 case studies, each chunk cited by URL.
- The pipeline War Room recomputes six months of weekly deal health and returns the replay in 143 ms, with no model in the path.
The challenge
A sales knowledge and onboarding copilot we built at Otsuka, anchored to the real shape of Otsuka's SPR sales data. A deterministic engine scores every deal on seven signals and explains each one; a self-hosted tool-calling model answers reps and managers by calling into that engine, hybrid retrieval and a deal graph, never by recalling facts on its own. The same health read that briefs a junior before a call flags a manager's dying deal.
Sales coaching is a judgement task built on records nobody has time to read: deal histories, activity logs, and what worked for comparable accounts. An assistant is the obvious shape for it, and also the dangerous one. A coaching answer that invents a win rate is worse than no answer, because it is confident, specific, and indistinguishable from a real one. So the constraint that shaped the system was that every number comes from code and every claim traces to a real record or a human-approved principle.
The system
A Next.js app talks to a FastAPI bridge over server-sent events. Before the model sees a turn, the bridge resolves the customer and deal in code (a deal picked from the selector is authoritative; an ambiguous name returns a picker instead of a guess) and pins the result into the prompt. Document goals branch to an LLMPlanner, which chooses only the set of capabilities to gather; entity IDs, edges and the fallback plan stay deterministic. Everything else runs a tool-selection loop against the served model: each batch of tool calls goes to an AdaptiveScheduler, which drops duplicates and turns the batch into a DAG where reads run in parallel and writes or external actions sit behind serialization barriers. A thread-pool ExecutionEngine runs that DAG against the SPR store and health engine, hybrid BM25 plus dense retrieval, and a networkx deal graph, and returns an append-only EvidenceBundle with citations; a failing tool degrades to an empty fragment rather than failing the run. The loop repeats until the model calls a finish tool, then a single synthesis round streams the answer. If every tool in the turn came back empty, a guard tells the model to say so instead of answering from memory.
The hard parts
Profiling before optimising: the double generation
The system was correct but slow, and the obvious suspects were wrong. Instrumenting each component of a turn showed LLM generation was 98% of wall time; tool execution was 0.8% and retrieval about 1%. The cost was structural: selection ran with tool_choice="auto", so when the model decided it needed no more tools it wrote the entire answer to say so, and synthesis then threw that away and wrote it again. An earlier estimate that blamed selection for 130s had lumped the discarded answer into the selection number.
Two failed fixes before the finish tool
Capping max_tokens on the selection round truncated long proposal tool calls, and llama-server started returning 500s on unparseable tool-call JSON. Streaming selection and aborting once prose began broke tools that narrate before calling. Both were reverted. What worked was a finish sentinel tool under tool_choice="required": the model always emits a call, real or finish, so selection never writes a throwaway answer. Later we kept "required" only for the first round, because forcing it every round made the model pack its answer into a tool argument; a leak check now drops those calls.
Grounding enforced in code, not in the prompt
Segment intelligence partitions deals by product category and industry and computes every statistic in Python from the store and the health engine. The model only writes prose over those numbers, offline, and a gate extracts every number from that prose; if any is missing from the computed report, the prose is discarded for a templated sentence. The chat loop has a matching guard. When every tool in a turn returns nothing, the model is told to say what was not found, a fix for a turn that once answered a failed CRM lookup with GTA 6 specs from memory.
Deciding what runs on which model, with data
To test moving synthesis to a smaller model, we froze the post-tool context once and ran the identical context on each arm. A bf16 8B gave only 1.11×, because decode was bandwidth-bound and it moved about as many bytes per token as the Q4 27B. A Q4 8B gave 2.72× wall and 3.8× decode at grounding parity, with weaker coaching style. A prompt-engineering pilot that seemed to close the style gap at n=4 lost at n=8. The 8B emitted the right tool on only 2 of 5 selection probes, so selection stayed on the 27B.
Serving on a shared GB10
Models that had coexisted started hitting OOM, and nothing in ps or free explained it. nvidia-smi's compute-apps query showed another team's container holding about 93 GB of the 119 GB unified memory; once it was stopped, both our models fit with room to spare. When serving later moved to a single atlas-35b NVFP4 model, long answers degenerated into repetition loops ("3. 3. 3."), which we traced to unset sampling: with no parameters it decoded greedily. Synthesis now runs at temperature 0.7 with explicit top-p and top-k, and tool selection stays near-greedy.
What I took from it
- Profile before optimising, and distrust your own earlier numbers. The latency was never in tool execution or retrieval; it was one wasted generation per turn, hidden in a metric attributed to the wrong stage.
- When a model keeps doing something you don't want, remove the option rather than adding an instruction. Capping tokens and aborting streams policed the behaviour and failed; the finish tool made it unrepresentable.
- Grounding has to be mechanical. A model told not to invent statistics still will; one whose output is checked against a whitelist of computed numbers cannot publish them.
- Write down the experiments that failed, with their numbers. The 8B-for-everything idea looked good at n=4 and lost at n=8, and recording that is what stops it being proposed again.
Stack
- Serving
- llama.cpp llama-server, atlas-35b (NVFP4), Qwen3 8B / 27B GGUF, OpenAI-compatible client
- Orchestration
- AdaptiveScheduler, ExecutionEngine (thread pool), LLMPlanner, Finish-tool loop
- Retrieval
- BM25 (rank-bm25 + Janome), fastembed MiniLM, RRF fusion, networkx
- App
- FastAPI, SSE, Next.js 15, React 19, SQLite (WAL), python-pptx
Timeline
- May 2026
Joined Otsuka in Tokyo as SDE intern.
- May – Jul 2026
Built Senpai: deal-health engine, orchestration engine, grounded retrieval.
- Jul 2026
Throughput work landed: 11 → 55 tok/s.
- Jul 2026
Latency work landed: finish-tool loop and the frozen-context model split.