NerdlyBuilds

I Said 'Hi' to My Local Coding Model and Got a KeyError

A local Qwen3-Coder-Next crashed my agent on the word 'hi'. Sampling and tool count were red herrings. The culprit was a max_tokens default.

·7 min read

Illustration: a small robot on a dark workbench holding a long paper receipt that spills off the desk, next to a mini AI computer and a monitor full of red error lines

Back in June, the third DGX Spark in the Batcave finally got a job. Two Sparks already ran the Gemma models behind Jarvis (the setup I wrote about in The Brain in the Basement). The third one had never served a model at all. I wanted it running Qwen3-Coder-Next as a dedicated coding backend for Hermes, the agent framework Jarvis leaned on at the time.

Getting the model up was the easy part. Getting it to answer the word “hi” without crashing ate the rest of the day, and the answer turned out to be one unset number.

The part that went fine

Qwen3-Coder-Next is a mixture-of-experts coding model: 80B parameters in total, about 3B active per token, built to drive agent scaffolds. The 4-bit AWQ build is 48 GB, which fit next to the 3D-generation container that already lived on that box and holds 25 GB of GPU memory. That neighbor had to stay. It’s the skill Jarvis uses when I ask it to make a 3D model.

I served it with SGLang 0.5.13.post1. This is the launch line I ended up with:

source ~/sglang-venv/bin/activate
nohup python3 -m sglang.launch_server \
  --model-path ~/models/Qwen3-Coder-Next-AWQ-4bit \
  --host 0.0.0.0 --port 8001 \
  --mem-fraction-static 0.68 --context-length 65536 \
  --tool-call-parser qwen3_coder --served-model-name qwen3-coder-next \
  --trust-remote-code > ~/qwen-serve.log 2>&1 &

The memory fraction mattered. At 0.55 SGLang only had room for about 26K tokens of KV cache. At 0.68 it reported 290,938, and it still sat happily beside the 3D container.

Then I tested it the boring way, straight against the API. Code completion came back clean. A tool call (square {"n":1024}) came back as a proper structured call. Plain chat answered “Hello!” and stopped. Throughput was about 30.6 tokens per second. Model fine, server fine.

So I pointed Hermes at it and typed the simplest thing I could think of:

$ hermes -z "hi"
agent failed: 'final_response'

That’s a Python KeyError with the word “Error” filed off. The real traceback got swallowed because I was running it through a non-interactive pipe.

Wrong turn one: the model won’t stop

First, I proved it was my change. The code hadn’t moved since a job that worked two nights earlier (the git reflog showed the same commit), and putting Hermes’s default back on Gemma made it work again instantly. So: something about Qwen plus Hermes.

Then I turned on debug output, and it looked like a smoking gun. On a bare “hi,” Qwen didn’t say hello. It ran python3 --version. Then which python. Then ls /usr/bin. In another run it invented a task about Python 3 and fired off parallel web searches for it. Turn after turn, it never ended.

And I could see in the Hermes source exactly why that would crash. The agent loop only sets final_response in the branch where the model answers without calling a tool. A model that calls tools forever never gets there, so the result dict comes back without the key, and the caller’s result["final_response"] blows up.

It was a tidy story. Hermes handed the model 30 tools and a long, capability-heavy system prompt, and Qwen is trained to be aggressively proactive. Of course it manufactured work. I was ready to blame the harness and move on.

Wrong turn two: sampling

My other hunch was sampling, maybe a repetition penalty pushing the model into strange loops. That one was easy to test, so I tested it.

Neither side was adding a penalty. SGLang had no repetition, frequency or presence penalty set and pulled Qwen’s recommended settings from the model’s config. Hermes was overriding the temperature with its generic default, but that was all.

Then the direct A/B, with a terminal tool and “hi”:

  • temperature 0.2: clean “Hello! How can I help you today?”, zero tool calls
  • temperature 0.7: same
  • temperature 1.0, top_p 0.95, top_k 40 (Qwen’s recommendation): same
  • an aggressive “ALWAYS use tools, investigate the environment” system prompt plus five tools: still a clean stop

If sampling were the trigger, low temperature would have looped. It didn’t. Crossed off.

The side quest that cost me hours

For several rounds, the probes I added to the Hermes source didn’t show up at all. I’d edit a file, run it, and get the same output as before.

