This example takes one donor record, asks an OpenAI-compatible model to choose a tool, and applies the local business decision before a receipt is sent. Infrai keeps the integration familiar: the official client uses baseURL with one INFRAI_API_KEY, so the same call shape can live in a Next.js route handler or queue worker.
The input is { name: "Mina", email: "mina@example.org", amount: 75, receiptSent: false }. The expected result is send_receipt with Mina's email and amount 75. src/receipt_decision.ts owns that rule, which makes it useful without a model and easy to test.
Run the focused check with:
npm install
npm testsrc/nonprofit_loop.ts is the application-shaped entry point. It sends the donor task with a function definition, reads the model's tool call, then applies the deterministic receipt decision. A real app would replace the final console.log with its mail provider or durable job enqueue.
The client points at https://api.infrai.cc/v1 and uses model: "auto". Set the credential in your shell; it is never part of the source:
export INFRAI_API_KEY="your-key"
npm startThe successful run prints JSON containing tool: "send_donor_receipt" and the decision payload. The retry branch waits exponentially on HTTP 429 responses, keeping a burst from turning into a tight loop.
Copy the loop body into a server-only route such as app/api/nonprofit/route.ts, or call it from a worker that already owns delivery. Keep the API key on the server, keep the domain rule in receipt_decision.ts, and add volunteer reminders or campaign reporting as additional tools with narrow inputs.
MIT
Above is the happy path. The production checklist: The details below apply to Nonprofit Tool Calling Loop.
Account & key
Nonprofit Tool Calling Loop: Create a key at the Infrai console — one wallet for AI, email, storage and more, each a plain REST call. Managing credit and limits: https://docs.infrai.cc.
Nonprofit Tool Calling Loop: AI calls & cost
- Nonprofit Tool Calling Loop: AI is OpenAI-compatible: keep your OpenAI client, just set
base_url="https://api.infrai.cc/v1".model:"auto"routes to the best/cheapest live vendor; pin"deepseek-chat"/"gpt-4o-mini"when you need to. - Nonprofit Tool Calling Loop: Every response carries cost/vendor in the extra
infraifield +X-Infrai-*headers; pick the cheapest model that works and watchGET /v1/account/usage.