Skip to main content

Run a margin-aware paid AI agent with Alephant x402 and Discord

Workflow preview

Workflow preview
100%
Run a margin-aware paid AI agent with Alephant x402 and Discord preview
Open on n8n.io

1. Workflow Overview

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, r...

Best for

  • CRM automation workflows
  • AI Chatbot automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.stickynote, n8n-nodes-base.webhook, @alephantai/n8n-nodes-alephant-analytics.alephantusage, n8n-nodes-base.if, n8n-nodes-base.respondtowebhook, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.lmchatopenai, @alephantai/n8n-nodes-alephant.alephanttool

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by alephantAI.

Original n8n.io source

1.1 Workflow description

Title
Run a margin-aware paid AI agent with Alephant x402 and Discord
Workflow name
Run a margin-aware paid AI agent with Alephant x402 and Discord

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

  1. Receives a POST request on a webhook endpoint that is intended to be paid per call over x402.
  2. Checks current budget status using Alephant Usage analytics and returns a 402-style JSON error response if the workspace budget is exceeded.
  3. 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.
  4. Runs the refined prompt on the chosen model using Alephant AI to capture per-call usage and cost metadata.
  5. Calculates per-call profit and margin percentage from fixed revenue, token usage, and an Alephant fee estimate.
  6. 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

  1. Install the Alephant community nodes and create an Alephant Virtual Key credential in n8n.
  2. 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.
  3. Configure a Discord Webhook credential and set it on the Discord node used for the margin feed.
  4. 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.
  5. 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.
  6. 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).

Requirements

  • Alephant account with a Virtual Key, free tier works (https://alephant.io)
  • Alephant community nodes installed: Cost Control, AI Analytics, AI Analytics Tool
  • OpenAI credential with Base URL set to https://ai.alephant.io/v1 and your Alephant virtual key as the API key
  • A Discord channel webhook for the margin feed
  • Optional: an Alephant policy endpoint if you want the auto-throttle write-back

Customization

  • Change the service by editing the agent system prompt (research summary, wallet-risk scoring, data enrichment, etc.)
  • Adjust the model tiers and routing logic the agent chooses between (premium vs economy)
  • Set your real price, token rate, and fee in the Per-Call P&L node
  • Tune the margin threshold that triggers throttling (default 20%) in the Margin Thin node
  • Swap Discord for Slack or email, or add a Google Sheets row per call
  • Register the webhook as a paid x402 endpoint in Alephant to charge per request in USDC

Additional info

Full information: https://developers.alephant.io/docs/overview/showcase/n8n-workflow

This workflow turns an n8n webhook into a paid, self-funding AI endpoint. A buyer pays per request in USDC over x402; Alephant verifies and settles the payment before the request reaches n8n, so revenue is booked before any work runs. A budget guard gates the call, then an AI agent reads recent spend through the Alephant UsageSummary tool and picks the cheapest model that still does the job. The work runs through Alephant for full cost tracking, and a P&L step calculates net margin (revenue minus token spend minus fee) on every single call. Healthy calls just respond and log; calls that drop below the margin threshold trigger a write-back to throttle, so a bad call never quietly loses money. The agent's own reasoning is also routed through Alephant via the OpenAI Base URL override, so the thing watching cost appears in its own dashboard. Notes: the Alephant analytics tool is read-only, so enforcement happens through the HTTP policy node, not the tool. After you run the Budget Guard node once, map the real budget field into the Budget OK node, since field names depend on your workspace response. Hard budget caps still live in the Alephant Budget Circuit Breaker; this workflow tunes within those rails.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 2 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 3 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 4 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 5 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 7 - Paid Endpoint (x402 target)

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2

Block 8 - Budget Guard (deterministic)

Type / Role
@alephantai/n8n-nodes-alephant-analytics.alephantUsage - alephantUsage
Config choices
Version 1

Block 9 - Budget OK?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 10 - Respond 402

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.1

Block 11 - AI Agent (Router)

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3.1

Block 12 - Agent Brain (routed through Alephant)

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.2

Block 13 - UsageSummary analytics in Alephant

Type / Role
@alephantai/n8n-nodes-alephant.alephantTool - alephantTool
Config choices
Version 1

Block 14 - Run Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryBufferWindow - memoryBufferWindow
Config choices
Version 1.3

Block 15 - Parse Decision

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 16 - Run Chosen Model (Cost Control)

Type / Role
@alephantai/n8n-nodes-alephant-ai.alephantAi - alephantAi
Config choices
Version 1

Block 17 - Per-Call P&L

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 18 - Respond to Buyer

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.1

Block 19 - Margin Thin?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 20 - Tighten Policy (write-back)

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.2

Block 21 - Margin Feed (Discord)

Type / Role
n8n-nodes-base.discord - discord
Config choices
Version 2

3. Summary Table

Workflow Run a margin-aware paid AI agent with Alephant x402 and Discord
Complexity advanced
Nodes 21
Categories CRM, AI Chatbot
Author alephantAI
Published 11 Jun 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/16259/16259.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Run a margin-aware paid AI agent with Alephant x402 and Discord do?

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, r...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your CRM, AI Chatbot use case.