Built for Agents · Controlled by Humans
错误码与 Agent 接口
给 agent 和写 agent 的人看:出错时怎么判断、怎么查自己的状态、怎么给自己设上限。机器可读版本:openapi.json、pricing.json、llms.txt。
1. 错误长什么样
保留 OpenAI / Anthropic 原有的字段(老客户端照常能用),另外加上这些字段。看 code 和 retryable 决定下一步,不要解析 message 的文字。
{
"error": {
"message": "Upstream models are busy right now; ... 上游模型暂时都忙 ...",
"type": "server_error",
"code": "upstream_busy", // 固定的英文错误码,见下表
"retryable": true, // 原样再发一次有可能成功
"action": "retry_or_switch", // 建议的下一步
"retry_after": 15, // 秒;和 Retry-After 响应头一致
"suggested_model": "monk", // 现在更健康的组合(只在上游类错误里出现)
"docs_url": "https://monk.party/help/errors.html#upstream_busy",
"request_id": "req_..." // 找客服时带上;响应头 X-Request-Id 里也有
}
}
Anthropic 格式(/v1/messages)是 {"type":"error","error":{...}},上面这些字段放在里面的 error 对象中。
2. 错误码
| code | HTTP | 可重试 | action | 说明 |
|---|---|---|---|---|
invalid_api_key | 401 | 否 no | fix_key检查 Key / check the key | Invalid or inactive API key. Key 无效或已停用。 |
key_expired | 401 | 否 no | renew续费 / renew | The subscription for this key has expired. Renew at https://monk.party/me/ — the key stays the same. 这把 Key 的订阅已到期,续费后 Key 不变。 |
daily_quota_exceeded | 429 | 是 yes | wait_reset等每日重置(UTC 0 点)/ wait for the daily reset | Daily usage limit reached for this key. 这把 Key 今天的用量到上限了。 |
budget_exceeded | 429 | 是 yes | wait_reset等每日重置(UTC 0 点)/ wait for the daily reset | The daily budget you set for this key is used up. 你给这把 Key 设的每日预算用完了。 |
agent_budget_exceeded | 429 | 是 yes | wait_reset等每日重置(UTC 0 点)/ wait for the daily reset | The daily request budget set for this agent is used up. 给这个 agent 设的每日次数用完了。 |
loop_detected | 429 | 是 yes | fix_loop修掉循环 / fix the loop | The same request was sent too many times in a short period; this agent is paused. 短时间内重复发了太多次同样的请求,这个 agent 已暂停。 |
agent_cooldown | 429 | 是 yes | wait按 retry_after 等 / wait retry_after | This agent is paused after a loop was detected. 这个 agent 因为疑似死循环被暂停中。 |
key_cooldown | 429 | 是 yes | wait按 retry_after 等 / wait retry_after | This key is cooling down. 这把 Key 正在冷却。 |
rate_limit_exceeded | 429 | 是 yes | wait按 retry_after 等 / wait retry_after | Sustained usage limit reached. 持续用量到上限了。 |
burst_limit | 429 | 是 yes | slow_down降低并发和频率 / slow down | Too many requests in a burst; slow down. 短时间请求太密,请放慢。 |
concurrency_limit | 429 | 是 yes | slow_down降低并发和频率 / slow down | Too many requests in flight. 同时进行的请求太多。 |
upstream_busy | 503 | 是 yes | retry_or_switch等一下重试,或换 suggested_model / retry or switch | Upstream models are busy. 上游模型暂时都忙。 |
upstream_quota | 503 | 是 yes | retry_or_switch等一下重试,或换 suggested_model / retry or switch | Upstream allowance is used up for now. 上游额度暂时用完了。 |
upstream_unavailable | 502 | 是 yes | retry_or_switch等一下重试,或换 suggested_model / retry or switch | Upstream service is unavailable. 上游服务暂时不可用。 |
upstream_error | 502 | 是 yes | retry_or_switch等一下重试,或换 suggested_model / retry or switch | The upstream model returned an error. Monk did not charge for this request. 上游模型出错了,这次请求不计费。 |
model_not_available | 503 | 是 yes | retry_or_switch等一下重试,或换 suggested_model / retry or switch | This model is not available right now. 这个模型现在不可用。 |
model_not_found | 404 | 否 no | fix_request改请求 / fix the request | Unknown model for this key. 这把 Key 不能用这个模型。 |
context_length_exceeded | 400 | 否 no | compact压缩对话 / compact | The conversation exceeds the model's context window. Compact it and retry. 对话超出上下文窗口,压缩后再试。 |
invalid_json | 400 | 否 no | fix_request改请求 / fix the request | Request body is not valid JSON. 请求体不是合法 JSON。 |
invalid_request | 400 | 否 no | fix_request改请求 / fix the request | The request was rejected. 请求被拒绝。 |
invalid_agent_id | 400 | 否 no | fix_request改请求 / fix the request | X-Monk-Agent must be 1-64 characters of letters, digits, . _ : - X-Monk-Agent 只能用 1-64 个字母、数字或 . _ : - |
insufficient_balance | 402 | 否 no | top_up充值 / top up | Opus wallet balance is too low. Top up at https://monk.party/me/#opus Opus 余额不足,请到用户中心充值。 |
daily_limit_exceeded | 429 | 是 yes | wait_reset等每日重置(UTC 0 点)/ wait for the daily reset | Daily Opus spending limit reached. 今天的 Opus 花费到上限了。 |
idempotency_key_reused | 422 | 否 no | fix_request改请求 / fix the request | This Idempotency-Key was already used with a different request body. 这个 Idempotency-Key 已经用于另一个请求体。 |
idempotency_in_progress | 409 | 是 yes | wait按 retry_after 等 / wait retry_after | A request with this Idempotency-Key is still running. 同一个 Idempotency-Key 的请求还在执行。 |
idempotency_replay_unavailable | 409 | 否 no | use_new_key换一个新的 Idempotency-Key / use a new key | That request already completed, but its result was too large to keep for replay. It was not run again. 那次请求已完成,但结果太大没有保存,不会再执行。 |
client_closed | 499 | 是 yes | retry重试 / retry | Client closed the request. 客户端关闭了请求。 |
3. 查自己的状态:GET /v1/me
用 Key 调用,Key 被限流或订阅过期时也能查。返回订阅到期时间、今日用量和各项上限、Opus 余额、你设的预算、按 Agent ID 分开的用量、模型状态和需要注意的事(notices)。
curl https://monk.party/v1/me -H "Authorization: Bearer sk-monk-..."
4. 服务状态:GET /v1/status
不用 Key。每个模型组合最近 15 分钟的成功率和状态(ok / degraded / down)。发请求前看一眼,避开 down 的组合。/v1/models 里每个模型的 monk.status 是同一份数据。
5. Agent ID:X-Monk-Agent
一把 Key 下面跑多个 agent 时,每个请求带上 X-Monk-Agent: 名字(1–64 个字母、数字或 . _ : -)。不带就算 default。用量、预算、暂停都按 agent 分开算,一个 agent 出问题不拖累别的。
6. 给自己设上限:POST /v1/me/budgets
curl https://monk.party/v1/me/budgets -H "Authorization: Bearer sk-monk-..." -H "Content-Type: application/json" \
-d '{"daily_cost_cny": 5, "agents": {"coder-1": {"daily_requests": 500}}}'
daily_cost_cny:这把 Key 每天最多用多少(只能比平台上限低)。daily_requests:某个 agent 每天最多多少次。填 null 取消。超了返回 budget_exceeded / agent_budget_exceeded,UTC 0 点重置。
7. 死循环保护
同一把 Key、同一个 agent、同一个请求体,10 分钟内发了 20 次,就判为死循环:返回 loop_detected,这个 agent 暂停 15 分钟(期间返回 agent_cooldown),同时在用户中心的消息中心通知你。其他 agent 不受影响。
8. 重试不重复执行:Idempotency-Key
请求带上 Idempotency-Key: 任意唯一字符串(最长 200)。记录保存 24 小时,按「Key + Idempotency-Key」区分。
| 上一次同一个键 | 这次 | 计费 |
|---|---|---|
| 请求体不一样 | 拒绝:idempotency_key_reused(422) | 不计 |
| 还在执行 | 拒绝:idempotency_in_progress(409),按 retry_after 再试 | 不计 |
| 已成功(含流式) | 原样重放,响应头 Idempotent-Replayed: true | 不再计 |
| 在第一个字之前失败 | 重新执行 | 失败那次不计 |
| 流到一半断了 | 重新执行 | 上游中断的那次不计(Opus 退回);客户端自己断开的按已产生的计 |
结果超过 4 MB 的只记「已完成」不保存内容,再用同一个键会返回 idempotency_replay_unavailable,不会再执行。
9. 每个响应都带的头
| 响应头 | 意思 |
|---|---|
X-Request-Id | 这次请求的编号 |
X-Monk-Agent | 算在哪个 agent 上 |
x-ratelimit-limit-requests / x-ratelimit-remaining-requests | 最近 1 小时的次数上限 / 剩余(月卡 Key) |
x-monk-ratelimit-6h-limit / -remaining | 最近 6 小时的次数上限 / 剩余 |
x-monk-daily-cost-limit / -used | 今日用量上限 / 已用(元) |
x-monk-plan-expires-at | 订阅到期时间 |
x-monk-budget-remaining | 你设的每日预算还剩多少(元) |
x-monk-agent-requests-remaining | 这个 agent 今天还能发几次 |
x-monk-lane | 这次走的线路:interactive(交互,好模型在前)或 batch(不开流式、不带工具、对话不超过 4 条的一次性请求,先用免费模型,全失败再退回交互线) |
x-monk-notice | 需要注意的事:plan_expiring、daily_cost_80、hour_requests_80、agent_budget_last |