The Agent Financial Protocol (AFP) - Financial Rails for the Machine Economy
The protocol that lets AI agents transact - 24/7, instant, programmable, secure.
AgentRails implements the Agent Financial Protocol (AFP), a complete financial system designed from the ground up for autonomous AI agents. Unlike traditional payment systems built for humans, AFP provides:
- Agent Identity: Decentralized identifiers (DIDs), reputation scores, and trust tiers
- Payment Primitives: Instant transfers, continuous streams, conditional escrows
- Session Keys: Time-limited, scoped permissions for autonomous operation
- On-Chain Infrastructure: Solana smart contracts for trustless execution
npm install @agentrails/solanaimport { AFP, Keypair } from '@agentrails/solana';
// Initialize AFP client
const wallet = Keypair.generate();
const afp = new AFP({
network: 'devnet',
wallet,
});
// Register as an agent
await afp.registerAgent({
did: 'did:afp:solana:myagent:0',
metadata: { name: 'MyAIAgent', purpose: 'Autonomous trading' },
});
// Send a payment
const result = await afp.pay({
to: 'RecipientAddress',
amount: 10.00, // 10 USDC
memo: 'Payment for API access',
});
console.log('TX:', result.txHash);// Create a session key for autonomous operation
const sessionKeypair = Keypair.generate();
await afp.createSessionKey({
publicKey: sessionKeypair.publicKey.toBase58(),
spendingLimit: 100, // $100 USDC limit
expiresIn: 86400, // 24 hours
});
// Agent can now pay autonomously within limits
await afp.payWithSessionKey({
agent: afp.address!,
sessionKey: sessionKeypair,
to: 'ServiceProvider',
amount: 5.00,
memo: 'GPT-4 API calls',
});// Stream $10/hour to a compute provider
await afp.createStream({
to: 'ComputeProvider',
ratePerHour: 10,
deposit: 100, // Initial $100 deposit
duration: 86400, // 24 hours
});// Create escrow for task completion
const escrow = await afp.createEscrow({
to: 'TaskProvider',
amount: 50,
expires: 86400, // 24 hours
condition: 'taskCompletion',
onExpire: 'refund',
});
// Release when task is complete
await afp.releaseEscrow({
depositor: afp.address!,
beneficiary: 'TaskProvider',
nonce: escrow.nonce,
});┌─────────────────────────────────────────────────────────────────┐
│ Agent Financial Protocol │
├─────────────────────────────────────────────────────────────────┤
│ Layer 5: Governance & Arbitration │
├─────────────────────────────────────────────────────────────────┤
│ Layer 4: Service Marketplace (Coming Soon) │
├─────────────────────────────────────────────────────────────────┤
│ Layer 3: Credit & Lending (Coming Soon) │
├─────────────────────────────────────────────────────────────────┤
│ Layer 2: Payment Primitives │
│ ┌──────────┬──────────┬──────────┬──────────┬──────────┐ │
│ │ Transfer │ Stream │ Escrow │ Request │ Batch │ │
│ └──────────┴──────────┴──────────┴──────────┴──────────┘ │
├─────────────────────────────────────────────────────────────────┤
│ Layer 1: Agent Identity │
│ ┌──────────┬──────────┬──────────┬──────────┐ │
│ │ DID │Reputation│ Vouch │ Trust │ │
│ └──────────┴──────────┴──────────┴──────────┘ │
├─────────────────────────────────────────────────────────────────┤
│ Solana Programs │
│ ┌──────────────┬──────────────┬──────────────┬───────────────┐│
│ │Agent Registry│Payment Router│Stream Factory│Escrow Factory ││
│ └──────────────┴──────────────┴──────────────┴───────────────┘│
└─────────────────────────────────────────────────────────────────┘
| Package | Description |
|---|---|
@agentrails/solana |
Solana adapter with on-chain program bindings |
@agentrails/sdk |
High-level SDK with REST API support |
@agentrails/core |
Core types and utilities |
AFP is implemented as four Solana programs written with Anchor:
- Agent registration with DIDs
- Reputation scores (0-1000)
- Trust tiers with transaction limits
- Vouching system with stake
- Direct transfers with protocol fees
- Session key management
- Agent wallets (PDAs)
- Spending limit enforcement
- Continuous payment streams
- Rate-per-second accounting
- Cliff periods
- Pause/resume/cancel
- Conditional payments
- Multiple condition types (signature, oracle, task completion)
- Dispute resolution with arbiters
- Automatic expiration handling
| Tier | Reputation | Transaction Limit |
|---|---|---|
| Unverified | 0 | $100 |
| Basic | 100+ | $1,000 |
| Verified | 300+ | $10,000 |
| Trusted | 500+ | $100,000 |
| Premium | 700+ | $1,000,000 |
| Institutional | 900+ | Unlimited |
See the /examples directory:
- autonomous-agent: Full autonomous agent implementation
- claude-agent: Integration with Claude's function calling
- Node.js 18+
- pnpm 8+
- Rust/Cargo (for Solana programs)
- Anchor CLI 0.29+
- Solana CLI 1.17+
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run tests
pnpm test# Deploy Solana programs
cd contracts/solana
./scripts/deploy.sh devnet
./scripts/initialize.ts- Agent Registry program
- Payment Router program
- Stream Factory program
- Escrow Factory program
- Solana SDK package
- Session key infrastructure
- Example implementations
- Base/Arbitrum adapters
- Credit pool program
- Service marketplace
- Python SDK
- x402 protocol bridge
- MCP server integration
- Enterprise dashboard
For the complete AFP specification, see:
- AFP-SPEC.md - Full protocol specification
- AI-INTEGRATIONS.md - Claude/LangChain/CrewAI integrations
- CONTRACTS.md - Smart contract architecture
We're building the financial infrastructure for the machine economy. Contributions welcome!
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
MIT
Built for the agent economy.
The machines are coming. Make sure they can pay.