在调用路径中实施美元预算硬停的本地护栏

floe-guard 是一个在进程内运行的本地估算型预算护栏,它把价格映射与调用路径结合,在调用发生前按美元额度拦截,而不是事后统计花费。它还提供调用前估算、语音会话按轮预留以及上下文感知的预算建议。

# floe-guard [![PyPI 版本](https://img.shields.io/pypi/v/floe-guard.svg)](https://pypi.org/project/floe-guard/) [![npm 版本( Npm 版本)](https://img.shields.io/npm/v/floe-guard.svg)](https://www.npmjs.com/package/floe-guard) [![下载](https://static.pepy.tech/badge/floe-guard/month)](https://pepy.tech/project/floe-guard) [![Python 版本](https://img.shields.io/pypi/pyversions/floe-guard.svg)](https://pypi.org/project/floe-guard/) [![CI](https://github.com/Floe-Labs/floe-guard/actions/workflows/ci.yml/badge.svg)](https://github.com/Floe-Labs/floe-guard/actions/workflows/ci.yml) [![许可证: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE) **要知道每个AI 所谓的真正成本。** 你的经纪人花在十几家小贩身上 每个呼叫——载体,speech-to-text,型号,语音,工具. floe-guard成本每个呼叫 当它结束,并保持一个 **活分类账** 正在处理中,没有 账户,没有注册, **默认无遥测**. 为您连接免费( 一个密钥) **覆盖范围得分** 财务报告和财务报告 **7 - 每日历史** ——审计级别. 图片显示每美元流向哪里,直到$2K/月跟踪,没有卡。 *hard-stops* a 横跨天花板前的运行环:$0.10而不是$4,000. **Python 键** (`pip install floe-guard`: 浅色 `check()` / `record()` 或适配器用于 [OpenAI](#openai) · [人类](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#anthropic>) · [双子座](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#google-gemini>) · [CrewAI](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#crewai>) · [LiteLLM](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#litellm>) · [LangChain](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langchain>) · [LangGraph](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langgraph>); 语音适配器用于 [管道猫](#pipecat-voice) · [LiveKit](#livekit-voice) · [瓦皮语Name](#vapi-voice) · [调试](#retell-voice); [预征入境大门](#voice-admission-gates-pre-call). **TypeScript** (`npm i floe-guard`): [Vercel AI SDK (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#vercel-ai-sdk>) 中件, 本地软件 [LiveKit 瓦皮 —— Retell](#voice-adapters) 语音适配器. 见 [适配器矩阵](#adapter-matrix) 在Python vs TypeScript中, > 在PyPI上读到这个? `docs/…` 财务报告和财务报告 `examples/…` 链接解析 > [GitHub README](https://github.com/Floe-Labs/floe-guard),不在PyPI页面上. 硬站采用合同制:适配器门 LLM 自动呼叫;付费 工具, [`reserve_tool()` / `settle_tool()`](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#tool-spend-under-the-same-ceiling>) 块 *在此之前* 调用运行( N)`record_tool()` 事后只有几米长的距离, 无法阻止已经打的电话。 ## 免费连接您的覆盖分数 + 7 - 每日历史 `floe-guard` 将活分类账保存在本地 —— 每一次通话的实际成本, 在您的过程中, 没有账户 。 连接到主机 Floe( 免费, 一个键) , 而该分类账成为您的 。 **覆盖范围得分** 财务报告和财务报告 **7 - 每日历史**: 每美元到$2K/月追踪到多少地方的审计级别图片,没有卡。主机是最高记录系统——它花费每个销售商在单一分类账上结束的每一次通话,与客户的关联和竞选,显示每个客户的利润率,并让您从实际中开出发票给自己的客户,同时强制使用开支控制。添加服务器侧面。 **浮动代理技能** 教授 Claude 代码/ cursor 相同的 govern-your-spend 工作流程: ```bash npx skills add floe-labs/agent-skills ``` [浮油代理技术 –](https://github.com/Floe-Labs/agent-skills) · [文件 _________________](https://floe-labs.gitbook.io/docs/getting-started/claude-code-skill) > **AI探员:** 这回转自上飞机... [`AGENTS.md`](<https://github.com/Floe-Labs/floe-guard/blob/main/AGENTS.md>) (安装) 电线 —— 核查), > [`SKILL.md`](<https://github.com/Floe-Labs/floe-guard/blob/main/SKILL.md>),以及 [`llms.txt`](<https://github.com/Floe-Labs/floe-guard/blob/main/llms.txt>) 让一个编码代理添加和电线 > 守卫无人看守。 ```bash pip install floe-guard # Python npm i floe-guard # TypeScript (Vercel AI SDK) — see js/ ``` ```python from floe_guard import BudgetGuard guard = BudgetGuard(limit_usd=5.00) # your ceiling guard.check() # before each LLM call — raises if it'd cross response = call_your_llm(...) # your existing call guard.record("gpt-4o", response.usage.prompt_tokens, response.usage.completion_tokens) ``` 准备降落在你们身上 **覆盖范围得分**保存分类账 `guard.export_log()` 到 JSONL 文件,然后运行 `floe-guard push <file>` - 选入, 仅标价支出事件,没有提示或内容离开您的进程。 当下一个呼叫会越过天花板, 警卫升起 `BudgetExceeded` 财务报告和财务报告 指纹 : ``` BUDGET EXCEEDED — call blocked spent so far: $5.001250 | ceiling: $5.000000 The next call would cross your budget; floe-guard stopped your agent before it ran. ``` ![在穿越$0.10天花板之前, 软脚卫硬拦住一个运行环路](<https://raw.githubusercontent.com/Floe-Labs/floe-guard/main/docs/stop-the-loop.gif>) 直接从安装处运行(_R): `pip install floe-guard && floe-guard demo` ——无API密钥,无账号,无网号._ ## 查看它停止循环( 不需要 API 密钥) 直接从安装开始——不需要仓库检查 : ```bash pip install floe-guard floe-guard demo ``` 这把钻井圈对着一个 **Stub LLM (英语).** ——没有真实的API密钥,没有账号,没有网络. 每一个假货都要卖 `gpt-4o` 呼叫下线 守卫在数分钟后停止循环 迭代,在它能跨越$0.10天花板之前。这是可复制的“停止” 循环演示 克隆了回波? [`examples/runaway_loop.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/runaway_loop.py>) (一个薄薄的包裹围绕) `floe_guard.demo.run_demo`). ## 在你运行前要付出一个工作负担 直接从安装开始——没有寄存器检出,没有API密钥,没有网络: ```bash floe-guard estimate gpt-4o --calls 1000 --tokens-in 800 --tokens-out 300 ``` ```text Estimating 1,000 call(s) to gpt-4o (800 in / 300 out per call) per call: $0.005000 total: $5.000000 price source: cost_map (snapshot YYYY-MM-DD) — offline, no network Guard this exact workload with a ceiling at (or above) the run total: from floe_guard import BudgetGuard guard = BudgetGuard(limit_usd=5.000000) # covers 1,000 call(s) ``` 由同一捆绑的成本图定价 警卫执行反对,所以 您设定的天花板就是您刚刚看到的成本。 无价模型以一个 干净错误, 永远不要猜测 $0.00 。 ## 那算什么费用? 另外一个演示——一个语音呼叫,每个从捆绑的地图中计价的腿,不 手动费率, 没有 API 键, 没有网络 : ```bash pip install "floe-guard[livekit]" # the demo imports livekit-agents python examples/voice_call_cost_livekit.py # needs a repo checkout — examples/ aren't in the wheel ``` ```text Per-leg call cost (all priced from the bundled cost map, no manual rates): livekit-stt $0.001027 # 8s × ($0.0077/min ÷ 60) Deepgram Nova-3 gpt-4o $0.003700 # 600 in / 220 out tokens LLM livekit-tts $0.009000 # 180 chars / 1k × $0.05 ElevenLabs Flash livekit-telephony $0.012750 # 1.5 min × $0.0085/min Twilio US inbound TOTAL $0.026477 ``` 这就是一个符号级工具不能给出的答案: 它的高度是LLM腿和 错过了其余的帐单。 收费率是公共US列表价格和 漂移——细节、警告和Pipecat版本 [语音适配器](#voice-adapters-stt--llm--tts). ## 你的价钱,不是单价 捆绑的地图是每个卖家的 **公开清单价格**几乎没人付钱 清单:量级、谈判合同、承付折扣、承运人 通行费和最低计划都会移动真实数字。所以,捆绑的费率 解析为 *未经证实* ——你确认或替换的起点,从不 声称你的帐单。 A **收费卡** 是你自己的价码,它比捆绑的地图还贵,而且可以定价 地图上从未听说过的销售商: ```bash export FLOE_RATE_CARD=~/floe-rates.json # a path (Python) or inline JSON (JS) ``` ```json { "gcp-vision-document-text-detection": { "mode": "ocr", "unit": "usd_per_page", "rate": 0.0004 }, "mistral-ocr": { "mode": "ocr", "unit": "usd_per_page", "rate": 0.001 } } ``` ```python from floe_guard import set_rate_card, resolve_voice_rate set_rate_card("~/floe-rates.json") # or a dict, or inline JSON rate = resolve_voice_rate("mistral-ocr", "ocr") rate.rate # 0.001 rate.source # "rate_card" — yours, not a list price rate.confirmed # True ``` 解析顺序,最具体的第一: ```text per-call override → your rate card → bundled list price → UnpriceableLegError ``` 每一个解决的费率都带有其来源—— `provider`, `source_url`, `retrieved_at`, `confirmed` - 所以,一个列表价格可以是 *显示* 以一键并排队 而不是悄悄地报告为成本。 而不是被跳过:如果你的宣布的费率不能读取,则回落到 列表价格会产生自信的,错误的数字。 一些供应商轮船 **无** 故意捆绑费率——Runpod(各等级每小时) 地图无法在其中选择, Mistral OCR 和 Azure 文档智能( 否) 已公布数字,HeyGenSimli和Beyond Students. (只有功劳),一个猜想 比率比无差:一条没有价格的腿闭塞,可见,错 悄悄地弄错了单子,加上一张收费卡. ## 为什么是护身符? 已经可以了 *看见了* 你的经纪人花的钱 问题在于看到太迟了 护花板是部分 **停止通话**而不是报告损失的部分。 - **`max_tokens` / `max_rpm`** 上限大小和比率,不 **(单位:美元)** - 一个便宜的模型 被困在循环 仍然耗尽预算。 - **使用日志和供应商仪表板** 告诉你你花了多少钱 *之后* 它不见了。 Floe -guard拒绝电话 *在此之前* 它穿过你的天花板。 - **一个成本回调,只是日志** 事后通知 无法阻止 运行——执行必须站在下一个呼叫前,这就是它住的地方. - **一个手卷 `spent += cost` 并行代理下的比赛** (CrewAI 粉丝 - out, `asyncio`, `Promise.all`: N 呼叫读数相同,但总数和次数相同。 从解剖学上看 浮层护卫储备`reserve()`/`settle()`),所以天花板(天花板) 以货币持有。 整个工作:艰难的一站 **在此之前** 下一个电话,那个 **压在粉丝的脚下** — 没有账户,没有网络,没有密码。 ## 如何运作 警卫坐着 **在调用路径中**,而不是在活动总线上。被动的听众是 说到花销 *事实之后* 并且无法阻止任何东西——所以执法部门必须 成为站在下一个呼叫前的东西: - **`check()`** 运行在每次 LLM 呼叫之前。它预测下一个呼叫的费用来自 最后一个,然后升起 `BudgetExceeded` 如果那会越过你的天花板... 调用从不运行 。 (如果有估计数, 运行中的总检查也会捕获过量射击) 低调下来了。 - **`record(model, prompt_tokens, completion_tokens)`** 每次回复后运行。 价钱很贵的 **离线** 从捆绑的 [LiteLLM成本图](<https://github.com/Floe-Labs/floe-guard/blob/main/src/floe_guard/cost_map.json>) 在运行中添加 USD 总计。 ### 坚持统一协调世界时一天跨进程预算(Python) 当每个进程打开时, Cron 和无服务器的工作才能共享一个上限 相同的数据库文件, 并使用可靠的 SQLite 文件锁定 。 单独本地文件的无服务器实例不协调; 使用主机 当无法共享文件时执行 : ```python from floe_guard import BudgetGuard, SqliteStore guard = BudgetGuard( limit_usd=5.00, window="utc-day", store=SqliteStore("agent-budget.sqlite3"), ) reservation = guard.reserve_tool(0.02) # atomic across sharing processes guard.settle_tool("search", 0.02, reserved=reservation) ``` 一个数据库文件代表一个逻辑预算。 保留持续到一个新的 UTC 日期选择一个新鲜窗口; 每一个调用日志, 工具属性和下调估计仍然是程序本地化的。 以保留方式死亡,留下必须收回的失效保留 手动。 此特性是 Python 唯一的, 并支持 `window="utc-day"` 仅限; 任意滚动期限尚未得到支持。 以估计数为基础,因此尽可能对实际请求作出大小保留。 `StreamGuard` 财务报告和财务报告 `guard_stream()` 需要一个内幕警卫。 店里有他们养的 `ValueError` 在消耗块和释放任何供应之前 保留:活动流累积不在各进程之间共享。 流线也不能重叠 `guard.step()` 范围: 在 a 内创建流 步骤提升 `ValueError` 并释放其保留; 开启一步,同时 溪流也是活动升起。在进入一步前完成或关闭溪流。 对于定价的溪流, `check()` 财务报告和财务报告 `reserve()` 计数活动即时/ 完成 指针对 `token_limit`,以扣除它们现有的印记。 中断本身仍以美元为基础;新入院者受象征性限制。 ### 无价模型已关闭 如果一个模型不在成本地图上 而你没有提供价格,警卫 **警告( E) 大声地拒绝** (`UnpriceableModelError`而不是默默地视之为 免费 - *你不能花太多钱去测量。* 给它一个价钱来强制执行它: ```python from floe_guard import BudgetGuard, ManualPrice guard = BudgetGuard( limit_usd=5.00, price_overrides={"my-self-hosted-model": ManualPrice(1e-6, 2e-6)}, # USD/token ) # or, set fail_closed=False to warn-and-skip for models you accept un-metered. ``` ### {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}什么地图价格 捆绑的价格是 **日期快照** 公开名单比率——供应商变动 将新鲜度当作信号,而不是保证。 已曝光 : `floe_guard.cost_map_generated_at()` (毕节) /. `costMapGeneratedAt()` (TypeScript) 返回 `YYYY-MM-DD` 它最后的生成/验证,所以你可以 显示它或门上。 卖家的地图是故意的 **OpenAI,Anthropic,谷歌双子座(AI) 和一套格鲁克模型** (规则在 [`scripts/update-cost-map.mjs`](<https://github.com/Floe-Labs/floe-guard/blob/main/scripts/update-cost-map.mjs>)()——并非全部 LiteLLM的上游列表. 通用开放量级名称( NAME OF) :`qwen3-32b`, `gpt-oss-120b`许多供应商以非常不同的价格提供服务,因此, 按一个卖家的费率解决它们,就等于少了一个开支守卫的米数。 不可贵贱,除非你窥视它们。`groq/…`)或通过人工价格。 **双子座以谷歌AI Studio(Gemini开发者API)的定价.** Vertex AI 维基百科中的相关条目: 维基语录链接:名人名言 - 文学作品 - 谚语 - 谚语 以自己——有时更贴切——的费率提供同样的代号,以及一个代号。 所以Vertex探员应该通过 `price_overrides` Google的双子座实验级 在 $0 的列表中, 故意保持不贵: 以 0 计价的聊天模式会测量 每一次免费的呼叫, 失败的关闭定价无法赶上。 模型 ids 灵活解决: 提供者- 前置格式工作 (`openai/gpt-4o`, `groq/qwen/qwen3-32b` 和 ChatGroq 数据 `qwen/qwen3-32b` 两者 击中同一条目; `gemini/gemini-2.5-flash` 赤裸裸的, `gemini-2.5-flash` 地图上还没有列出 (`claude-opus-4-8-<date>`以别名输入而不是关闭的价格。 其他一切——米斯特拉尔,科赫尔,奥拉马,贝德洛克,实时/音频模型, 自我托管——需要 `price_overrides` (或 减) `fail_closed=False` 来接受它 (未计). ## 保护你的第一个真正的工作流程 你看着它停下来了 *树枝* 循环——真正的回报是保护a *真实* 一个, 地方天花板靠它积蓄。你们挑堆积物,每个堆积物都是一个适配器。 几条线,不要重排: - **OpenAI / Anthropic /双子座** — [`guarded_completion`](#openai) 结束客户端的呼叫 。 - **LangChain / LangGraph** — a [调用处理器](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langchain>) / [`guarded_node`](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langgraph>) 每个扇形树枝。 - **CrewAI / LiteLLM** — [`budget_guarded_llm`](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#crewai>) / [`guarded_completion`](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#litellm>). - **语音( Pipecat ) .LiveKit瓦皮·雷特尔** — [每回合语音适配器](#voice-adapters-stt--llm--tts). 见 [适配器矩阵](#adapter-matrix) 在Python vs TypeScript中, ## 框架适配器( 可选的额外) ### OpenAI ```bash pip install floe-guard[openai] ``` ```python from openai import OpenAI from floe_guard import BudgetGuard from floe_guard.integrations.openai import guarded_completion guard = BudgetGuard(limit_usd=1.00) client = OpenAI() response = guarded_completion(guard, client, model="gpt-4o", messages=[...]) ``` `guarded_completion` 在呼吁之前保留预算(提高) `BudgetExceeded` 因此一个被封杀的电话从没有到达OpenAI),并且记录在之后花费. 使用( U) `guarded_acompletion` 带有 `AsyncOpenAI` 客户端为 async 。 见 [`examples/openai_adapter.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/openai_adapter.py>) 用于可运行的 硬站演示(不需要API密钥). 所有其他适配器在合同签订前/记录后都遵循同样的预留——全程行驶。 **[docs/adapters.md](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md>)**: [CrewAI](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#crewai>) · [LiteLLM](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#litellm>) · [LangChain](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langchain>) · [LangGraph](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#langgraph>) · [人类](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#anthropic>) · [双子座](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#google-gemini>) · [Vercel AI SDK (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#vercel-ai-sdk>). ## 语音适配器(STT → LLM → TTS ). 语音管道没有单一的呼叫站点可以包装: LLM坐落在运行中 并且为召唤而不断地燃起火焰。 函数包装器, 这些适配器执行 **每转弯** - LLM号机前的储备 调用( 所以转弯 ) *在此之前* 它的TTS/Audio 花堆在呼叫之上 )),解决管道的实际使用问题。 报告,并放出一个结束的转弯,而不报告使用量(a) ),所以保留不会在天花板上漏出。 A. 报告 **Python 键** 管道、 LiveKit、 Vapi 和 Retell 适配器——见 编号 [语音适配器](#voice-adapters) 用于TypeScript双胞胎的部分。 **整个呼叫从捆绑的地图中定价——没有手型速率.** 每个名称 运输车辆销售商(`stt_model`, `tts_model`, `telephony`和浮控价格 全程呼叫——STT(每秒)+LLM(每令牌)+TTS(每个1k字符)+电话 (每分钟)——从卖家的语音成本地图,所以答案是: *"这是什么 电话费"* 在框外的 $0 级。 (`stt_usd_per_second` / `tts_usd_per_1k_chars` / `telephony_usd_per_minute`仍然( ) 工作并赢得地图;一条没有卖家的腿或一个覆盖器的腿 未计数(只标价合同),而且地图无法定价的供应商 **失败 关闭** (`UnpriceableVoiceError`而不是在默默无声的$0进行测量. > **电话在 v1 中只使用美国**,每个语音速率是a **drift-prone > 抓图** ——供应商更经常地改变这些价格 > 而不是重新刷新地图,所以把它们当作一个估计,并对照 > 的实时定价页,然后才能信任一个数字。 > `"__voice__"` 键 [`cost_map.json`](<https://github.com/Floe-Labs/floe-guard/blob/main/src/floe_guard/cost_map.json>); 刷新 > 他们与 [`scripts/update-cost-map.mjs`](<https://github.com/Floe-Labs/floe-guard/blob/main/scripts/update-cost-map.mjs>)种子 > 销售商:Deepgram,AssemblyAI(STT);ElevenLabs,Cartesia Sonic,Rime(TTS); > Twilio, Cartesia Line (电话). Telnyx 被推迟等待核实名单 > 执行率。 > **交接前录取** (reserve-before-turn)——电话是 **per-minute > 应计**而不是现场切线。 > > 一些卖家不向地图上的罐头单位开帐单: TTS在本地定价 > 每个音频分钟(Cartesia Sonic, Rime)在假定~1000时转换. > chars/min,和每会次的间接费用(如AssemblyAI)没有建模——所以是 > 磁带腿是一种 **估计数**,而不是准确的发票,并且可以低于或 > 说得太过了 `floe-guard` 是一个 **本地间距上限**; 权威 > 盖是服务器侧 。 单次通话中按腿细分(无人工价格——`python') 例/ voice_call_cost_livekit .py`, 无 API 键, 无 网络 : ```text Per-leg call cost (all priced from the bundled cost map, no manual rates): livekit-stt $0.001027 # 8s × ($0.0077/min ÷ 60) Deepgram Nova-3 gpt-4o $0.003700 # 600 in / 220 out tokens LLM livekit-tts $0.009000 # 180 chars / 1k × $0.05 ElevenLabs Flash livekit-telephony $0.012750 # 1.5 min × $0.0085/min Twilio US inbound TOTAL $0.026477 ``` ### 管道( 语音) ```bash pip install floe-guard[pipecat] ``` 丢弃一个 `FloeBudgetGuardProcessor` 直接在LLM之后进入输油管 服务。它保留在每个转弯 `LLMFullResponseStartFrame` 和定居从 编号 `LLMUsageMetricsData` 管道发射——所以管道 `PipelineTask` 必须 将创建为 `enable_metrics=True, enable_usage_metrics=True`. ```python from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.task import PipelineTask, PipelineParams from floe_guard import BudgetGuard from floe_guard.integrations.pipecat import FloeBudgetGuardProcessor guard = BudgetGuard(limit_usd=1.00) pipeline = Pipeline([ transport.input(), stt, context_aggregator.user(), llm, FloeBudgetGuardProcessor(guard, model="gpt-4o"), # meters AND hard-stops tts, transport.output(), context_aggregator.assistant(), ]) task = PipelineTask( pipeline, params=PipelineParams(enable_metrics=True, enable_usage_metrics=True), ) ``` > **断裂** — `transport`, `stt`, `llm`, `tts`,以及 `context_aggregator` 是您已有的 Pipecat 对象; 这只显示守护者坐在您已有的管道中的位置 。 对于一个完整可运行的演示( 没有 API 键, 没有网络 —— 需要) `pip install floe-guard[pipecat]`),参见 [`examples/voice_turn_budget.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_turn_budget.py>). 默认情况下,一个被封锁的转弯会推向致命的转折 `ErrorFrame` 终止 管道 – 每个适配器都给你一个难阻塞的管道。 `on_budget_exceeded` async召回,先说优雅的"包装"行 而不是砍掉电话的死。 给卖家点名 从地图上调出其余的电话: `tts_model="elevenlabs-flash-v2.5"` 自动计 `TTSUsageMetricsData` 管道猫 发射; `stt_model` / `telephony` 已明确计数(Pipecat发射无 STT / 电话使用框架) `processor.meter_stt(seconds)` / `processor.meter_telephony(minutes)`。见 [`examples/voice_call_cost_pipecat.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_pipecat.py>) (单位:千美元) 每条腿全部崩溃(无 API 键,无网络——需要) `pip install floe-guard[pipecat]`). ### LiveKit(声音) ```bash pip install floe-guard[livekit] ``` `LiveKitBudgetGuard.attach(session, agent)` 在 / / 之前电线连接保留地 / LiveKit 结算后合同 `AgentSession`: 它保留在经纪人的 `llm_node` 并按会话决定 `metrics_collected` `LLMMetrics`. LiveKit's `LLMMetrics` 不报告服务型号,所以成本与 编号 `model` 你通过这里。 ```python from livekit.agents import AgentSession from floe_guard import BudgetGuard, ManualPrice from floe_guard.integrations.livekit import LiveKitBudgetGuard guard = BudgetGuard( limit_usd=1.00, price_overrides={"gemini-2.0-flash": ManualPrice(0.30e-6, 2.50e-6)}, ) budget = LiveKitBudgetGuard(guard, model="gemini-2.0-flash") session = AgentSession(...) budget.attach(session, agent) # wire reserve / settle / release await session.start(agent=agent, room=ctx.room) ``` > **断裂** — `session`, `agent`,以及 `ctx.room` 来自于您的 LiveKit 代理进入点(`JobContext`这只显示卫兵的附身之处。 说明供应商名称 -- -- `stt_model="deepgram-nova-3"`, `tts_model="elevenlabs-flash-v2.5"`, `telephony="twilio-us-inbound-local"` - 测量器 STT / TTS / 调音器(通常是一个声音) (从地图上通过) `record_tool`,没有手型速率. STT / TTS 自动解析 LiveKit 的度量事件; 驱动电话每分钟一次 `budget.meter_telephony(minutes)`. 单位覆盖(`stt_usd_per_second` / `tts_usd_per_1k_chars` / `telephony_usd_per_minute`仍然工作并赢得 地图,和一个 `on_budget_exceeded` 音响回调在转弯前会讲总结线 结束。见 [`examples/voice_call_cost_livekit.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_livekit.py>) 每条腿的完整细分(无API键)和 [`examples/voice_turn_budget.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_turn_budget.py>) 为难阻拦者. ### 瓦皮( 语音) ```bash pip install floe-guard # the Vapi adapter is framework-free — no extra ``` 自定义 - LLM 代理只看到 **样板腿**,这样 `VapiBudgetGuard` 守卫 `/chat/completions` 转过身来通过 `assistant-request` (原始内容存档于2017-10-09). webhook. `guard_completion` (JSON)和 (中文(简体) ). `guard_stream` (SSE) 预留估计费用 **在此之前** 上游呼叫,解决瓦皮真正的OpenAI `usage` 之后, 并解除对错误/破坏的搁置——因此,超预算转弯得到一个402代替 到达您的 LLM 。 设置 `stream_options={"include_usage": True}` 编辑 上游流流请求或 `guard_stream` 大声失败( SSE 省略 `usage` 没有它). ```python from floe_guard import BudgetGuard from floe_guard.errors import BudgetExceeded from floe_guard.integrations.vapi import VapiBudgetGuard guard = BudgetGuard(limit_usd=1.00) budget = VapiBudgetGuard( guard, stt_model="deepgram-nova-3", # $/sec from the voice map tts_model="elevenlabs-flash-v2.5", # $/1k-chars from the voice map telephony="twilio-us-inbound-local", # $/min from the voice map ) # POST /chat/completions — the custom-LLM endpoint Vapi calls each turn. completion = await budget.guard_completion( lambda: openai_client.chat.completions.create(model=model, messages=messages), model=model, ) # budget exhausted → BudgetExceeded (return a 402), never proxied upstream ``` `budget.meter_stt(seconds)` / `budget.meter_tts(chars)` / `budget.meter_telephony(minutes)` 累积代理机从未看见的腿( 驱动) 他们从瓦皮的 `end-of-call-report` Webhook 。 指定销售商为其定价 从地图上,或通过每个单元的覆盖(`stt_usd_per_second` / `tts_usd_per_1k_chars` / `telephony_usd_per_minute`胜过它。 [`examples/voice_call_cost_vapi.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_vapi.py>) 联 合 国 每条腿断裂(无API键,无网络). ### 雷特尔(语音) ```bash pip install floe-guard # the Retell adapter is framework-free — no extra ``` Retell的定制LLM运行于一个WebSocket上. `RetellBudgetGuard` a 时的储备金 `response_required` 互动到达(在LLM呼叫之前,所以转弯是 它的TTS / 电话费花在堆上之前就被屏蔽了 ) 符号使用量 : `content_complete`,并当更新时释放存储器 `response_id` 中断。 请选中 `response` 您已经流出的事件... Retell 发送平庸事件结束 `ws`: ```python from floe_guard import BudgetGuard from floe_guard.integrations.retell import RetellBudgetGuard guard = BudgetGuard(limit_usd=1.00) budget = RetellBudgetGuard(guard, model="gpt-4o-mini") # in your custom-LLM WS message handler: turn = budget.begin_turn(event) # reserve before the LLM call if not turn.admitted: # budget exhausted → wrap up the call await ws.send(json.dumps(budget.response(event["response_id"], "I'm out of budget — wrapping up.", complete=True, end_call=True))) return await ws.send(json.dumps(budget.response(event["response_id"], text, complete=True))) budget.settle_turn(event["response_id"], {"promptTokens": n, "completionTokens": m}) ``` `budget.meter_stt` / `meter_tts` / `meter_telephony` 测量腿部的插座 从不曾看见(从已知的期间),从语音地图中计价 当您指定供应商或每个单位的覆盖。 `budget.close()` 释放任何 仍然开放 暂停呼叫 撕毁。 [`examples/voice_call_cost_retell.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_retell.py>) 联 合 国 每条腿断裂(无API键,无网络). ## 语音接收门(呼叫前) 适配器在仪表上方 已经运行的电话。 `floe_guard.gates` 是那个 步骤 *在此之前* 在呼叫边界处, **承认或拒绝** 来电 它返回了每个管弦乐手进入的准确的 JSON 形状 Webhook 期望, 所以同样的拒绝合同 您在当地服务 是主机 Floe service. 付费升级是URL互换,而不是重写. ```python from floe_guard import BudgetGuard, gates guard = BudgetGuard.from_floe(api_key="floe_…") # or a local BudgetGuard(limit_usd=…) # Retell inbound webhook — only the boolean `true` rejects: gates.retell(guard) # budget left → {"call_inbound": {}} (admit; pass admit={"dynamic_variables": …}) # exhausted → {"call_inbound": {"reject": True}} # Vapi assistant-request webhook — respond within ~7.5s: gates.vapi(guard, assistant_id="asst_…") # budget left → {"assistantId": "asst_…"} (or assistant={…} for an inline assistant) # exhausted → {"error": "Sorry, this agent is out of budget right now."} # Pipecat / custom / Bland — the provider-agnostic decision: if not gates.pre_call(guard): ... # reject at your call boundary ``` 过 `estimated_call_usd=` 当剩余预算无法支付 下个调用( 例如 ) `$/min × expected minutes`),而不只是当它用完的时候. **非约束性飞行前。** 门 *读取* 其余预算;没有 保留它,所以在同时发出的电话中,它可以接受比预算更多的 严格地说,这是粗糙的入院控制—— 装订的,原子的金钱门 留守待命警卫`check` / `reserve` ),与主机相同 Floe只检查预拨闸门. **仅限预告录用.** 一个大门决定一个电话 *开始*;没有 接到通知后,一通电话就通到尾声——这里没有任何东西可以切断。 一个离路口。`guard_stream` 能够停止一个 LLM *生成*,则不是 ) 预算,而不是平衡。v1. > **核查说明。** Retell的入驻网火为入驻 **phone/SMS** > 电话(不是dial-to-sip);其网络通话行为没有记录。 *发送 > 调用* 元数据字段名称未经证实,所以没有 `gates.bland()` 然而——使用 > `gates.pre_call(guard)` 把拒绝电线接入布兰德的Webhook节点 ## 适应器矩阵 | 适配器 | Python 键 | TypeScript | |---|---|---| | OpenAI | ✅ | 通过 [Vercel AI SDK (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#vercel-ai-sdk>) | | 人类 | ✅ | 通过 [Vercel AI SDK (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#vercel-ai-sdk>) | | 谷歌双子座 | ✅ | 通过 [Vercel AI SDK (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/adapters.md#vercel-ai-sdk>) | | LangChain | ✅ | — | | LangGraph | ✅ | — | | CrewAI | ✅ | — | | LiteLLM | ✅ | — | | Vercel AI SDK (英语). | — | ✅ | | **LiveKit** (声音) | ✅ | ✅ | | **瓦皮语Name** (声音) | ✅ | ✅ | | **调试** (声音) | ✅ | ✅ | | **管道猫** (声音) | ✅ | — | 包裹船 **语音脚定价** (`price_voice_leg` / `priceVoiceLeg`), **预征入境大门** (`gates.retell` / `gates.vapi` / `gates.pre_call` / `gates.preCall`),以及 **本地语音适配器** LiveKit、瓦皮和雷特尔 (以下)——Python和TypeScript在锁步. ### 语音适配器 Python和TypeScript 船原型 STT → LLM → TTS 会话配音器 堆栈。在模型打开前,每个储量都固定在实际使用量上,释放在 和米的 STT / TTS / 电话腿 `__legs__` 成本图 (无法通过电子邮件关闭) `UnpriceableVoiceError`)——同一执行合同跨越. 两种语言。 **(a) 转帐前/通知前接纳加每转帐结算; 没有中途停赛。** ```ts // LiveKit Agents (Node) — @livekit/agents is an optional peer import { LiveKitBudgetGuard } from "floe-guard/adapters/livekit"; new LiveKitBudgetGuard(guard, { model, sttModel, ttsModel, telephony }).attach(session, agent); // Vapi custom-LLM proxy — wrap the /chat/completions turn import { VapiBudgetGuard } from "floe-guard/adapters/vapi"; const budget = new VapiBudgetGuard(guard, { sttModel, ttsModel, telephony }); const completion = await budget.guardCompletion(() => openai.chat.completions.create(req), { model }); // Retell custom-LLM WebSocket — reserve on response_required, settle on content_complete import { RetellBudgetGuard } from "floe-guard/adapters/retell"; const decision = budget.beginTurn(event); // { admitted } — reserves before the LLM call budget.settleTurn(event.response_id, usage); // settle real usage after content_complete ``` ```python # Vapi custom-LLM proxy — wrap the /chat/completions turn from floe_guard.integrations.vapi import VapiBudgetGuard budget = VapiBudgetGuard(guard, stt_model="deepgram-nova-3", tts_model="elevenlabs-flash-v2.5", telephony="twilio-us-inbound-local") completion = await budget.guard_completion( # reserve → run → settle on real usage lambda: openai_client.chat.completions.create(model=model, messages=body["messages"]), model=model, ) # Retell custom-LLM WebSocket — reserve on response_required, settle on content_complete from floe_guard.integrations.retell import RetellBudgetGuard budget = RetellBudgetGuard(guard, model="gpt-4o-mini") decision = budget.begin_turn(event) # { admitted } — reserves before the LLM call budget.settle_turn(event["response_id"], usage) # settle real usage after content_complete ``` 每艘船都有一个可运行的无键演示(`examples/voice_call_cost_livekit.py`, `examples/voice_call_cost_vapi.py`, `examples/voice_call_cost_retell.py`; 节点 `js/examples/*_voice_cost.mjs`印有预先通知的收监决定和 每段通话的费用明细;完整 API 请参阅模块文档字符串。Pipecat 的 服务器管道是 Python 唯一的,所以它的预算处理器生活在 Python 中 软件包( R)`FloeBudgetGuardProcessor`)——有节点管道服务器表面. 要适应。 关于连接到现有语音管道的浮标,见浮标文件: **[在您已有的管道中添加浮点数](https://floe-labs.gitbook.io/docs/getting-started/integrate-existing-pipeline)**. ## 了解背景的预算编制 坚不可摧是保证; `advisory()` 是那个 *颠倒*。读它之前的a 步骤让您的代理人 **适应** 当它接近盖子时, 拍手到一个更便宜的模型, 收缩任务,或者收尾——而不是被截断中场. ```python guard = BudgetGuard(limit_usd=0.10, near_limit_bps=7000) # flag at 70% used adv = guard.advisory() # BudgetAdvisory(near_limit=False, used_bps=125, remaining_usd=0.0987, ...) model = "gpt-4o-mini" if adv.near_limit else "gpt-4o" # downshift near the cap # On the very first call, check() has no historical usage to predict cost with. # To ensure the ceiling is enforced on the first run, pass an estimated cost: guard.check(estimated_cost=0.01) # still the hard line — taper or not, this holds response = call_your_llm(model) guard.record(model, response.usage.prompt_tokens, response.usage.completion_tokens) ``` `advisory()` 返回时 `near_limit`, `used_bps` (基点使用), `remaining_usd`,并报告总预算。 `expected_cost` (单位:千美元) 警卫自己的下调估计)和 `est_calls_remaining` (还有多少电话) 剩下的预算购买, `None` (直到第一次通话录音)——通话 头室,不光是美元 声音,它也报告 `burn_rate_usd_per_min` (自警卫成立以来的____分钟——美元/min语音组观察; 按每个呼叫/转弯的费率,设一名警卫, `None` 在任何时间之前 叶片。 **软** 信号——该模型可能 忽略它; `check()` 是天花板的顶端 [`examples/budget_aware.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/budget_aware.py>) 可运行的磁盘演示 (无API键). 模型选择只有一个轴, 相同的信号驱动器 **任何** 成本杠杆,和在 最大的杠杆在别处 —— 检索深度 ([`examples/retrieval_depth.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/retrieval_depth.py>): RAG `top_k` 坠落 20 → 12 → 5),上下文大小. ([`examples/context_size.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/context_size.py>): 停止重新发送整个 记录、封顶答复缩短)和计划复杂程度 ([`examples/plan_complexity.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/plan_complexity.py>)细细的推理, 然后放下可选的子任务以保护所需的子任务。 模型固定并缩小了一个非模型参数作为预算排水(没有API键). ### 预算意识重试 当代理商出现时 盲目复习可以再次花费同样的昂贵路径 跑出头室。 `with_budget_retry()` 以现有警卫为代表: 预算正常时正常重试,请询问您的代码, 以便宜的重试计划 何时 `advisory().near_limit` 是真的,打电话 `check(estimated_cost)` 在此之前 每一次重试都这样 超预算重试从不运行。 ```python from floe_guard import BudgetGuard, RetryPlan, with_budget_retry guard = BudgetGuard(limit_usd=1.00) def premium_model(): return call_model("gpt-4o") def mini_model(): return call_model("gpt-4o-mini") result = with_budget_retry( guard, premium_model, estimated_cost=0.20, max_attempts=2, on_degrade=lambda exc, adv: RetryPlan(call=mini_model, estimated_cost=0.01), ) ``` 帮助者不排序模型或知道提供者定价; 呼叫者定义 "贱货"是什么意思? `on_degrade`. TypeScript 曝光的图案与 `withBudgetRetry()`。见 [`examples/budget_retry.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/budget_retry.py>) 用于无网络演示。 你刚写的录音机逻辑 随身携带到主机上 —— 相同的近限 信号( E)`near_limit` + `used_bps`页:1 *每张* 供应商和上限; 主办单位 `X-Floe-Budget-Advisory` 信头将它嵌在下面 `tightest` 带原始整数 值,所以字段访问是一个轻度重映射。见 [要主机的一行线路](#one-line-to-hosted). TypeScript软件包曝光 `guard.advisory()`,绘制 Python 的字段 `snake_case` (e.g., `near_limit`, `used_bps`, `remaining_usd`(至TypeScript 's `camelCase` (`nearLimit`, `usedBps`, `remainingUsd`),其中较新的 TypeScript 字段标记为可选。 JSONL 计划 行为产生者 `exportLog()` 匹配 Python 的颜色 `export_log()` 确切的说 ## 花费日志、工具、符号和最后期限 除了美元坚韧不拔之外,浮动卫士还有几个预算方面的内容——充分参考 **[docs/advanced.md](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md>)**: - **[逐笔开支记录](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#per-call-spend-log>)** 所有定价物品的分类账; `export_log()` 释放稳定的JSONL. - **[在同一上限下使用工具](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#tool-spend-under-the-same-ceiling>)** — `reserve_tool()` / `settle_tool()` 将付费的API电话与符号置于相同的封顶下. - **[按步骤编列的最高限额( P)](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#token-ceilings-and-per-step-budgets>)** ——与USD天花板并列的标志性顶帽和每步顶帽. - **[LatencyBudget](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#latencybudget--deadlines-the-same-way>)** – 与SLA对阵的相同时间储备/咨询模式. - **[请求规模的估计数和中流执行](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#request-sized-estimates-and-mid-stream-enforcement>)** — `estimate_call()` 超大小的首次呼叫; `guard_stream()` 来切断中世纪的溪流 ## 要主机的一行线路 已经在主机Floe上? 保留您的代码的每行 —— 交换构造器 。 `from_floe` 读你的 **服务器侧预算头室** 并将其作为当地人 因此,免费的 / 托管的升级是一行: ```python from floe_guard import BudgetGuard guard = BudgetGuard.from_floe(api_key="floe_…") # ceiling = your hosted headroom guard.check() # everything else is unchanged response = call_your_llm(...) guard.record("gpt-4o", response.usage.prompt_tokens, response.usage.completion_tokens) ``` 预算,而不是平衡:改为: *会议室* 信号和强制停止 **当地** — [主机浮动](#when-you-outgrow-local-guardrails) 仍然是源头 对不可逾越的、交叉复仇的盖子的真理。没有密钥集 ~ 没有网络 。 [zero-telemetry](#no-telemetry) 变数控件); 失败的读取失败关闭 (通过) `fallback_limit_usd=` 改为降低到当地上限。 您的磁带逻辑也延续了: 本地 `advisory()` 和东道主的 `X-Floe-Budget-Advisory` 头部显示相同 **近限信号** (`near_limit` + `used_bps` 所以,"靠近盖子? 拍子现在" 决定您所执行的分支是相同的。 线条形状不同 — 主机头 窝在最紧的盖下 `tightest` 有原始整数,所以可以访问 是轻轻的复刻图——但它能解答横跨 *每张* 供应商和上限,不 就是你在当地装的 ## 老实说,这是什么 花卫是... **当地,按估计数计算** 护栏,它的价格是... 供应商成本图 *在您的进程内*: - 成本图可以随着供应商价格的改变而漂移——像任何快照一样刷新. - 它只看见你的卖家 - 确定代理人或错误可以绕行一个进程内检查. - 在重价或冷价基数下,它与稳态支出,而不是 第一平行波。 预留了最后一个调用的成本( 3) 。`0` 一直持续到 首个 `record()`)——按实际要求大小: `estimate_call()` ( LiteLLM 适配器为您做此操作), 或使用主机 Floe 为硬盖 以任意货币计算 - 中流执行`guard_stream`价格由 ~ 4 字符块/托肯 高压,除非你提供一种示意剂,所以断点是大概的; 与供应商报告的使用情况进行最后的应计调节。 它本身是真正有用的,而且对它的极限是诚实的。 度量衡,没有"零违约"索赔——这是免费的本地停靠站,不是金库. ## 无遥测 船队的护卫 **没有** 它不发送使用事件,不安装pings, 没有标识符 — 除了运行时, 任何进程都无法离开 。 **你两件事情 明确选择加入**: 主机预算为(套) `FLOE_API_KEY` / 使用情况 [`from_floe`](#one-line-to-hosted)和(或) [分类账同步](#sync-your-ledger-for-coverage-score-opt-in) (`enable_sync()` / `floe-guard push`永远不要,永远不要 背景情况。 这是一个选择,而不是疏忽。守护者的全部价值是信任:a 图书馆,静悄悄地从人们的代理人身上 筛选出用法与 一个你把预算交给的工具。 ## 同步您的分类账以获取覆盖分数( opt- in) Floe的网关看不到它从未经过过路由的花费——BYOK,自我托管,或非路径 LLM/工具调用. **编辑器同步** 即是选择进入,从而缩小了这一差距:它推动您 本地开支分类账输入 Floe 的调节模式, 所以您 **覆盖范围得分** (单位:千美元) 分得的你经纪人的花费 弗洛实际上可以执行)成为计算 预算,不是平衡,而是报告你的情况 *已经花费* (单位:千美元) 覆盖和归属; 它不移动钱,也不改变钱包余额 。 **默认关闭,永远。** 两个明确的步骤——选择加入,然后发送——和否 背景发送 : ```python guard.enable_sync(api_key="floe_…") # opt in (read_write agent key). Sends nothing yet. ... # run your agent; spend accrues on the guard n = guard.sync() # THE send — POSTs export_log() now; returns events accepted guard.disable_sync() # revoke; the guard sends nothing after this ``` 或从保存的分类账中一枪: ```bash floe-guard push ledger.jsonl --key floe_… # or: your_export_log_producer | floe-guard push ``` **请求体正是 [`export_log()`](<https://github.com/Floe-Labs/floe-guard/blob/main/docs/advanced.md#per-call-spend-log>) JSONL** — 每个定价支出事件一行: `timestamp`, `kind` (`llm`/`tool`), `model_or_tool`, `prompt_tokens`, `completion_tokens`, `cost_usd`,则 选项 `label` / `reserved` 你准备好了。 **没有提示, 没有信件内容, 没有 标识符** 超过a `label` 你选择... 这是... *已执行*,不仅仅是 承诺: `push_ledger` 用字段验证每行并拒绝任何记录 在计划之外,所以任何额外的 离开,即使一个手工编辑的分类账。 你的钥匙骑着 `Authorization` 信头和方向被拒绝,所以 键或分类账可以重寄给未经批准的主机。 重同步是安全的 — 同步 端点是一意孤行的 所以已经摄入的事件不是 一个从未来过电话的警卫 `enable_sync()` 从未发送(a) `sync()` ——无网络) —— [no-telemetry](#no-telemetry) 默认搁置 。 ## 当你超越当地的护栏时 `floe-guard` 停止超额使用 **每个进程,当地** ——无户口,无网. 当天花板需要支撑 整个舰队,主机Floe成为 记录系统:它花费每个供应商的每个电话 在一个分类账上,显示你的 每个客户的利润率,并让您从实际金额中扣除自己的客户的发票——与 执行移动了服务器侧 。 | | floe-guard( 此重播) (此重播) | 主机浮动 | |---|---|---| | 运行 | 当地,在你的进程中 | 服务器侧 | | 范围 | 一个进程 | 每个供应商和代理商 | | 控件 | 用力停在你的帽子边 | 杀开关+一个分类账:成本、差值、重新计费 | 已经在主机Floe上运行了 ? 该软件包唯一的网络呼叫是选择进入主机 预算改为:设置 `FLOE_API_KEY` (代理密钥) `floe_…`和(或) `hosted_remaining_usd()` 返回服务器侧预算头室 `GET /v1/agents/credit-remaining`. `FLOE_API_BASE_URL` 覆盖 API 主机( 默认) `https://credit-api.floelabs.xyz`。除非设置了密钥,否则不会运行。 密码 [单线升级](#one-line-to-hosted) 为: `BudgetGuard.from_floe(api_key=…)`, 以该会议室作为当地最高限额——预算,而不是平衡,以及 执法不动。 → [dev-dashboard.floelabs.xyz (英语).](https://dev-dashboard.floelabs.xyz/) ## 实例 所有可运行的例子都存在其中 [`examples/`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/>)使用时 `python examples/<file>` 从雷波根。 | 示例 | 说明 | 额外 | API 密钥/网络 | |---|---|---|---| | [`runaway_loop.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/runaway_loop.py>) | 犬形硬站演示——在穿越$0.10前停止一个树枝循环. | 无 | 无 | | [`streaming_guard.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/streaming_guard.py>) | 飞行前区块通过超大一级呼叫+中流截断 `guard_stream()` | 无 | 无 | | [`budget_aware.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/budget_aware.py>) | 了解背景的胶带: 代理下移到便宜的模型中 `advisory().near_limit` 旅费 | 无 | 无 | | [`budget_retry.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/budget_retry.py>) | 预算意识重试/优雅的退化 `with_budget_retry()` | 无 | 无 | | [`context_size.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/context_size.py>) | 背景大小适应预算:历史经过调整和 `max_tokens` 上限在天花板附近 | 无 | 无 | | [`plan_complexity.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/plan_complexity.py>) | 计划复杂度适应性:可选子任务下降,推理深度降低到顶端附近 | 无 | 无 | | [`retrieval_depth.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/retrieval_depth.py>) | RAG `top_k` 作为预算排水量,缩水分为两个步骤(20 → 12 → 5). | 无 | 无 | | [`step_budget.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/step_budget.py>) | 相继循环的逐步符号封顶: 未停止运行就屏蔽了一个离家步骤 | 无 | 无 | | [`tool_budget.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/tool_budget.py>) | 作为同一USD天花板的一等公民,工具支出(Apollo lookups, Exa search) | 无 | 无 | | [`openai_adapter.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/openai_adapter.py>) | `guarded_completion` 对抗鸭子型的支架——练习真正的飞行前坚守 | 无 | 无 | | [`anthropic_adapter.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/anthropic_adapter.py>) | 带有本地即时缓存定价的 Anthropic 适配器( 缓存写入 v 缓存读取 v 未选中) | 无 | 无 | | [`langgraph_budget_aware.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/langgraph_budget_aware.py>) | LangGraph `guarded_node` 用一个咨询驱动的路由器,在盖子前抓取 | `pip install floe-guard[langgraph]` | 无 | | [`langchain_groq_example.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/langchain_groq_example.py>) | LangChain在ChatGroq(Llama-3)上的回调处理器:呼叫1成功,呼叫2是硬停止的 | `pip install floe-guard[langchain] langchain-groq` | `GROQ_API_KEY` + 网络 | | [`voice_turn_budget.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_turn_budget.py>) | 输油管 `FloeBudgetGuardProcessor`:多回合的语音对话停止中途 | `pip install floe-guard[pipecat]` | 无 | | [`voice_call_cost_pipecat.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_pipecat.py>) | 完全每条腿的呼叫费用(STT + LLM + TTS + 电话)通过Pipecat,从捆绑的地图中定价 | `pip install floe-guard[pipecat]` | 无 | | [`voice_call_cost_livekit.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_livekit.py>) | 完全每条腿的呼叫费用(STT + LLM + TTS + 电话)通过LiveKit,从捆绑的地图中定价. | `pip install floe-guard[livekit]` | 无 | | [`voice_call_cost_vapi.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_vapi.py>) | 通过 Vapi 自定义- LLM 代理,从捆绑的地图中定价的全程调用费用( STT + LLM + TTS + 电话) | `pip install floe-guard` | 无 | | [`voice_call_cost_retell.py`](<https://github.com/Floe-Labs/floe-guard/blob/main/examples/voice_call_cost_retell.py>) | 通过Retell自定义-LLM WebSocket从捆绑的地图中定价的全程单程呼叫费用(STT + LLM + TTS + 电话) | `pip install floe-guard` | 无 | ## 以浮点护栏建造 在您的项目中使用浮标护卫? 添加徽章让其他人找到: [![由护船护卫](https://img.shields.io/badge/guarded%20by-floe--guard-2f81f7.svg)](https://github.com/Floe-Labs/floe-guard) ```markdown [![guarded by floe-guard](https://img.shields.io/badge/guarded%20by-floe--guard-2f81f7.svg)](https://github.com/Floe-Labs/floe-guard) ``` ## 发展 ```bash pip install -e ".[dev]" pytest ruff check . ``` 关于TypeScript软件包,参见: [`js/README.md`](<https://github.com/Floe-Labs/floe-guard/blob/main/js/README.md>)B. 捐款 欢迎——首先: [CONTRIBUTING . md (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/CONTRIBUTING.md>); 释放为 追踪到 [CHANGELOG . md (英语).](<https://github.com/Floe-Labs/floe-guard/blob/main/CHANGELOG.md>). ## 许可证 MIT——见 [LICENSE](<https://github.com/Floe-Labs/floe-guard/blob/main/LICENSE>).

查看原始来源