Tracing Qwen Code

MLflow Tracing provides automatic tracing for Qwen Code, Alibaba's open-source terminal-based coding agent. After setup, MLflow will automatically capture traces of your Qwen Code conversations including:
- User prompts and assistant responses
- Tool usage (file operations, shell commands, etc.)
- Token usage (input, output, and total tokens)
- Session metadata including working directory and user
Traces are created after each conversation turn.
Setup
Requirements
- MLflow tracking server running
- Qwen Code installed (
npm install -g @qwen-code/qwen-code) - Node.js 18+
- The
@mlflow/qwen-codenpm package
Step 1: Install the Tracing Plugin
npm install -g @mlflow/qwen-code
If you'd rather not install globally, you can invoke the bin through npx (e.g. npx @mlflow/qwen-code setup) — everything below works the same way.
Step 2: Run the Setup Command
Running mlflow-qwen-code setup starts an interactive flow for setting up the tracing hook.
~/my-project $ mlflow-qwen-code setup
Configure MLflow tracing for Qwen Code
? Where should MLflow tracing be installed?
● Project ./.qwen/ (default)
○ User ~/.qwen/
↑/↓ to move, enter to select
✓ Registered Stop hook in /path/to/project/.qwen/settings.json
MLflow tracking URI [http://localhost:5000] http://localhost:5000
MLflow experiment ID [0] 1
✓ Wrote tracing config to /path/to/project/.qwen/mlflow-tracing.json
Next steps
1. Start the MLflow tracking server in a separate terminal:
mlflow server --port 5000
2. Launch qwen — traces appear at http://localhost:5000 after each turn.
Pass --non-interactive / -y to skip the interactive flow and use the default values.
Older Qwen Code versions (prior to 0.14) disabled the hook system by default and required a top-level "hooksConfig": { "enabled": true } entry to turn it on. Current versions ignore that setting and run hooks unconditionally — it's safe to leave in if you already have it, but no longer needed.
Step 3: Start the MLflow Tracking Server
The MLflow server must be running to receive traces. Start it in a separate terminal:
mlflow server --port 5000
Step 4: Launch Qwen Code
qwen
Traces become visible at http://localhost:5000 after each turn completes.
MLFLOW_TRACKING_URI and MLFLOW_EXPERIMENT_ID environment variables (if set in the shell that launched qwen) override the values written to mlflow-tracing.json. This is convenient for one-off overrides in CI or when switching between local and Databricks endpoints.
How It Works
- The Stop hook fires after each Qwen Code conversation turn
- Qwen passes the session ID and transcript path via stdin
- The plugin reads the chronological ChatRecord JSONL transcript and matches each
functionCallto itstool_resultbycallId - An MLflow trace is created with an AGENT root span (
qwen_code_conversation) and LLM/TOOL child spans. LLM spans use OpenAI chat format ({model, messages}inputs,{choices}outputs)
Configuration Examples
Write these values via mlflow-qwen-code setup or by editing ~/.qwen/mlflow-tracing.json directly:
{ "trackingUri": "http://localhost:5000", "experimentId": "0" }
{ "trackingUri": "databricks", "experimentId": "123456789" }
Per-run overrides via environment variables (take precedence over the config file):
MLFLOW_TRACKING_URI=databricks MLFLOW_EXPERIMENT_ID=123456789 qwen
Token Usage and Cost
MLflow automatically tracks token usage for each LLM call within Qwen Code conversations. See the Token Usage and Cost Tracking documentation for details.
Troubleshooting
Common Issues
Tracing not working:
- Confirm
mlflow-qwen-codeis on PATH: runwhich mlflow-qwen-code(should resolve afternpm install -g @mlflow/qwen-code) - Confirm
~/.qwen/mlflow-tracing.jsonexists and contains a validtrackingUri. Re-runmlflow-qwen-code setupif the file is missing or stale. - Verify Node.js 18+ is being used (required for
fetchsupport) - On Qwen Code versions older than 0.14, hooks are disabled by default — add a top-level
"hooksConfig": { "enabled": true }entry to enable them
Missing traces:
- Traces are created per conversation turn, not per session
- Verify the MLflow tracking server is accessible
- Check the Qwen debug log (
~/.qwen/debug/latest) for hook execution status
Duplicate traces:
- Ensure the hook is only registered once — check both
~/.qwen/settings.jsonand.qwen/settings.jsonfor duplicate entries
Disable Tracing
Remove the hooks section from settings.json. Existing traces are preserved.
(On older Qwen Code versions that still honor hooksConfig, setting "hooksConfig": { "enabled": false } will also disable the hook system without removing the entry.)