Examples
Self-contained programs that exercise each core capability. Run any example and inspect the resulting fuze-traces.jsonl to see what was recorded.
TypeScript
01, Basic Guard
The simplest possible example. Wrap a function with guard() and see the trace output.
import { guard } from 'fuze-ai'
const search = guard(async function searchDocuments(query: string) {
return await vectorDb.search(query)
})
const results = await search('AI agent safety')
// Check ./fuze-traces.jsonl for the trace02, Budget Ceiling
Set a $1.00 budget. Each step costs $0.30. The 4th call gets blocked.
import { guard, configure, BudgetExceeded } from 'fuze-ai'
configure({ defaults: { maxCostPerRun: 1.00 } })
const analyse = guard(analyseFn, { maxCost: 0.30 })
try {
await analyse('doc')
} catch (err) {
if (err instanceof BudgetExceeded) {
// Budget ceiling hit, step blocked before execution
}
}03, Loop Detection
Simulate an agent stuck retrying the same failed search. Fuze catches it after 3 identical calls.
import { guard, configure, LoopDetected } from 'fuze-ai'
configure({
defaults: { maxIterations: 20, onLoop: 'kill' },
loopDetection: { repeatThreshold: 3 },
})
const search = guard(searchFn)
// 3rd identical call throws LoopDetected04, Side-Effect Tracking
Create an invoice (side-effect with compensation), then fail on email send. Shows how Fuze tracks which steps need rollback.
const invoice = guard(createInvoice, {
sideEffect: true,
compensate: cancelInvoice,
})05, Multi-Agent
Two agents (researcher + writer) share a single budget using createRun().
import { createRun } from 'fuze-ai'
const run = createRun('research-team', { maxCostPerRun: 5.00 })
const search = run.guard(webSearch, { maxCost: 0.10 })
const draft = run.guard(writeDraft, { maxCost: 1.00 })
await search('query')
await draft('outline')
console.log(run.getStatus()) // { totalCost, stepCount, ... }
await run.end()06, MCP Proxy
Protect any MCP server with zero code changes.
See the full MCP Proxy docs for configuration options.
Python
01, Basic Guard
02, Budget Ceiling
03, Loop Detection
04, Side-Effects
05, LangGraph Adapter
Running all examples
Every example produces a fuze-traces.jsonl file. Each line is a JSON record with timestamps, costs, and guard events.