Skip to content

noah-ing/AgentRails

Repository files navigation

AgentRails

The Agent Financial Protocol (AFP) - Financial Rails for the Machine Economy

The protocol that lets AI agents transact - 24/7, instant, programmable, secure.


Overview

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

Quick Start

Installation

npm install @agentrails/solana

Basic Usage

import { 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);

Autonomous Agent with Session Keys

// 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',
});

Payment Streams

// Stream $10/hour to a compute provider
await afp.createStream({
  to: 'ComputeProvider',
  ratePerHour: 10,
  deposit: 100, // Initial $100 deposit
  duration: 86400, // 24 hours
});

Conditional Escrows

// 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,
});

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    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 ││
│  └──────────────┴──────────────┴──────────────┴───────────────┘│
└─────────────────────────────────────────────────────────────────┘

Packages

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

Smart Contracts

AFP is implemented as four Solana programs written with Anchor:

Agent Registry

  • Agent registration with DIDs
  • Reputation scores (0-1000)
  • Trust tiers with transaction limits
  • Vouching system with stake

Payment Router

  • Direct transfers with protocol fees
  • Session key management
  • Agent wallets (PDAs)
  • Spending limit enforcement

Stream Factory

  • Continuous payment streams
  • Rate-per-second accounting
  • Cliff periods
  • Pause/resume/cancel

Escrow Factory

  • Conditional payments
  • Multiple condition types (signature, oracle, task completion)
  • Dispute resolution with arbiters
  • Automatic expiration handling

Trust Tiers

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

Examples

See the /examples directory:

  • autonomous-agent: Full autonomous agent implementation
  • claude-agent: Integration with Claude's function calling

Development

Prerequisites

  • Node.js 18+
  • pnpm 8+
  • Rust/Cargo (for Solana programs)
  • Anchor CLI 0.29+
  • Solana CLI 1.17+

Setup

# Install dependencies
pnpm install

# Build all packages
pnpm build

# Run tests
pnpm test

Deploy to Devnet

# Deploy Solana programs
cd contracts/solana
./scripts/deploy.sh devnet
./scripts/initialize.ts

Roadmap

Completed

  • Agent Registry program
  • Payment Router program
  • Stream Factory program
  • Escrow Factory program
  • Solana SDK package
  • Session key infrastructure
  • Example implementations

In Progress

  • Base/Arbitrum adapters
  • Credit pool program
  • Service marketplace

Planned

  • Python SDK
  • x402 protocol bridge
  • MCP server integration
  • Enterprise dashboard

Protocol Specification

For the complete AFP specification, see:

Contributing

We're building the financial infrastructure for the machine economy. Contributions welcome!

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

License

MIT


Built for the agent economy.

The machines are coming. Make sure they can pay.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors