# Open WebUI Model Routing Diagnosis

When a user selects one model in Open WebUI but gets responses from a different one:

## Diagnostic Steps

1. **Check llama-swap logs** — `journalctl -u llama-swap --no-pager -n 100 | grep "POST /v1/chat/completions"`
   - Look for requests from Docker bridge IP (172.x.x.x = Open WebUI container)
   - Verify which model was actually hit

2. **Check Open WebUI database** — the truth is in SQLite:
   ```bash
   docker cp open-webui:/app/backend/data/webui.db /tmp/webui.db
   python3 -c "
   import sqlite3, json
   conn = sqlite3.connect('/tmp/webui.db')
   
   # Check which model was actually used for each message
   rows = conn.execute('SELECT role, model_id FROM chat_message ORDER BY created_at DESC LIMIT 10').fetchall()
   for r in rows: print(f'{r[0]}: model={r[1]}')
   
   # Check what model the chat metadata thinks it's on
   rows = conn.execute('SELECT chat FROM chat ORDER BY created_at DESC LIMIT 1').fetchall()
   chat = json.loads(rows[0][0])
   print(f'Chat models: {chat.get(\"models\",[])}')
   
   # Check the message tree for model changes mid-chat
   history = chat.get('history', {}).get('messages', {})
   for mid, msg in history.items():
       print(f'  {msg.get(\"role\")}: model={msg.get(\"models\",[])} children={msg.get(\"childrenIds\",[])[:3]}')
   conn.close()
   "
   ```

3. **Interpret the message tree:**
   - Each user message in the tree stores its `models` array = the model selected in the dropdown at send-time
   - If the tree shows model switching mid-chat, the user changed the model dropdown
   - The `chat_message` table stores `model_id` per assistant message = the model that actually generated it
   - A mismatch between chat metadata (`models` array) and `chat_message.model_id` means the UI shows one model but the backend used another

4. **Check for "Thinking" toggle override:**
   - When the Open WebUI "Thinking" toggle is ON, it sends `reasoning_effort` in the payload
   - Some frontend versions auto-select a reasoning-capable model when the toggle is flipped
   - Check the chat's message tree for sudden model switches that coincide with user messages that have different `models` values

5. **Check for model crash masquerading as wrong-model output:**
   - If llama-swap's `sendLoadingState: true` and a model crashes during load
   - Open WebUI renders the loading SSE event as if it were model output
   - The user sees `llama-swap loading model: X` as the response
   - Verify: check llama-swap logs for `exited prematurely` entries

## Common Causes
- **User switched model mid-chat** — the DB tree shows different models per turn
- **Thinking toggle auto-route** — frontend overrides selected model with a reasoning-capable one
- **Model crash** — `sendLoadingState` SSE rendered as response text
- **Open WebUI model list cache stale** — DB config stores hardcoded `model_ids` that don't match llama-swap's actual model list
