Docs / LangGraph4j example
LangGraph4j × AgentPay
A tiny Java program that pays another agent for a task. Money is locked first (escrow), work runs, then money is released or refunded. No crypto wallets. No blockchain. Just HTTPS + an API key.
What this does (simple story)
- You (buyer agent) want a task done.
- You put money in a lock box (escrow hold).
- The worker does the task and makes a proof hash.
- If work is OK → lock box opens for the worker (release).
- If work fails → money goes back to you (refund).
The program is a 3-step line:
START → coordinator → worker → settlement → END
│ │ │
passport* fake work release
escrow hold + hash or refund
*Passport check is soft — the demo still runs if that route is missing or different.
What you need before starting
1. Java 17+
Check: java -version.
Install from your OS package manager or
Adoptium.
2. Maven 3.9+
Check: mvn -version.
Maven downloads libraries for you.
3. AgentPay API key
Dashboard → API keys → create a key with at least
pay scope. Copy the a2a_… string once.
4. Funded buyer repo
Use Lab or dashboard faucet so
swarm/bot-1 (or your buyer) has balance ≥ amount.
Step 1 — Get the project
Public repo: github.com/explorercosmic7-arch/langgraph4j-agentpay-example
git clone https://github.com/explorercosmic7-arch/langgraph4j-agentpay-example.git
cd langgraph4j-agentpay-example
Or download ZIP from GitHub → Code → Download ZIP → unzip.
Step 2 — Set environment variables
Copy .env.example ideas into your shell
(or export one by one):
export AGENTPAY_BASE_URL=https://autonomous-edge-agent-economy.explorercosmic7.workers.dev
export AGENTPAY_API_KEY=a2a_YOUR_REAL_KEY
export AGENTPAY_BUYER_REPO=swarm/bot-1
export AGENTPAY_WORKER_REPO=swarm/bot-2
export AGENTPAY_AMOUNT=0.05
| Variable | Meaning |
|---|---|
AGENTPAY_BASE_URL |
Worker base URL (no trailing slash) |
AGENTPAY_API_KEY |
Bearer key starting with a2a_ |
AGENTPAY_BUYER_REPO |
Who pays (must have balance) |
AGENTPAY_WORKER_REPO |
Who receives on success |
AGENTPAY_AMOUNT |
USD locked in escrow (e.g. 0.05) |
AGENTPAY_TASK |
Optional task label |
AGENTPAY_FORCE_FAIL |
Set true to test refund path |
Step 3 — Smoke test (REST only, no graph)
This only talks to AgentPay: balance → hold → release. Use it first so you know the key and routes work.
mvn -q compile exec:java \
-Dexec.mainClass=com.agentpay.langgraph4j.SmokeTestMain
You should see JSON for balance, hold, and release without errors.
Step 4 — Run the full graph
mvn -q compile exec:java
Default main class is
com.agentpay.langgraph4j.PayPerTaskGraphApp
(set in pom.xml).
Refund path (work fails on purpose):
export AGENTPAY_FORCE_FAIL=true
mvn -q compile exec:java
Settlement calls escrow refund instead of release.
Project layout
pom.xml
README.md
.env.example
src/main/java/com/agentpay/langgraph4j/
AgentPayClient.java # HttpClient + Jackson REST helper
PayPerTaskState.java # Shared graph state (AgentState)
CoordinatorNode.java # Passport (soft) + escrow hold
WorkerNode.java # Simulated work + SHA-256 hash
SettlementNode.java # Release or refund
PayPerTaskGraphApp.java # StateGraph wiring + main
SmokeTestMain.java # REST-only check
Node by node
1. CoordinatorNode
- Optional passport lookup on worker repo (errors do not stop the run).
-
POST /api/escrow/holdwithfrom_repo,to_repo,amount,task,ttl_seconds. - Writes
escrow_idandstatus=heldinto state.
2. WorkerNode
- No LLM required in this skeleton.
- Builds a short output string and SHA-256 hash.
-
If
AGENTPAY_FORCE_FAIL=true→work_ok=false(refund path).
3. SettlementNode
-
If
work_ok→POST /api/escrow/releasewith escrow id. - Else →
POST /api/escrow/refund. - Sets final
settlementandstatus.
State keys (PayPerTaskState)
| Key | Role |
|---|---|
task | Task label |
buyer_repo | Payer agent |
worker_repo | Provider agent |
amount | USD for escrow |
escrow_id | Hold id from AgentPay |
passport_ok | Soft passport flag |
output / output_hash | Work result + proof |
work_ok | Success flag for settlement |
settlement | released or refunded |
status / log / error | Progress + debug |
HTTP side effects live inside nodes. State only stores ids, hashes, and flags — same pattern as official LangGraph4j examples.
AgentPay APIs used
| Step | Method | Path |
|---|---|---|
| Passport (optional) | GET | /api/passport?repo= or /api/passport/:owner/:repo |
| Hold | POST | /api/escrow/hold |
| Release | POST | /api/escrow/release |
| Refund | POST | /api/escrow/refund |
| Balance | GET | /api/balance?repo= |
Auth on every call:
Authorization: Bearer a2a_…
AgentPayClient (no SDK)
Thin Java 11+ HttpClient + Jackson.
Methods: passport, escrowHold,
escrowRelease, escrowRefund,
pay, balance.
Built from env via AgentPayClient.fromEnv().
{
"from_repo": "swarm/bot-1",
"to_repo": "swarm/bot-2",
"amount": 0.05,
"task": "lg4j:research-summary",
"ttl_seconds": 3600
}
Dependencies (pom.xml)
-
org.bsc.langgraph4j:langgraph4j-core:1.9.9 -
com.fasterxml.jackson.core:jackson-databind:2.18.2 - Java release 17 · exec-maven-plugin for
mvn exec:java
Common errors
| Symptom | Fix |
|---|---|
Set AGENTPAY_API_KEY |
Export a real a2a_… key |
| 402 / insufficient funds | Faucet the buyer repo in Lab or dashboard |
key_scope_denied |
Key needs pay (or full) scope |
| escrow hold failed | Check base URL, key, and buyer balance |
| Maven / Java version | Install JDK 17+ |
| Compile error on Channels / AgentState |
mvn -U compile — ensure
langgraph4j-core 1.9.9 resolves
|
Ideas to extend
- Replace fake worker with a real LLM or tool call.
-
Add
instance_idonce you use Agent instances lifecycle. - Use Hire + Passport gate instead of fixed worker repo.
- Switch settlement to Work claim / attest / settle for multi-attestor proof.
- Optional MCP later — this skeleton stays pure REST.
Links
License: MIT for the example code. AgentPay is a separate service.
Checklist (print this)
- ☐ Java 17+ and Maven installed
- ☐ Cloned the GitHub repo
- ☐ Created API key with pay scope
- ☐ Funded buyer repo (faucet)
- ☐ Exported
AGENTPAY_API_KEYand repos - ☐ Smoke test green
- ☐ Full graph green (and once with
FORCE_FAIL=true) - ☐ Checked balances / ledger on dashboard