ToolCallLM
Function-calling model training — Qwen3-8B, QLoRA, BFCL evaluation
- Role
- Co-builder · team of 2
- Timeline
- May 2026 – Jul 2026
- Context
- Otsuka Corporation · Tokyo
The outcome
- 97% BFCL accuracy on single-turn function calling with the fine-tuned Qwen3-8B.
- 50K+ synthetic function-calling examples produced for open-source LLM training.
- Single-turn BFCL (EXP5, Unsloth NF4 QLoRA): 97.0% simple_python, 95.5% multiple, 93.0% parallel, 92.0% parallel_multiple.
- Multi-turn BFCL (EXP5): 34% base, 13.5% miss_func, 24% miss_param, 20% long_context, against the base model's 34%, 38%, 24% and 25%.
- Gold-history evaluation measured 53–69% per-step accuracy against 13.5–34% rollout scores, placing the multi-turn failure in compounding errors rather than per-step policy.
- Training time per full run cut from 30–35 hours to about 24, with group_by_length measured at 2.12× on identical rows.
- Converged in a single epoch — 1,434 steps in 5h53m, final eval loss 0.1256 at 96.0% eval token accuracy.
- A public replay of 12 real EXP3 transcripts, with the two misses left in.
The challenge
We fine-tuned Qwen3-8B for function calling on a single NVIDIA DGX Spark during an internship at Otsuka. The work ran as a loop of experiments: build a training blend from public tool-calling datasets and generated data, train a LoRA or QLoRA adapter, score it on the Berkeley Function-Calling Leaderboard (BFCL), and let the failure analysis pick the next blend. Single-turn calling reached 92–97% on BFCL's non-live categories. Multi-turn stayed hard, and much of the second half went into finding out why.
An 8B model learns the shape of a tool call quickly. The agent loop is harder: read a tool result, notice an error, and issue the one corrective call. BFCL scores multi-turn trajectories all-or-nothing, so one bad step zeroes the sample. The effects we were chasing were a few points wide, so every stage had to be measurable and every number reproducible.
The system
Each stage is a small Python module behind a thin script, driven by YAML configs that inherit from one base file, so an experiment is a config diff rather than a code change. The sources arrive in incompatible shapes (xLAM single-turn calls, ToolACE and ToolMind trajectories), so they are blended before preprocessing, then normalised into one Pydantic-validated schema and written as a single JSONL with turn-aware truncation to 8,192 tokens. Data from a Qwen2.5-72B teacher and from an executor-grounded recovery generator passes through the same validator, so the trainer never sees where a row came from. Training began on TRL's SFTTrainer with BF16 LoRA and moved to Unsloth with an NF4-quantised base once runs passed a day; in both, labels are built explicitly so only assistant tokens carry loss. Checkpoints are validated before resume (trainer state, adapter config and weights all present), and a heartbeat watchdog stops a stalled run gracefully before forcing an exit. For evaluation the adapter is merged into full weights and handed to the official bfcl CLI, which serves it through vLLM; our runner only builds the environment and collects scores, so a BFCL upgrade touches one file. A separate gold-history evaluator scores multi-turn tasks one step at a time with perfect history, and its gap against the rollout score, read with the failure taxonomy, is what chose the next dataset.
The hard parts
The loss was being computed on the wrong tokens
Early runs used full-sequence causal loss, so gradients flowed through system prompts, tool schemas and tool results, and on tool-heavy examples those tokens dominated. Underneath sat a quieter bug: the pad token aliased EOS, and the collator masks labels wherever input matches the pad id, so turn terminators dropped out of the loss exactly where truncated JSON was the observed failure. We switched to response-only masking through the chat template's generation markers, explicit -100 labels outside assistant spans, and a dedicated pad token. Checkpoints from before the fix optimised a different objective, so we discarded those comparisons instead of reinterpreting them.
Single-turn training erased multi-turn
The xLAM-only control run lifted single-turn to an 85.95% average and drove multi-turn from the base model's 32% to 0.25%, because xLAM has no examples of continuing after a tool result. Blending in 40% ToolACE trajectories, picked by diversity sampling against an xLAM coverage report (new tool domains, deeper conversations, error-recovery turns), brought multi-turn back to 19.5% with single-turn at 86.63%. Negative examples, ordinary chat with irrelevant tool schemas injected, taught the model that a visible tool is not an instruction to call it.
Telling a bad policy from compounding errors
Five experiments left multi-turn BFCL between roughly 10% and 34%, and a rollout score cannot say whether the model picks wrong steps or fails to recover from its own. We built a teacher-forced evaluator that feeds the gold history up to each turn and scores that turn alone with BFCL's state checker: step accuracy was 53–69% against rollout scores of 13.5–34%, a 30–43 point gap in every category. An audit of the blend explained it. After a tool error, only 26.0% of next turns issued a corrected call, while 74% narrated or gave up, so we wrote a recovery generator that executes the real BFCL backend classes rather than letting an LLM invent tool results.
Runs measured in days on one GB10
BF16 LoRA runs on 40–50K rows at 8,192 tokens took 30–35 hours, so one bad run cost more than a day. Moving to Unsloth with an NF4 base and 8-bit AdamW freed memory, and group_by_length removed padding waste: an A/B on the same 256 rows measured 847.7 s against 399.8 s, a 2.12× speedup. A full run dropped to about 24 hours with no single-turn regression. Gradient checkpointing stayed on, because activations at batch 4 and 8,192 tokens measured around 87 GB without it.
Measuring with the right harness
Under BFCL's prompting handler the base model scored 0% on single-turn, because it answers in thinking-mode text rather than the call format that handler parses. Later, EXP5 was briefly scored with the wrong handler, which under-reported multi_turn_base by 12 points and miss_param by 13.5. The harness also classifies every failure (malformed versus truncated JSON, wrong tool versus hallucinated tool, missing versus invented arguments) and records the tool name and field path, so a drop traces back to examples instead of staying a count.
What I took from it
- Check what the loss is actually computed over before trusting any curve. Full-sequence loss on tool-heavy examples optimises conversation mimicry, and the training numbers look reasonable while it happens.
- Fix the harness before the model. A handler mismatch turned a 95% base model into 0%, and the same mistake later hid 12 points of multi-turn accuracy.
- Score steps, not only trajectories. All-or-nothing scoring hides whether a model is wrong or merely unrecovered, and those need opposite fixes.
- Audit what the data teaches after an error. Our blend modelled giving up nearly three times as often as recovering, and the model learned exactly that.
Stack
- Model
- Qwen3-8B, LoRA, QLoRA (NF4), PEFT
- Training
- TRL SFTTrainer, Unsloth, Flash Attention 2, W&B
- Hardware
- NVIDIA DGX Spark (GB10)
- Data
- xLAM-60k, ToolACE, ToolMind, Qwen2.5-72B-AWQ teacher, Pydantic
- Evaluation & serving
- BFCL v4, vLLM, FastAPI
Timeline
- May 2026
Pipeline and baselines; xLAM-only control run, then the xLAM and ToolACE blend.
- Jun 2026
ToolMind blend, Unsloth NF4 QLoRA, gold-history step evaluation and recovery-data generators.
- Jul 2026
Automated evals — 97% BFCL single-turn accuracy.
- Jul 2026
Wrap-up: a Japanese-language infographic summarising the experiments.