Quick Overview
This workflow exposes a paid webhook endpoint, verifies budget via Alephant usage analytics, routes the request through an OpenAI-based agent to pick the most cost-effective model, runs the chosen model through Alephant for cost tracking, then returns results with per-call margin reporting to Discord and optional throttling.
How it works
- Receives a POST request on a webhook endpoint that is intended to be paid per call over x402.
- Checks current budget status using Alephant Usage analytics and returns a 402-style JSON error response if the workspace budget is exceeded.
- Sends the buyer request and budget context to an OpenAI-based routing agent (via Alephant) that calls Alephant UsageSummary and outputs strict JSON selecting a model tier and refined prompt.
- Runs the refined prompt on the chosen model using Alephant AI to capture per-call usage and cost metadata.
- Calculates per-call profit and margin percentage from fixed revenue, token usage, and an Alephant fee estimate.
- Responds to the webhook caller with the model result plus net margin metrics, posts the margin line to Discord, and calls a policy write-back HTTP endpoint to throttle if margin drops below 20%.
Setup
- Install the Alephant community nodes and create an Alephant Virtual Key credential in n8n.
- Create an OpenAI credential that uses Base URL https://ai.alephant.io/v1 with your Alephant virtual key as the API key, and select it for the agent’s language model.
- Configure a Discord Webhook credential and set it on the Discord node used for the margin feed.
- Update the Per-Call P&L constants (price per call, token rate per 1k, external spend, and fee percentage) to match your real unit economics.
- Replace https://ai.alephant.io/REPLACE_WITH_POLICY_ENDPOINT with your real policy/throttling endpoint and set the ALEPHANT_API_KEY environment variable used for the Authorization header.
- Copy the webhook URL for the paid endpoint and configure your x402 buyer/agent to call this endpoint with the expected request body fields (for example, topic and runId).