-
Notifications
You must be signed in to change notification settings - Fork 10
feat: tool spend as a first-class primitive with reserve/settle parit… #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
442c07b
feat: tool spend as a first-class primitive with reserve/settle parit…
shivamfloe 4007607
feat: add changes post test.
shivamfloe 19f4db9
feat: add feedback changes.
shivamfloe d9736ce
feat: conflict changes resolution.
shivamfloe 7e43b7c
Merge branch 'main' into feat/tool-tracking
shivamfloe 789f427
Merge branch 'main' into feat/tool-tracking
shivamfloe 7ffde29
fix: changes around
shivamfloe 193cd56
fix: minor readme change.
shivamfloe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """Tool-spend kill-switch demo — runs with NO API key and NO account. | ||
|
|
||
| Some agents spend more per run on paid tool calls (Apollo lookups, Exa | ||
| searches, scraping APIs) than on LLM tokens. Those costs never touch a token | ||
| cost map — the caller knows the price — but they burn the same real dollars. | ||
| This demo shows tool spend as a first-class citizen of the SAME ceiling: | ||
|
|
||
| 1. ``reserve_tool``/``settle_tool`` — the pre-call hard-stop. A tool's price is | ||
| known BEFORE the call, so enforcement is exact: the crossing call never runs. | ||
| 2. ``check()`` + ``record_tool`` — the sequential loop contract. A runaway | ||
| tool loop dies at the ceiling, exactly like a runaway LLM loop. | ||
| 3. ``tool_costs`` — per-tool attribution, so you can see where the money went. | ||
|
|
||
| Run it:: | ||
|
|
||
| python examples/tool_budget.py | ||
|
|
||
| The "tools" are stubs — no network, no keys, no real spend. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from floe_guard import BudgetExceeded, BudgetGuard | ||
|
|
||
| APOLLO_COST = 0.02 # $ per people-lookup — known up front | ||
| EXA_COST = 0.004 # $ per search | ||
|
|
||
|
|
||
| def stub_apollo_lookup(company: str) -> dict[str, str]: | ||
| return {"company": company, "contact": "jane@..."} | ||
|
|
||
|
|
||
| def stub_exa_search(query: str) -> list[str]: | ||
| return [f"result for {query!r}"] | ||
|
|
||
|
|
||
| def main() -> None: | ||
| guard = BudgetGuard(limit_usd=0.10) | ||
| print(f"Budget: ${guard.limit_usd:.2f} — shared by tokens AND tools\n") | ||
|
|
||
| # ── 1. pre-call hard-stop: reserve the KNOWN price before the call ───────── | ||
| print("Prospecting until the budget says stop...") | ||
| companies = 0 | ||
| try: | ||
| while True: | ||
| handle = guard.reserve_tool(APOLLO_COST) # raises BEFORE the call | ||
| stub_apollo_lookup(f"company-{companies}") | ||
| guard.settle_tool("apollo.people_lookup", APOLLO_COST, reserved=handle) | ||
| for _ in range(2): # a couple of searches per company | ||
| guard.check() # sequential contract works for tools too | ||
| stub_exa_search("intent signals") | ||
| guard.record_tool("exa.search", EXA_COST) | ||
| companies += 1 | ||
| except BudgetExceeded: | ||
| print(f" stopped after {companies} companies — the crossing call never ran.\n") | ||
|
|
||
| # ── 2. attribution: where did the money go? ──────────────────────────────── | ||
| print(f"spent ${guard.spent_usd:.4f} of ${guard.limit_usd:.2f}, by tool:") | ||
| for tool, total in sorted(guard.tool_costs.items()): | ||
| print(f" {tool:<22} ${total:.4f}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.