-
Notifications
You must be signed in to change notification settings - Fork 584
docs: add spending policy integration guide (LetAgentPay) #166
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
Open
maximberg
wants to merge
1
commit into
BlockRunAI:main
Choose a base branch
from
maximberg:add-letagentpay-policy-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Spending Policies with LetAgentPay | ||
|
|
||
| ClawRouter answers *how* an agent pays — wallet signature for auth, USDC micropayments via x402, no API keys, no accounts. It does not answer *should this agent pay this amount right now*. | ||
|
|
||
| With a funded wallet and a runaway loop, an agent can drain its balance across hundreds of routed LLM calls in seconds. That's where a policy layer in front of the wallet helps. | ||
|
|
||
| [LetAgentPay](https://letagentpay.com) is a vendor-neutral spending policy engine — open-source ([repo](https://github.com/letagentpay/letagentpay), MIT, self-host or cloud) that runs 8 deterministic checks on a proposed spend before it leaves the agent. | ||
|
|
||
| This guide shows the integration pattern. Disclosure: I work on LetAgentPay. The doc lives in this repo because ClawRouter users have asked about budget caps and this gap is real. | ||
|
|
||
| ## The pattern | ||
|
|
||
| A thin wrapper around your existing ClawRouter usage: ask the policy engine → call ClawRouter → confirm actual cost. | ||
|
|
||
| ```typescript | ||
| import { LetAgentPay } from "letagentpay"; | ||
| import { ClawRouter } from "@blockrun/clawrouter"; | ||
|
|
||
| const policy = new LetAgentPay({ token: process.env.LETAGENTPAY_TOKEN! }); | ||
| const router = new ClawRouter(/* your ClawRouter config */); | ||
|
|
||
| async function policedChat(opts: { | ||
| messages: { role: string; content: string }[]; | ||
| estimatedCostUsd: number; | ||
| description?: string; | ||
| }) { | ||
| // 1. Pre-flight: ask the policy engine before the spend | ||
| const purchase = await policy.requestPurchase({ | ||
| amount: opts.estimatedCostUsd, | ||
| category: "llm_inference", | ||
| merchantName: "ClawRouter", | ||
| description: opts.description ?? "Routed LLM call", | ||
| }); | ||
|
|
||
| if (purchase.status === "rejected") { | ||
| throw new Error(`Policy denied: ${purchase.policyCheck?.failedCheck}`); | ||
| } | ||
| if (purchase.status === "pending") { | ||
| throw new Error(`Policy escalated to human review: ${purchase.requestId}`); | ||
| } | ||
|
|
||
| // 2. Auto-approved — proceed with ClawRouter | ||
| const result = await router.chat({ messages: opts.messages }); | ||
|
|
||
| // 3. Confirm actual x402-settled cost | ||
| await policy.confirmPurchase(purchase.requestId, { | ||
| success: true, | ||
| actualAmount: result.costUsd, | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| ## A starter policy | ||
|
|
||
| ```json | ||
| { | ||
| "version": "1.0", | ||
| "daily_limit": 20.00, | ||
| "per_request_limit": 1.00, | ||
| "allowed_categories": ["llm_inference"], | ||
| "schedule": { | ||
| "timezone": "UTC", | ||
| "default": { "allow": "00:00-23:59" } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| What this catches: | ||
|
|
||
| | Concern | Control | | ||
| | --- | --- | | ||
| | One runaway call draining the wallet | `per_request_limit` | | ||
| | A loop running all night | `daily_limit` + `schedule` | | ||
| | Agent spending outside intended scope | `allowed_categories` | | ||
| | Total wallet protection | `weekly_limit` / `monthly_limit` / account budget | | ||
|
|
||
| ## Notes | ||
|
|
||
| - **Estimate conservatively.** ClawRouter picks the model after the call lands. Estimate worst-case (max model × max tokens), then `confirmPurchase` reports the real x402-settled amount. The held amount releases on confirm. | ||
| - **Failed routes.** If ClawRouter rejects the request, call `confirmPurchase` with `success: false` to release the hold. | ||
| - **Self-host.** LetAgentPay runs as a self-hostable service or as managed cloud. Same SDK either way. | ||
|
|
||
| ## See also | ||
|
|
||
| - [LetAgentPay docs](https://letagentpay.com/docs) — full setup, policy reference, dashboard | ||
| - [ASPS spec](https://letagentpay.com/asps/spec) — open spec for agent spending policies, vendor-neutral | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling to match documented guidance.
The code snippet doesn't implement the failure handling described in the Notes section (line 82). If
router.chatthrows an error or rejects the request, the code should callconfirmPurchasewithsuccess: falseto release the hold, but currently errors would leave the hold unreleased.🛡️ Proposed fix to add error handling
🤖 Prompt for AI Agents