DocsLangChain

Cleave for LangChain

langchain-cleave gives a LangChain agent Cleave's four agent tools: plan a swap, track it, find a token and get gas.

Install

pip install -U langchain-cleave

Python 3.10 or later. No API key is needed.

Example

This example also needs langchain and a chat model package:

pip install -U langchain-cleave langchain "langchain[anthropic]"
from langchain.agents import create_agent
from langchain_cleave import CleaveToolkit

agent = create_agent(
    model="claude-sonnet-4-6",
    tools=CleaveToolkit().get_tools(),
)

agent.invoke(
    {
        "messages": [
            {
                "role": "user",
                "content": (
                    "Plan a swap of 25 USDC to WETH on Base for wallet "
                    "0x00000000000000000000000000000000000A11cE"
                ),
            }
        ]
    }
)

The four tools

The tools have the same names, inputs and answers as the tools of the MCP server, and call the keyless Agent REST API. CleaveToolkit().get_tools() returns all four.

ToolClassWhat it does
cleave_plan_swapCleavePlanSwapPlans a swap on one chain, a cross-chain swap or a bridge, and returns unsigned calls in signing order
cleave_track_swapCleaveTrackSwapReports the state of a sent plan or a gas order, and what arrived
cleave_find_tokenCleaveFindTokenFinds a token by symbol or address and flags look-alikes
cleave_get_gasCleaveGetGasOffers native gas for USDC; with the relay turned on, passes the order the wallet signed to Cleave's relayer

What the tools return

  • cleave_plan_swap returns a plan whose calls are unsigned and in signing order. The user's own wallet signs and sends them.
  • The tools never take a private key. They sign nothing; the only thing they send is a gas order the wallet has signed, and only when the gas relay is turned on.
  • Read a plan in this order: blocked and notices, then requiresConfirmation, then route.outcomeKind and receive, then fees, then validUntil.
  • fees lists costs charged by others, such as a bridge fee. Cleave's fees are included in the price.

The gas relay is off by default

  • cleave_get_gas returns offers. A call with offer or signature comes back as a refusal with code relay_disabled, without a request.
  • Turn the relay on in code, not from the agent, with include_gas_relay=True:
from langchain_cleave import CleaveGetGas, CleaveToolkit

tools = CleaveToolkit(include_gas_relay=True).get_tools()
gas = CleaveGetGas(allow_relay=True)

With the relay on, HumanInTheLoopMiddleware(interrupt_on={"cleave_get_gas": True}) pauses the agent so a person approves each signed order before it is sent; the agent needs a checkpointer. An authorization that is never submitted never debits the wallet.

Before signing

  • Do not act on a blocked plan.
  • Before any wallet signs, show the user the warnings, the recipient, the amounts and the slippage, and get their confirmation. Do not raise slippage above the default 50 bps without the user's consent.
  • Never change the recipient or raise slippage to get past a refusal or a warning.
  • blocked, requiresConfirmation and the notices are advisory. They bind only if your application, or the signer it uses, enforces them.

Track with cleave_track_swap before sending a plan again: a plan still in flight that is sent twice can pay twice.

Server side only

Call the API from a server, bot or agent process. The Agent REST API refuses requests that carry an Origin header, as browsers send, with 403 and code forbidden_origin.

Terms of use

Use of the API, the partner API and the MCP server is subject to the Terms of Use and the Privacy Policy. By sending requests or connecting a client after this notice, you accept them.

If an agent or other software calls Cleave, its operator accepts them and is responsible for what it does. Partner and agent keys are issued only after the key holder accepts them. Stay within the published rate limits.