How to Add ATXP to Dify
Dify is one of the fastest-growing open-source LLM platforms globally — but Dify agent payments is a gap the platform hasn’t closed. Your Dify workflows can chain LLM steps, call tools, and automate complex tasks. The moment an agent needs to pay for something — a search API, a data enrichment service, a browser automation call — you’re back to hardcoded API keys, per-service billing accounts, and zero visibility into what the workflow actually spent.
ATXP closes that gap. This guide walks through how to wire ATXP’s payment and identity layer into a Dify workflow using Dify’s custom tool interface. The worked example: a research agent that pays for Perplexity API calls and returns itemized receipts for every run.
What Is Dify and Why Do Dify Agent Payments Matter?
Dify is an open-source platform for building LLM-powered applications and agent workflows. As of Q1 2026, it has surpassed 80,000 GitHub stars (GitHub, March 2026) and is widely deployed for:
- Multi-step research and summarization pipelines
- Customer support agents with external tool access
- Content generation workflows pulling from live data
- Enterprise automation (via self-hosted or the Dify cloud edition)
What Dify does well: chaining LLM calls, defining tools, managing conversation state, and giving non-engineering teams a visual workflow builder.
What Dify doesn’t include: any native payment infrastructure. When a Dify tool calls a paid API, credentials live in environment variables and charges hit whatever billing account was wired up at setup time. There is no per-run budget, no agent-level spending limit, no receipt, and no way to know which workflow caused a $200 API spike.
That works for a solo developer running one workflow on one service. It breaks when:
- Multiple workflows share the same API credentials
- You need cost tracking per conversation, per task, or per client
- You want a hard spending cap on an agent — not just hope it doesn’t loop
- You’re billing clients back based on actual agent consumption
ATXP gives each Dify agent a real budget with real accountability. Why API key sprawl is a security problem →
How Dify Agent Payments Work With ATXP
ATXP exposes a unified payment API that Dify treats as a custom tool. Instead of storing individual API keys for each paid service a workflow calls, you store one ATXP credential. ATXP handles the rest:
- Routing — determines which service to call based on the request
- Spending limits — enforces hard caps per task, per session, or per account
- Receipts — returns structured JSON receipts for every transaction
- Credential isolation — each agent session has its own spend context; a runaway workflow can’t drain a shared account
Here’s the model in one sentence: your Dify agent calls ATXP, ATXP calls the target service, ATXP returns the result plus a receipt, and the charge is debited from the agent’s allocated budget.
| Approach | Credential model | Per-task budget | Receipt | Blast radius at failure |
|---|---|---|---|---|
| Direct API keys in Dify env vars | Shared across all workflows | No | No | Unlimited |
| Per-workflow API keys | Better isolation, still manual | No | No | Per-service, still unlimited |
| ATXP via Dify custom tool | One credential, agent-scoped | Yes | Yes | Bounded by budget cap |
What You’ll Need
- A Dify account (cloud or self-hosted; this guide uses the cloud edition)
- An ATXP account — sign up at atxp.ai
- An ATXP API key and agent ID from the ATXP dashboard
- Familiarity with Dify’s workflow builder or chatflow editor
Step 1 — Create an ATXP Agent Account
Every Dify workflow you want to track separately should have its own ATXP agent account. Each account gets a wallet, a configurable spending limit, and an isolated transaction log.
In the ATXP dashboard, create a new agent account and set a starting budget. For testing, $5 covers hundreds of typical API calls. For production research agents, $20–50 per workflow per day is a reasonable starting cap depending on query volume.
Note your agent_id and api_key — you’ll use both in Step 2. More on what an agent account includes →
Step 2 — Define the ATXP Tool in Dify
Dify supports custom tools via OpenAPI 3.1 schema. Navigate to Tools → Custom Tools → Create Tool and paste this schema:
openapi: 3.1.0
info:
title: ATXP Payment Tool
description: Route paid API calls through ATXP with per-task budget enforcement
version: 1.0.0
servers:
- url: https://api.atxp.ai/v1
paths:
/call:
post:
operationId: pay_and_call
summary: Route a request through ATXP to a target service
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [agent_id, service, request]
properties:
agent_id:
type: string
description: ATXP agent account ID
service:
type: string
description: Target service slug (e.g. "perplexity", "serper", "browserbase")
request:
type: object
description: The request payload to forward to the target service
budget_cap:
type: number
description: Optional per-call hard cap in USD; overrides account default
responses:
'200':
description: Service response plus receipt
content:
application/json:
schema:
type: object
properties:
result:
type: object
receipt:
type: object
properties:
transaction_id:
type: string
amount_usd:
type: number
service:
type: string
timestamp:
type: string
/receipt/{transaction_id}:
get:
operationId: get_receipt
summary: Retrieve receipt for a completed transaction
parameters:
- name: transaction_id
in: path
required: true
schema:
type: string
In the tool’s Authorization settings, add your ATXP API key as a Bearer token. This is the only external credential Dify needs to store for your ATXP-routed services.
Step 3 — Build the Research Agent Workflow
Here’s the complete workflow: a user submits a research question, Dify formats it, ATXP routes it to Perplexity, and the workflow returns both the answer and the transaction receipt.
Workflow nodes:
- Start — user inputs a research question
- LLM (Query Formatter) — cleans the question into a focused Perplexity prompt
- Tool: ATXP pay_and_call — sends the formatted prompt via ATXP
- If/Else — checks whether ATXP returned an error (budget exhausted, service down)
- LLM (Answer Formatter) — structures Perplexity’s raw response
- End — returns formatted answer plus receipt JSON
Tool call configuration for node 3:
{
"agent_id": "{{env.ATXP_AGENT_ID}}",
"service": "perplexity",
"request": {
"model": "sonar",
"messages": [
{
"role": "user",
"content": "{{nodes.query_formatter.output}}"
}
]
},
"budget_cap": 0.05
}
The budget_cap: 0.05 hard-fails if the Perplexity cost for a single call exceeds $0.05. At Perplexity’s current sonar pricing of approximately $0.001 per query as of March 2026 (Perplexity AI pricing page, March 2026), you will never hit this in normal operation. But if a malformed prompt triggers an unusually expensive completion, or a loop bug fires this node 500 times, the cap holds. Why per-task budgets are the right model →
Is your Dify workflow spending more than it should?
ATXP gives every agent a real budget with real receipts — one credential, per-task spending caps, and an audit trail for every call. Start free at atxp.ai →
Step 4 — Surface the Receipt in Workflow Output
The ATXP tool call returns this alongside the service result:
{
"transaction_id": "txn_01HX7KQMP3V",
"amount_usd": 0.0009,
"service": "perplexity",
"model": "sonar",
"tokens_used": 1240,
"timestamp": "2026-03-31T14:22:07Z",
"budget_remaining_usd": 19.47
}
Wire it through to the End node output:
{
"answer": "{{nodes.answer_formatter.output}}",
"cost_usd": "{{nodes.atxp_call.receipt.amount_usd}}",
"transaction_id": "{{nodes.atxp_call.receipt.transaction_id}}",
"budget_remaining": "{{nodes.atxp_call.receipt.budget_remaining_usd}}"
}
Every workflow run now returns what the agent found and exactly what it cost. For teams using Dify to serve multiple clients or departments, this is the data you need for accurate cost allocation without combing through API dashboards. How to track what your agent spent →
Step 5 — Handle Budget Exhaustion Gracefully
When an agent’s ATXP budget runs out, pay_and_call returns a structured error rather than throwing:
{
"error": "budget_exhausted",
"message": "Agent budget cap reached. Remaining: $0.00.",
"transaction_id": null,
"amount_usd": 0
}
In the If/Else node after the tool call:
- If
nodes.atxp_call.error == "budget_exhausted"→ route to a graceful message: “This task has used its allocated research budget. Contact your administrator to increase the limit.” - Else → continue to the Answer Formatter
Without this handler, a budget exhaustion returns a null result to the LLM formatter, which then produces a confused or hallucinated answer. Handle it explicitly.
Comparison: What ATXP Adds That Dify Can’t Do Alone
| Capability | Dify native | ATXP + Dify |
|---|---|---|
| Per-task spending cap | No | Yes — configurable per call |
| Per-agent budget isolation | No | Yes — one account per workflow/client |
| Transaction receipt | No | Yes — structured JSON every call |
| Credential scope | Env var shared across Dify instance | Agent-scoped, isolated per account |
| Budget exhaustion handling | Silent null / error | Structured error with remaining balance |
| Spend audit trail | No | Yes — full log in ATXP dashboard |
| Multi-service single credential | No | Yes — route to any integrated service |
The Dify-native approach is the right starting point. The moment you have more than one workflow, more than one team member with API access, or any need to track costs accurately, the ATXP layer pays for itself quickly.
What About Dify’s Built-In Tool Marketplace?
Dify’s marketplace includes pre-built connectors for Tavily, Wikipedia, Stable Diffusion, and others. These are convenient but run under Dify’s shared credential model — no per-workflow or per-agent spending isolation, no receipts.
ATXP and Dify’s native tools coexist. Use native Dify tools for calls where cost tracking doesn’t matter. Route premium, mission-critical, or client-billable calls through ATXP. There’s no conflict — a single workflow can mix both.
Frequently Asked Questions
Does this work with Dify self-hosted?
Yes. The ATXP custom tool connects over HTTPS to api.atxp.ai. As long as your self-hosted Dify instance can make outbound HTTPS requests (default behavior), the integration is identical. Store the ATXP_AGENT_ID in your self-hosted .env file rather than Dify cloud’s secrets manager.
Can I set different budgets for different Dify workflows?
Yes. Create a separate ATXP agent account for each workflow — or each client, or each department. Each account has its own spending limit and transaction log. In Dify, reference the appropriate agent_id in each workflow’s tool configuration. You can also override the per-call budget_cap in individual tool node configurations without changing the account-level limit.
What services does ATXP route to besides Perplexity?
ATXP integrates with search, data enrichment, image generation, and browser automation services via the service slug in pay_and_call. The full catalog is in the ATXP dashboard and is updated as new integrations launch.
What happens if the target service is down when ATXP routes a call?
ATXP returns a structured service_unavailable error and does not debit the agent account. The amount_usd field will be 0. Your Dify If/Else handler should check for this alongside the budget_exhausted case.
Is a native Dify marketplace listing planned?
The custom tool method in this guide is the current production path and works in live deployments. A Dify marketplace listing is on the ATXP roadmap. Until it ships, the OpenAPI schema approach here is fully supported.
What to Build Next
Once ATXP is wired into Dify, the obvious next steps:
- Multi-service workflows — route research to Perplexity, structured lookups to a data API, and browser tasks to a headless browser service — all through one ATXP agent account with one unified receipt
- Client billing — create one ATXP account per client, pull the transaction log weekly, and produce itemized invoices without touching any underlying API dashboards
- Budget alert webhooks — configure ATXP to notify when a workflow approaches its daily cap, before it hits zero mid-run
For the foundational model this is built on: How to Build an AI Agent Without API Keys →