Skip to main content

langchain-leanroute

LangChain integration for Leanroute, the agent-native AI gateway.

ChatLeanroute is LangChain's ChatOpenAI pointed at Leanroute: one key for 100+ models across 14 providers (OpenAI, Anthropic, Google, xAI, DeepSeek, Qwen and more), with failover, caching, spend caps, MCP tools on any model, and cost per agent run. Tools, structured output, streaming, async and LangGraph work unchanged.

Install

pip install langchain-leanroute

Create a runtime key (gw_live_*) at leanroute.dev/dashboard/keys and set it:

export LEANROUTE_API_KEY=gw_live_...

New accounts get 100 free requests on low-cost models, no card needed.

Use

from langchain_leanroute import ChatLeanroute

llm = ChatLeanroute(model="deepseek/deepseek-v4-1-flash")
print(llm.invoke("Explain retrieval-augmented generation in one sentence.").content)

Model ids use the provider/model form, e.g. anthropic/claude-sonnet-5 or openai/gpt-5.6-terra. Full list: leanroute.dev/docs/models.

Agent runs

Name your agent and give each run an id, and Leanroute groups the calls into runs on the Agent runs page: cost per run, models used, and every tool called.

from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from langchain_leanroute import ChatLeanroute, new_run_id


@tool
def get_weather(city: str) -> str:
    """Return the current temperature for a city."""
    return f"{city}: 22°C"


def run(task: str) -> str:
    llm = ChatLeanroute(
        model="anthropic/claude-sonnet-5",
        agent_name="weather-agent",
        session_id=new_run_id(),  # one per run
    )
    agent = create_react_agent(llm, tools=[get_weather])
    result = agent.invoke({"messages": [("user", task)]})
    return result["messages"][-1].content


print(run("What's the weather in Singapore?"))

agent_name is sent as X-Agent-Name (without it, the API key's name is used) and session_id as X-Agent-Session-Id. Headers are fixed per instance, so create one ChatLeanroute per run. Every request also carries X-Leanroute-Client: langchain-leanroute/<version>.

Options

Argument Default What it does
model required provider/model id
api_key LEANROUTE_API_KEY Leanroute runtime key
base_url LEANROUTE_BASE_URL, else Leanroute Override for staging or self-hosted
agent_name none X-Agent-Name
session_id none X-Agent-Session-Id; use new_run_id()
agent_id none X-Agent-Id, a subagent within the run
explicit_routing False Sends x-gateway-routing: explicit: no model swaps, no failover

Every other ChatOpenAI argument (temperature, max_tokens, timeout, max_retries, ...) passes through.

License

Apache-2.0. See LICENSE.

Metadata

Release files for langchain-leanroute 0.1.0

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-leanroute 0.1.0
File Size Uploaded Provenance Actions
langchain_leanroute-0.1.0.tar.gz 5.4 kB With provenance Details for langchain_leanroute-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-leanroute 0.1.0
File Interpreter ABI Platform Actions
langchain_leanroute-0.1.0-py3-none-any.whl Python 3 none any Details for langchain_leanroute-0.1.0-py3-none-any.whl

Total release size: 11.7 kB

Release files / langchain_leanroute-0.1.0.tar.gz

Download URL langchain_leanroute-0.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
efc56264638ec53a8bfac3a6ea332dde1ff8d21772560b617a1a9415b2b814f0
BLAKE2b-256 checksum
How to use checksums
02d57a67d914a440a7d779b82341de5fff49604477be38702aee8687c0e61e6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / langchain_leanroute-0.1.0-py3-none-any.whl

Download URL langchain_leanroute-0.1.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b71c9b29ef4fe1d1d517a4ab98c118447e1d9f8e7a37dacce18dd008a8adb7fb
BLAKE2b-256 checksum
How to use checksums
4da3b92b145d648c9febe506bd072a9217ee3fae9c1cda58201b77258976ace2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for langchain-leanroute 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for langchain_leanroute-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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