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