构建 AI 系统,无需硬编码模型选择。
每次请求附带一条策略。unhardcoded 过滤实时模型目录,选出满足规则的成本最低的候选模型,通过你的提供商密钥运行,并返回决策追踪记录。
fp 301140696-1054914287
模型路由循环
一条策略决定一次调用。相同输入、相同目录、相同决策。每次如此,且有书面记录。
top_k 回退级联。工作流模式
工作流是由路由步骤组成的有界无环图。每个节点携带自己的策略并独立路由;整个图生成一条统一的追踪记录。这正是 unhardcoded 从"带规则的路由"演变为一个完整系统的地方。
低成本分类,按质量下限起草回复,最后由一个强力 no-log 守卫在发送前决定是否拒绝。
关键点:最后一步是由策略强制执行的门控,可以中止发送,而不仅仅是希望它有效。
查看 flow_ir
["flow", {
"u": {"kind": "input"},
"t": {"kind": "llm", "system": "Classify the ticket and extract the account id as JSON.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["has_cap", "supports_json_mode"]],
["neg", ["normalize", ["field", "price_out"]]], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["u"]},
"d": {"kind": "llm", "system": "Write a reply using the ticket and the triage.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["cmp", "bench_intelligence", "ge", 0.55]],
["neg", ["normalize", ["field", "price_out"]]], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["u", "t"], "template": "Ticket:\n$1\n\nTriage:\n$2"},
"g": {"kind": "llm", "system": "Check brand voice, PII, refund limits. Refuse if any fail.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["is", "no_log"]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["d"]},
"out": {"kind": "output", "inputs": ["g"]}
}]先用低成本模型生成初稿,再由强力批评者列出缺陷,最后重写并修复每个问题。
关键点:在预算范围内实现质量跃升——大部分令牌消耗在低成本模型上。
查看 flow_ir
["flow", {
"u": {"kind": "input"},
"d": {"kind": "llm", "system": "Draft an answer.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["neg", ["normalize", ["field", "price_out"]]], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["u"]},
"c": {"kind": "llm", "system": "Critique the draft: list concrete flaws and gaps.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["d"]},
"r": {"kind": "llm", "system": "Rewrite the answer, fixing every point in the critique.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["u", "d", "c"], "template": "Question:\n$1\n\nDraft:\n$2\n\nCritique:\n$3"},
"out": {"kind": "output", "inputs": ["r"]}
}]同一强力策略下进行 N 次种子采样,然后由评判者从中择优选出胜出结果。
关键点:sample 以确定性方式在排名靠前的候选模型中分散采样——可复现的多样性,而非随机。
查看 flow_ir
["flow", {
"u": {"kind": "input"},
"n1": {"kind": "llm", "system": "Answer the question.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["sample", 0.5], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["u"]},
// n2, n3: the same policy, two more seeded draws
"j": {"kind": "llm", "system": "Pick the single best candidate; return it verbatim.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["n1", "n2", "n3"], "template": "A:\n$1\n\nB:\n$2\n\nC:\n$3"},
"out": {"kind": "output", "inputs": ["j"]}
}]三个按族系固定的模型并行起草,第四个模型将它们合成为一个更优的单一答案。
关键点:family_eq 锁定了确切的模型系列,因此评审团可复现——而不是"今天最便宜的那个"。
查看 flow_ir
["flow", {
"u": {"kind": "input"},
"a": {"kind": "llm", "system": "Draft an answer.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["family_eq", "gemini-3.1-pro-preview"]],
["zero"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]], "inputs": ["u"]},
// b: family_eq "claude-opus-4-8" · c: family_eq "deepseek-v4-flash"
"f": {"kind": "llm", "system": "Synthesize the single best answer from the drafts.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["a", "b", "c"]},
"out": {"kind": "output", "inputs": ["f"]}
}]推理器和编码器在不同过滤条件下并行运行,之后由合并步骤整合。
关键点:每个分支为其任务(推理 vs 编码)选择最合适的模型,而非让一个模型包揽所有工作。
查看 flow_ir
["flow", {
"u": {"kind": "input"},
"rz": {"kind": "llm", "system": "Reason through the problem step by step.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["is", "cap_reasoning"]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]], "inputs": ["u"]},
"cd": {"kind": "llm", "system": "Produce any code the problem needs.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["cmp", "bench_coding_rank", "le", 5]],
["field", "bench_coding"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]], "inputs": ["u"]},
"m": {"kind": "llm", "system": "Merge the reasoning and the code into one answer.",
"policy": ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"], ["argmax"], ["id"], ["always", {"action": "next_candidate"}]],
"inputs": ["rz", "cd"], "template": "Reasoning:\n$1\n\nCode:\n$2"},
"out": {"kind": "output", "inputs": ["m"]}
}]快速入门
unhardcoded 兼容 OpenAI 接口。相比普通调用,仅需三处改动。
-
将 SDK 指向托管地址。修改
baseURL;SDK 的其余部分保持不变。const client = new OpenAI({ baseURL: "https://<your-host>/v1", apiKey: process.env.UNHARDCODED_KEY, }); -
在调用中附加策略。在后端构建
policy_ir,与messages一起发送。路由来自策略,因此model只是追踪记录标签。const res = await client.chat.completions.create({ model: "policy:support", policy_ir: ["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["cmp", "bench_intelligence", "ge", 0.5]], // filter ["neg", ["normalize", ["field", "price_out"]]], // rank: cheapest ["argmax"], ["id"], ["always", {"action": "next_candidate"}]], messages, }); -
读取追踪记录。响应中包含决策内容:所选模型、经过排序和拒绝的候选模型,以及策略指纹(fingerprint)。
{ "chosen": { "model_family": "deepseek-v4-pro", "price_out": 1.5 }, "trace": { "policy_fingerprint": "301140696-1054914287", "rejected": [{ "model_family": "deepseek-v4-flash", "reason": "cmp bench_intelligence ge 0.5" }], "total_latency_ms": 425 } }
策略预设
以卡片形式呈现的常用路由模式。阅读规则,复制策略,调整下限和上限。
bench_intelligence ge 0.5price_out查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]],
["cmp", "bench_intelligence", "ge", 0.5]],
["neg", ["normalize", ["field", "price_out"]]],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["add",
["scale", 0.6, ["normalize", ["field", "bench_intelligence"]]],
["scale", 0.4, ["neg", ["normalize", ["field", "price_out"]]]]],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]bench_intelligence查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]]],
["field", "bench_intelligence"],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]is cap_reasoning查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["is", "cap_reasoning"]],
["field", "bench_intelligence"],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]is in_imageprice_out查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["is", "in_image"]],
["neg", ["normalize", ["field", "price_out"]]],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]context ge 200000price_out查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]], ["cmp", "context", "ge", 200000]],
["neg", ["normalize", ["field", "price_out"]]],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]bench_agentic_rank le 5bench_agentic查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]],
["has_cap", "supports_tools"], ["cmp", "bench_agentic_rank", "le", 5]],
["field", "bench_agentic"],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]is has_tee · is no_log查看 JSON
["policy", ["and", ["meets_req"], ["not", ["is", "disabled"]],
["is", "has_tee"], ["is", "no_log"]],
["field", "bench_intelligence"],
["argmax"], ["id"], ["always", {"action": "next_candidate"}]]每次决策都留下凭证
追踪记录是模型选择过程的结构化、可重放记录:哪些模型被考虑过,每个模型为何通过或被拒绝,执行了什么,以及如何复现。
sha256 计算。工作流还包含 flow_fingerprint。参考文档
完整规范,独立于阅读路径之外。
GitHub 源码:unhardcoded(托管服务)· unhardcoded-engine(策略引擎)