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: external-model-delegation
3description: UserPromptSubmit hook pattern that classifies prompts into six cost tiers and delegates to external model CLIs
4when-to-use: When configuring or debugging prompt-based delegation to qwen3, deepseek, kimi, or codex
5user-invocable: false
6effort: medium
7---
8
9# External Model Delegation Pattern
10
11A `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.
12
13## Tier routing table
14
15| Tier | Delegation command | Cost |
16|------|-------------------|------|
17| QWEN | `qwen3 "prompt"` | $0 (local Ollama) |
18| DEEPSEEK_FLASH | `deepseek --flash "prompt"` | $0.14 / $0.28 per M tokens |
19| DEEPSEEK_PRO | `deepseek --pro "prompt"` | $0.44 / $0.87 per M tokens |
20| KIMI | `kimi --quiet -p "prompt"` | $0.60 / $2.50 per M tokens |
21| CODEX | `codex exec` | varies |
22| CLAUDE | handle natively | $3-5 / $15-25 per M tokens |
23
24## Delegation script pattern
25
26Each script is a self-contained executable in `~/bin/` that accepts a prompt and writes the response to stdout:
27
28```
29~/bin/
30├── qwen3 # Shell: curl to local Ollama API
31├── kimi # Shell: execs Kimi CLI binary
32├── deepseek # Python: httpx to DeepSeek Anthropic-compat API
33└── route-task # Shell + qwen3: classifies prompt into tier
34```
35
36### Script contract
37
381. Accept prompt as first argument: `qwen3 "what is 2+2"`
392. Support `--flash` / `--pro` model flags (deepseek)
403. Support `--quiet` mode flag (kimi)
414. Write response to stdout, errors to stderr
425. Exit 0 on success, non-zero on error
43
44### Writing a new delegation script
45
46```bash
47#!/bin/bash
48# Minimal delegator template
49PROMPT="$1"
50API_KEY="${EXTERNAL_API_KEY:-}"
51# Call external API, write result to stdout
52curl -s https://api.example.com/chat \
53 -H "Authorization: Bearer $API_KEY" \
54 -d "$(jq -n --arg p "$PROMPT" '{prompt: $p}')" \
55 | jq -r '.response'
56```
57
58## Routing hook flow
59
60```
61User types prompt
62 ↓
63UserPromptSubmit hook fires
64 ↓
65qwen3 classifies into tier (QWEN|DEEPSEEK_FLASH|DEEPSEEK_PRO|KIMI|CODEX|CLAUDE)
66 ↓
67Hook injects additionalContext: "Run: <delegation-command>"
68 ↓
69Claude reads context, spawns delegation script, returns output
70 ↓
71User sees response from the delegated model
72```
73
74## Classification tiers
75
76| Tier | Task types |
77|------|-----------|
78| QWEN | grep, find, regex, shell, syntax lookups, log reading, short summaries |
79| DEEPSEEK_FLASH | Simple code, boilerplate, CRUD, test writing, small fixes, config |
80| DEEPSEEK_PRO | Multi-file features, refactors, debugging, medium coding, docs |
81| KIMI | Single-file review, medium reasoning, commit messages, diff summaries |
82| CODEX | Bulk generation, mechanical changes across many files |
83| CLAUDE | Architecture, security, complex debugging, system design, quality-critical |
84
85## Environment
86
87```bash
88# Required env vars (set in ~/.zshrc)
89export DEEPSEEK_API_KEY="sk-..." # For deepseek delegator
90export OPENAI_API_KEY="sk-..." # For codex CLI
91# Ollama must be running locally for qwen3 classification + delegation
92```