External Model Delegation Pattern
A UserPromptSubmit hook classifies every user prompt into one of six cost/performance tiers. The hook injects additionalContext instructing Claude to run a specific delegation script and return the output.
Tier routing table
| Tier |
Delegation command |
Cost |
| QWEN |
qwen3 "prompt" |
$0 (local Ollama) |
| DEEPSEEK_FLASH |
deepseek --flash "prompt" |
$0.14 / $0.28 per M tokens |
| DEEPSEEK_PRO |
deepseek --pro "prompt" |
$0.44 / $0.87 per M tokens |
| KIMI |
kimi --quiet -p "prompt" |
$0.60 / $2.50 per M tokens |
| CODEX |
codex exec |
varies |
| CLAUDE |
handle natively |
$3-5 / $15-25 per M tokens |
Delegation script pattern
Each script is a self-contained executable in ~/bin/ that accepts a prompt and writes the response to stdout:
~/bin/
├── qwen3 # Shell: curl to local Ollama API
├── kimi # Shell: execs Kimi CLI binary
├── deepseek # Python: httpx to DeepSeek Anthropic-compat API
└── route-task # Shell + qwen3: classifies prompt into tier
Script contract
- Accept prompt as first argument:
qwen3 "what is 2+2"
- Support
--flash / --pro model flags (deepseek)
- Support
--quiet mode flag (kimi)
- Write response to stdout, errors to stderr
- Exit 0 on success, non-zero on error
Writing a new delegation script
#!/bin/bash
# Minimal delegator template
PROMPT="$1"
API_KEY="${EXTERNAL_API_KEY:-}"
# Call external API, write result to stdout
curl -s https://api.example.com/chat \
-H "Authorization: Bearer $API_KEY" \
-d "$(jq -n --arg p "$PROMPT" '{prompt: $p}')" \
| jq -r '.response'
Routing hook flow
User types prompt
↓
UserPromptSubmit hook fires
↓
qwen3 classifies into tier (QWEN|DEEPSEEK_FLASH|DEEPSEEK_PRO|KIMI|CODEX|CLAUDE)
↓
Hook injects additionalContext: "Run: <delegation-command>"
↓
Claude reads context, spawns delegation script, returns output
↓
User sees response from the delegated model
Classification tiers
| Tier |
Task types |
| QWEN |
grep, find, regex, shell, syntax lookups, log reading, short summaries |
| DEEPSEEK_FLASH |
Simple code, boilerplate, CRUD, test writing, small fixes, config |
| DEEPSEEK_PRO |
Multi-file features, refactors, debugging, medium coding, docs |
| KIMI |
Single-file review, medium reasoning, commit messages, diff summaries |
| CODEX |
Bulk generation, mechanical changes across many files |
| CLAUDE |
Architecture, security, complex debugging, system design, quality-critical |
Environment
# Required env vars (set in ~/.zshrc)
export DEEPSEEK_API_KEY="sk-..." # For deepseek delegator
export OPENAI_API_KEY="sk-..." # For codex CLI
# Ollama must be running locally for qwen3 classification + delegation
1---2name: alinaqi-claude-bootstrap-external-model-delegation3description: External Model Delegation Pattern4---5# External Model Delegation Pattern67A `UserPromptSubmit` hook classifies every user prompt into one of six cost/performance tiers. The hook injects `additionalContext` instructing Claude to run a specific delegation script and return the output.89## Tier routing table1011| Tier | Delegation command | Cost |12|------|-------------------|------|13| QWEN | `qwen3 "prompt"` | $0 (local Ollama) |14| DEEPSEEK_FLASH | `deepseek --flash "prompt"` | $0.14 / $0.28 per M tokens |15| DEEPSEEK_PRO | `deepseek --pro "prompt"` | $0.44 / $0.87 per M tokens |16| KIMI | `kimi --quiet -p "prompt"` | $0.60 / $2.50 per M tokens |17| CODEX | `codex exec` | varies |18| CLAUDE | handle natively | $3-5 / $15-25 per M tokens |1920## Delegation script pattern2122Each script is a self-contained executable in `~/bin/` that accepts a prompt and writes the response to stdout:2324```25~/bin/26├── qwen3 # Shell: curl to local Ollama API27├── kimi # Shell: execs Kimi CLI binary28├── deepseek # Python: httpx to DeepSeek Anthropic-compat API29└── route-task # Shell + qwen3: classifies prompt into tier30```3132### Script contract33341. Accept prompt as first argument: `qwen3 "what is 2+2"`352. Support `--flash` / `--pro` model flags (deepseek)363. Support `--quiet` mode flag (kimi)374. Write response to stdout, errors to stderr385. Exit 0 on success, non-zero on error3940### Writing a new delegation script4142```bash43#!/bin/bash44# Minimal delegator template45PROMPT="$1"46API_KEY="${EXTERNAL_API_KEY:-}"47# Call external API, write result to stdout48curl -s https://api.example.com/chat \49 -H "Authorization: Bearer $API_KEY" \50 -d "$(jq -n --arg p "$PROMPT" '{prompt: $p}')" \51 | jq -r '.response'52```5354## Routing hook flow5556```57User types prompt58 ↓59UserPromptSubmit hook fires60 ↓61qwen3 classifies into tier (QWEN|DEEPSEEK_FLASH|DEEPSEEK_PRO|KIMI|CODEX|CLAUDE)62 ↓63Hook injects additionalContext: "Run: <delegation-command>"64 ↓65Claude reads context, spawns delegation script, returns output66 ↓67User sees response from the delegated model68```6970## Classification tiers7172| Tier | Task types |73|------|-----------|74| QWEN | grep, find, regex, shell, syntax lookups, log reading, short summaries |75| DEEPSEEK_FLASH | Simple code, boilerplate, CRUD, test writing, small fixes, config |76| DEEPSEEK_PRO | Multi-file features, refactors, debugging, medium coding, docs |77| KIMI | Single-file review, medium reasoning, commit messages, diff summaries |78| CODEX | Bulk generation, mechanical changes across many files |79| CLAUDE | Architecture, security, complex debugging, system design, quality-critical |8081## Environment8283```bash84# Required env vars (set in ~/.zshrc)85export DEEPSEEK_API_KEY="sk-..." # For deepseek delegator86export OPENAI_API_KEY="sk-..." # For codex CLI87# Ollama must be running locally for qwen3 classification + delegation88```