Stale .pyc files. And my first attempt at clearing them didn’t work either, because the find -prune I wrote quietly swallowed the -delete. This is the version I keep around now:

find . -type d -name __pycache__ | grep -v /venv/ | while read d; do rm -rf "$d"; done

Somewhere in there I also updated Hermes to current main, 543 commits ahead of what I’d been running. The crash didn’t care, which at least proved it wasn’t a version bug.

What was actually on the wire

What finally cracked it was going to the network instead of the logs. I put a tiny logging proxy between Hermes and the model server and captured the real outbound request.

Hermes was asking for max_tokens: 65536.

Nobody had set max_tokens, so it defaulted to the full context window. With about 38K tokens of tool definitions and system prompt already in the request, SGLang did the math and said no, correctly, with an HTTP 400:

Requested token count exceeds model's maximum context length of 65536 tokens.
You requested a total of 103934 tokens: 38398 input + 65536 completion.

Bar chart of the token budget against a 65,536-token context window. The failing request was 38,398 input tokens plus 65,536 requested output tokens, 103,934 in total, well past the limit. The fixed request was the same 38,398 input plus 8,192 output, 46,590 in total, comfortably under.

The rest was a chain reaction. Hermes read the 400 as “context exceeded” and tried to compress the conversation. The compression attempts failed. Then it exited through an early-return path that builds a result without a final_response key, and the caller’s hard bracket access did the rest.

Flow diagram of the crash: "hi" goes to Hermes, which sends max_tokens 65,536 to SGLang. SGLang returns HTTP 400. Hermes reads that as context exceeded, compression attempts fail, it returns early without a final_response key, and result["final_response"] raises KeyError.

So the error message wasn’t lying. It just named the last domino, three steps away from the first one. Whatever was going on with all those tool calls in the debug runs, it wasn’t what crashed the agent.

The fix: two lines that matter

Set max_tokens on purpose. Hermes reads it from the model block of the config, so the coding setup got this:

model:
  default: qwen3-coder-next
  context_length: 65536
  max_tokens: 8192

Input plus max_tokens has to stay under the context length. With the same 38K prompt, 8,192 leaves plenty of room. I could go to 16,384 if a task ever needs it.

Stop crashing on a missing key. The other half was a defensive edit in Hermes itself, so an early return degrades to an error message instead of a stack trace. The shape of it:

# before
return result["final_response"]
# after
return result.get("final_response") or result.get("error")

That one was a local edit to someone else’s code, which meant the next auto-update could silently undo it. I noted that and moved on, which is the kind of note I always regret.

I did keep one idea from the wrong turns. Hermes has no per-model toolset setting, so I gave Qwen its own profile with a lean set of about 12 tools instead of 30. In that profile it wrote and verified a factorial function, and when I put a task on Hermes’s kanban board assigned to the coding profile, the gateway spun up a Qwen worker that finished it in 29 seconds. Dispatch across two models on one board worked. The plan from there was Gemma for the thinking jobs and Qwen for the building ones.

How it ended

That nohup never became a systemd service. In August I took Qwen off the Spark to make room for Llama 3.3 70B, which I wanted for a creative prompt-writing workflow. Before removing it I checked that nothing depended on it: its provider had an empty fallback list, and a grep of the orchestrator, the HUD and the Discord bridge found no references.

Then it refused to die politely. kill -TERM did nothing. It took kill -9 on the launcher plus its sglang::scheduler and sglang::detokenizer children to free about 62 GB.

The replacement got a proper systemd user service, which promptly crash-looped every few minutes. The user had Linger=no, so every time my SSH session ended, systemd tore down the user instance and everything in it:

loginctl enable-linger "$USER"

And this month I retired Hermes entirely. The bug outlived the agent in the only place that counts: my habits.

What I do differently now

  • Always set max_tokens. Never let a client default it to “whatever the context allows.” Check that input plus output fits, with the real tool list included.
  • Look at the wire. A throwaway logging proxy ended hours of theories. Logs tell you what the app thinks happened. The request tells you what did.
  • Test your hypotheses, not just your fixes. The sampling A/B was cheap and killed a whole branch of guessing. The tool-loop story felt even better and was just as wrong.
  • Clear the bytecode cache after editing installed Python, and check that the clear actually worked.

The whole thing rhymes with the trading bot that sat looking healthy for six days. The loudest symptom is rarely the cause. This time, the cause was a number nobody typed.

Get an email when I publish something new