Skip to main content

langchaint

langchaint is a provider-neutral Python client for LLM applications. It provides fully typed, asynchronous APIs for generation, streaming, embeddings, tools, retries, and billing. The application owns the agent loop.

Alpha: the API may change without notice.

Why langchaint

  • Consistent API. Bind request fields once with LLM.bind(), then call generate_one(), generate_many(), or stream_one() on the resulting BoundLLM.
  • Useful type annotations. For example, llm.bind(response_format=Answer) types generation.output as Answer.
  • Outcome variants with autocomplete. Match on .kind with editor autocomplete and no class imports.
  • Coordinated retries. Share concurrency limits, request-start pacing, and provider-directed pauses across models using one rate-limit quota.
  • Complete billing. Generation and GenerationError values retain provider-reported usage from every recorded request, including billed retries.
  • Streaming. stream_one() returns an async context manager and async iterator. final() returns the typed generation with its usage.
  • Agent loops in Python. Provider-neutral messages, typed tools with argument validation, concurrent dispatch, and explicit outcome variants support async control flow.

Install

langchaint requires Python 3.13 or newer.

Install the extra for each backend you use:

pip install "langchaint[openai]"
Backend Class Install
Anthropic Anthropic langchaint[anthropic]
Anthropic on Amazon Bedrock AnthropicBedrock langchaint[anthropic-bedrock]
Cohere embeddings on Amazon Bedrock CohereBedrock langchaint[cohere-bedrock]
DeepSeek DeepSeek langchaint[deepseek]
Gemini Gemini langchaint[gemini]
OpenAI OpenAI langchaint[openai]
OpenAI embeddings OpenAI langchaint[openai-embedding]
OpenAI on Amazon Bedrock OpenAIBedrock langchaint[openai-bedrock]

Install langchaint[tracing] for OpenTelemetry tracing.

Generate a typed output

import asyncio

from pydantic import BaseModel

from langchaint.openai import OpenAI


class Answer(BaseModel):
    answer: str
    confidence: float


async def main() -> None:
    assistant = (
        OpenAI()
        .llm("gpt-5.6-terra")
        .bind(
            system_prompt="Answer clearly and concisely.",
            response_format=Answer,
        )
    )
    generation = await assistant.generate_one("Why is the sky blue?")

    print(generation.output.answer)
    print(generation.usage.cost_in_usd)


asyncio.run(main())

The Pydantic model validates the provider response.

generate_many() returns generations in input order, or raises GenerationError after cancelling and awaiting unfinished inputs. generate_many_records() returns one normalized record per input, including generation failures. Pass resume_path to persist records and resume interrupted batches.

Coordinate retries across a rate-limit quota

Create one SharedBackoff for each rate-limit quota, and pass it to every backend that sends requests against that quota. Models from one OpenAI share its SharedBackoff:

from langchaint import SharedBackoff

openai = OpenAI(
    shared_backoff=SharedBackoff(max_concurrent_requests=8, max_request_starts_per_second=50.0),
)

fast_model = openai.llm("gpt-5.6-luna")
strong_model = openai.llm("gpt-5.6-sol")

A rate-limit response pauses request starts across the shared quota. After a transient failure local to one request, langchaint waits and retries that request.

Stream with an explicit lifetime

text_assistant = OpenAI().llm("gpt-5.6-terra").bind()

async with text_assistant.stream_one("Explain photosynthesis.") as stream:
    async for item in stream:
        # Without tools, every item is str | ReasoningDelta
        if isinstance(item, str):
            print(item, end="", flush=True)

    generation = await stream.final()

final() consumes the remaining stream and returns the assembled generation.

Build agent loops

The application controls turn limits, state, approvals, model changes, and persistence.

messages: list[Message] = [UserMessage(content=prompt)]

for _ in range(max_turns):
    generation = await bound.generate_one(messages)

    match generation.kind:
        case "tool_call":
            messages.append(generation.assistant_message)
            outcomes = await bound.tool_manager.dispatch_many(generation.tool_calls)
            messages.extend(outcome.tool_message for outcome in outcomes)
        case "plain":
            return generation.output

raise RuntimeError("model did not finish within max_turns")

ToolManager.dispatch_many() runs tool calls concurrently and preserves their order.

See examples/02_tool_loop.py for a complete typed tool loop.

Account for every request

generation.usage.cost_in_usd includes every billed retry recorded for the input. GenerationError.usage preserves the recorded cost of a failed input.

More examples

See examples/README.md for complete examples.

License

MIT License

Metadata

Release files for langchaint 0.32.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 langchaint 0.32.0
File Size Uploaded Provenance Actions
langchaint-0.32.0.tar.gz 180.3 kB Without provenance Details for langchaint-0.32.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchaint 0.32.0
File Interpreter ABI Platform Actions
langchaint-0.32.0-py3-none-any.whl Python 3 none any Details for langchaint-0.32.0-py3-none-any.whl

Total release size: 381.0 kB

Release files / langchaint-0.32.0.tar.gz

Download URL langchaint-0.32.0.tar.gz
Size 180.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d1b772971c1883004817486dda8848cd91f22e8f90c8228511b4df95ce40a903
BLAKE2b-256 checksum
How to use checksums
d1b44ebd63a62192b45bc925ebb0a0135dc075be8f8a4ecc6e73d78fb57e91c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / langchaint-0.32.0-py3-none-any.whl

Download URL langchaint-0.32.0-py3-none-any.whl
Size 200.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1e64fc24a676f6c0df38313d67e0d2e8bdae5c3f97186fb154858c84e4e7d6a
BLAKE2b-256 checksum
How to use checksums
06f17056213db7dd36a1747786ac5279c7599f34df2751b693b70faeca57d3d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for langchaint 0.32.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.

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.

Release history Release notifications | RSS feed

This release

0.32.0 This release

2 release files No 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