Skip to main content

langchain-foliant

Listed in LangChain's integration directory (langchain-ai/docs#6306). Works against the Foliant reference node and, through foliant-protocol[chain], the contracts on Avalanche Fuji.

Foliant for LangChain: a payment tool for agents that pay x402 endpoints, and a middleware that enforces the agent's spending budget on every paid tool call. Built for the supervisor / subagents pattern: the supervisor holds the crew's budget, each subagent gets a delegated cap, and every spend is checked against every level.

pip install langchain-foliant   # pulls in foliant-protocol, the reference implementation

What it does

  • FoliantPaymentTool — calls an HTTP endpoint that charges per request over x402. On a 402 it opens a payment channel to, or joins the pool of, the provider, then signs one off-chain update per call. Thousands of calls settle in one on-chain transaction. If the agent's policy does not allow the payment the tool returns BUDGET REFUSED: … to the model rather than raising.
  • FoliantBudgetMiddleware — wraps tool calls; before a paid call it checks that the agent can still pay (policy headroom plus unspent balance in open channels or pools) and returns a clear tool message, or stops the run, if not.
  • FoliantCrew — maps a supervisor and its subagents onto a Foliant budget tree. worker(name, policy, fund=…) delegates a child account with a policy no wider than the supervisor's and returns the tool and middleware for that subagent; revoke(name) expires the worker and recalls its balance with the supervisor's key alone.

The wire format is unchanged x402 (X-PAYMENT, X-PAYMENT-RESPONSE, the 402 accepts body), so this sits beside langchain-x402 and per-call wallets rather than replacing them.

Example

import httpx
from foliant import Agent, KeyPair, Ledger, Policy
from langchain.agents import create_agent
from langchain_foliant import FoliantCrew

ledger = Ledger()                       # the reference in-memory ledger; contracts on Base/Avalanche are the next phase
http = httpx.Client()

supervisor = Agent(ledger, KeyPair(), KeyPair(),
                   Policy(per_tx_max=500, per_window_max=150, window_secs=3600))
crew = FoliantCrew(supervisor, http)

worker_policy = Policy(per_tx_max=100, per_window_max=100, window_secs=3600)
research = crew.worker("research", worker_policy, fund=1_000)
writer = crew.worker("writer", worker_policy, fund=1_000)

research_agent = create_agent("openai:gpt-5.5", tools=[research.tool], middleware=[research.middleware])
writer_agent = create_agent("openai:gpt-5.5", tools=[writer.tool], middleware=[writer.middleware])

# Each worker may commit up to 100 in the hour; the crew as a whole stops at 150,
# so the third worker's deposit is refused even though its own cap has room.
# crew.committed() -> {"research": 100, "writer": 50, "crew": 150}
# crew.revoke("writer") -> expires the writer's policy and recalls its unspent balance.

Refund and settlement semantics

Deposits into a channel or pool are the committed value the policy sees; per-call updates are bounded by the deposit. The provider settles at its own cadence with one transaction per channel or per pool. A payer recovers its unspent deposit by closing a channel or exiting a pool unilaterally after the timeout; a provider that goes silent cannot hold a payer's funds. Receipts are provider-signed, one per call, and are available on the tool as tool.receipts.

Status

The tool and middleware run against the Foliant Python reference implementation (an in-process ledger) today, which is what the tests exercise. Solidity contracts and a registered x402 scheme on Base and Avalanche are the next phase; the tool's interface does not change when they land.

Tests

pip install -e ".[test]"
pytest

Includes LangChain's standard tool tests (langchain-tests) and behaviour tests for payment, refusal, middleware inside create_agent, and the crew.

Licence

Apache-2.0. Copyright 2026 Machine Quotient Ltd.

Metadata

Release files for langchain-foliant 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-foliant 0.1.2
File Size Uploaded Provenance Actions
langchain_foliant-0.1.2.tar.gz 7.6 kB Without provenance Details for langchain_foliant-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-foliant 0.1.2
File Interpreter ABI Platform Actions
langchain_foliant-0.1.2-py3-none-any.whl Python 3 none any Details for langchain_foliant-0.1.2-py3-none-any.whl

Total release size: 16.4 kB

Release files / langchain_foliant-0.1.2.tar.gz

Download URL langchain_foliant-0.1.2.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
25c10680e99b7191cfda8bc776f7c62bc03c6fbb23273bcd4be0ea0b70f11103
BLAKE2b-256 checksum
How to use checksums
63456256e9ae3c044d4e2add1c13295e45de076ae7c7d12fa102b3f92a2e3ef1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / langchain_foliant-0.1.2-py3-none-any.whl

Download URL langchain_foliant-0.1.2-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3f600bef298a9adb92b013a75cfe5f954389a516d9f035756047668b0e32755
BLAKE2b-256 checksum
How to use checksums
2559701705071d949906e06d1969d5f67fdad2447360fa5c619d20462f50e4a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for langchain-foliant 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page