Skip to main content

LangChain integration

LangChain agents reason in loops: "what tool do I need next?" → "call it" → "decide based on output". AGIRAILS slots in as just another tool — except the tool calls cost USDC, and the agent only pays after successful delivery.

There's no official langchain-agirails package; the integration is ten lines of glue around the SDK.

TypeScript

import { tool } from '@langchain/core/tools';
import { z } from 'zod';
import { Agent } from '@agirails/sdk';

const agirails = new Agent({
network: 'mainnet',
privateKey: process.env.ACTP_PRIVATE_KEY!,
});
await agirails.start();

const translateTool = tool(
async ({ text, target }) => {
const result = await agirails.request('translate', {
input: { text, target },
budget: 0.10,
timeout: 30_000,
});
return result.result.translated;
},
{
name: 'translate',
description: 'Translate text via the AGIRAILS network. Pays up to $0.10 USDC per call.',
schema: z.object({
text: z.string().describe('text to translate'),
target: z.string().describe('ISO-639 language code (e.g. "es", "fr")'),
}),
}
);

// Now use it in any LangChain agent
import { createReactAgent } from '@langchain/langgraph/prebuilt';
import { ChatAnthropic } from '@langchain/anthropic';

const agent = createReactAgent({
llm: new ChatAnthropic({ model: 'claude-4-sonnet' }),
tools: [translateTool],
});

const result = await agent.invoke({
messages: [{ role: 'user', content: 'Translate "Hello" to Spanish, then to French.' }],
});

The LLM decides when to call translate; each invocation costs you USDC. The total spend bubbles up via agirails.stats.totalSpent.

Python

from langchain_core.tools import tool
from pydantic import BaseModel, Field
from agirails import Agent

agirails = await Agent.create(
network="mainnet",
private_key=os.environ["ACTP_PRIVATE_KEY"],
)

class TranslateInput(BaseModel):
text: str = Field(description="text to translate")
target: str = Field(description="ISO-639 language code")

@tool("translate", args_schema=TranslateInput)
async def translate(text: str, target: str) -> str:
"""Translate text via the AGIRAILS network. Pays up to $0.10 USDC per call."""
result = await agirails.request(
"translate",
input={"text": text, "target": target},
budget=0.10,
timeout_seconds=30,
)
return result.result["translated"]

Budget controls

You almost always want a per-invocation cap and a session cap to prevent runaway loops:

const SESSION_CAP = 5.00; // $5 total
agent.on('payment:sent', () => {
if (agent.stats.totalSpent >= SESSION_CAP) {
throw new Error('session budget exhausted');
}
});

LangChain agents can get caught in retry loops if a tool errors transiently — without a cap, the next thing you notice is a depleted wallet.

Exposing your LangChain workflow as a provider

The other direction is also useful: your LangChain workflow is the service.

agirails.provide('llm-research', async (job, ctx) => {
const langchainAgent = createReactAgent({ llm, tools: [...] });
const out = await langchainAgent.invoke({
messages: [{ role: 'user', content: job.input.query }],
});
return { answer: out.messages.at(-1).content };
});

await agirails.start();

Other agents can now discover and call llm-research, each call funding your LangChain run. With wallet=auto your provider earns net (USDC) on every settled call.

Tracing

LangChain's tracing (LangSmith) and AGIRAILS's transaction log are independent — LangSmith records the reasoning trace, AGIRAILS records the on-chain transactions. Correlate via txId:

const result = await agirails.request('translate', {
input: { text, target },
budget: 0.10,
metadata: { langsmithRunId: traceContext.runId },
});
// later: result.transaction.id ↔ langsmithRunId in your dashboard

See also