When a workflow grows, prompt size issues often appear in the wrong place. A Dify node may fail after a retrieval step, Cursor may send a larger editing context than expected, and a Node.js service may combine user text with internal instructions. The provider receives one request, but the team has to debug three tools.
This tutorial builds a small prompt size sentinel before requests reach Vector Engine. Vector Engine can serve as an OpenAI-compatible API gateway and as part of an LLM API provider layer, but the calling tools still need a shared request contract: Base URL, API Key source, model name, payload size, and a clear model_not_found path.
Registration URL: https://api.vectorengine.cn/register?aff=Igym
Define the contract
Use one shared connection shape across the tools:
Provider: Vector Engine
Base URL: https://api.vectorengine.cn/v1
API Key: VECTOR_ENGINE_API_KEY
model name: gpt-4o-mini
callers: Dify, Cursor, Node.js
sentinel rule: block oversized prompts before sending
The sentinel is not a replacement for provider limits. It is a local guard that makes failures easier to explain before a request leaves the application.
Create the Node.js sentinel
Create prompt-size-sentinel.mjs:
const baseURL = "https://api.vectorengine.cn/v1";
const apiKey = process.env.VECTOR_ENGINE_API_KEY;
const model = process.env.VECTOR_ENGINE_MODEL || "gpt-4o-mini";
const maxChars = Number(process.env.MAX_PROMPT_CHARS || 12000);
if (!apiKey) {
throw new Error("VECTOR_ENGINE_API_KEY is missing");
}
function estimatePayload(messages) {
return messages.reduce((total, message) => {
return total + `${message.role}:${message.content}`.length;
}, 0);
}
function assertPromptBudget(messages) {
const promptChars = estimatePayload(messages);
if (promptChars > maxChars) {
const error = new Error("prompt_budget_exceeded");
error.details = { promptChars, maxChars, model };
throw error;
}
return promptChars;
}
const messages = [
{ role: "system", content: "Return a short engineering checklist." },
{ role: "user", content: process.argv.slice(2).join(" ") || "Check Vector Engine setup." }
];
const promptChars = assertPromptBudget(messages);
const response = await fetch(`${baseURL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model,
messages
})
});
const text = await response.text();
console.log(JSON.stringify({
status: response.status,
baseURL,
model,
promptChars,
responsePreview: text.slice(0, 500)
}, null, 2));
if (!response.ok) process.exit(1);
Run it with:
export VECTOR_ENGINE_API_KEY="sk-..."
export VECTOR_ENGINE_MODEL="gpt-4o-mini"
export MAX_PROMPT_CHARS="12000"
node prompt-size-sentinel.mjs "Summarize the rollout notes for Dify and Cursor."
The script prints only operational metadata. Do not log the API Key or full prompts in shared tickets.
Add the same rule to Dify
In Dify, add a small preprocessing node before the model call:
1. Collect the text that will be sent to the model.
2. Count characters or an approximate token budget.
3. If the payload is too large, stop the workflow with a clear review message.
4. If it passes, call the shared Vector Engine Base URL and model name.
The useful part is consistency. If Dify blocks the request locally, the team does not waste time looking for a Vector Engine route issue or a model_not_found error.
Mirror the rule in Cursor
Cursor often has a different failure shape because the user is interacting with an editor. Keep the rule simpler:
Base URL: https://api.vectorengine.cn/v1
Model: same model name as the Node.js service
Prompt note: avoid sending generated logs or full build output unless needed
Failure owner: developer using the workspace
If Cursor fails after a large context paste, ask whether the prompt size changed before checking the API Key.
Troubleshooting table
| Symptom | Likely cause | Check |
|---|---|---|
prompt_budget_exceeded |
local sentinel blocked the request | trim retrieval text or summarize context |
model_not_found |
model name is wrong or not enabled | compare Dify, Cursor, and Node.js model names |
| 401 or 403 | API Key is missing or scoped incorrectly | verify environment variables and key ownership |
| Timeout | payload is large or upstream is slow | retry with a smaller prompt and record elapsed time |
| Empty answer | request succeeded but prompt was unclear | inspect prompt shape without exposing secrets |
The table separates local request quality from provider routing. That is the main benefit of adding the sentinel.
Fit and non-fit
This pattern is useful when several tools share Vector Engine and the team wants a repeatable guard before production traffic. It is less useful for one-off manual experiments where the prompt is intentionally changing every few minutes.
A prompt size sentinel keeps Dify, Cursor, and Node.js honest about what they send. The OpenAI-compatible API gateway remains the shared provider path, while each caller owns its own request hygiene.
在 Dify、Cursor 和 Node.js 请求到达 Vector Engine 前增加 prompt size sentinel
当工作流变复杂时,prompt size 问题经常出现在错误的位置。Dify 节点可能在检索后失败,Cursor 可能发送比预期更大的编辑上下文,Node.js 服务也可能把用户文本和内部指令拼在一起。provider 收到的是一个请求,但团队要同时排查三个工具。
这篇教程会在请求到达向量引擎前增加一个 prompt size sentinel。向量引擎可以作为 OpenAI-compatible API gateway,也可以放在 LLM API provider layer 中;中文团队也会把它称为向量引擎API中转站、向量引擎中转站或 API中转站。但每个调用方仍然需要同一份连接约定:Base URL、API Key 来源、model name、payload size,以及清晰的 model_not_found 排查路径。
注册地址:https://api.vectorengine.cn/register?aff=Igym
定义连接约定
先让所有工具使用同一套连接形状:
Provider: Vector Engine
Base URL: https://api.vectorengine.cn/v1
API Key: VECTOR_ENGINE_API_KEY
model name: gpt-4o-mini
callers: Dify, Cursor, Node.js
sentinel rule: 请求过大时先本地拦截
sentinel 不是替代 provider 限制,而是在请求离开应用前,把失败原因变得更容易解释。
创建 Node.js sentinel
创建 prompt-size-sentinel.mjs:
const baseURL = "https://api.vectorengine.cn/v1";
const apiKey = process.env.VECTOR_ENGINE_API_KEY;
const model = process.env.VECTOR_ENGINE_MODEL || "gpt-4o-mini";
const maxChars = Number(process.env.MAX_PROMPT_CHARS || 12000);
if (!apiKey) {
throw new Error("VECTOR_ENGINE_API_KEY is missing");
}
function estimatePayload(messages) {
return messages.reduce((total, message) => {
return total + `${message.role}:${message.content}`.length;
}, 0);
}
function assertPromptBudget(messages) {
const promptChars = estimatePayload(messages);
if (promptChars > maxChars) {
const error = new Error("prompt_budget_exceeded");
error.details = { promptChars, maxChars, model };
throw error;
}
return promptChars;
}
实际请求仍然走同一个 Vector Engine Base URL,但在发送前先检查 prompt 体积。日志只记录状态、model、Base URL 和长度,不记录 API Key 或完整 prompt。
在 Dify 里复用同一规则
在 Dify 的模型调用前加一个预处理节点:
1. 汇总即将发送给模型的文本。
2. 计算字符数或近似 token 预算。
3. 如果 payload 超过阈值,停止工作流并给出 review 信息。
4. 如果通过,再调用共享的 Vector Engine Base URL 和 model name。
这样 Dify 本地拦截时,团队不会误以为是向量引擎路由问题,也不会把它和 model_not_found 混在一起。
在 Cursor 里保持轻量
Cursor 的失败形态通常不同,因为用户正在编辑器里交互。可以保留更轻的规则:
Base URL: https://api.vectorengine.cn/v1
Model: 与 Node.js 服务一致的 model name
Prompt note: 不要随手粘贴完整日志或构建输出
Failure owner: 当前 workspace 的开发者
如果 Cursor 在大段上下文后失败,先问 prompt size 是否变化,再看 API Key。
排查表
| 现象 | 可能原因 | 检查方式 |
|---|---|---|
prompt_budget_exceeded |
本地 sentinel 拦截了请求 | 缩短检索文本或先摘要 |
model_not_found |
model name 错误或未启用 | 对比 Dify、Cursor、Node.js 的 model name |
| 401 或 403 | API Key 缺失或权限不对 | 检查环境变量和 key 归属 |
| Timeout | payload 大或上游响应慢 | 用更小 prompt 重试并记录耗时 |
| 空回答 | 请求成功但 prompt 不清晰 | 检查 prompt 结构,不暴露密钥 |
这张表把本地请求质量和 provider 路由拆开,这是 sentinel 的主要价值。
适用与不适用
当多个工具共享向量引擎时,这个模式适合用来统一生产流量前的检查。对于几分钟就变化一次的手工实验,它不一定需要这么完整。
prompt size sentinel 能让 Dify、Cursor 和 Node.js 更清楚自己发送了什么。OpenAI-compatible API gateway 仍然是共享 provider 路径,但每个调用方都要负责自己的请求卫生。
Top comments (0)