Use LLM batch APIs with your LangChain chains — 50% cost savings, zero code changes
Project description
50% cost savings on LLM APIs with zero code changes.
Quick Start • Features • Documentation • Providers • Contributing
langasync lets you use provider batch APIs (OpenAI, Anthropic) with your existing LangChain chains. Wrap your chain, submit inputs, get results at half the cost.
from langasync import batch_chain
# Your existing chain — no changes needed
chain = prompt | model | parser
# Wrap for batch processing
batch_wrapper = batch_chain(chain)
# Submit and retrieve results
job = await batch_wrapper.submit(inputs)
results = await job.get_results()
Why langasync?
Provider batch APIs offer 50% cost savings on tokens for workloads that can tolerate 24-hour turnaround. But they require completely different code patterns — file uploads, polling, result parsing.
langasync abstracts all of that:
| Without langasync | With langasync |
|---|---|
| Rewrite chains as batch requests | Same chain, just wrapped |
| Manage file uploads (OpenAI) | Automatic |
| Build custom polling logic | Built-in BatchPoller |
| Parse provider-specific responses | Unified BatchItem |
| Handle partial failures manually | Automatic success/error separation |
Quick Start
Installation
pip install langasync
Set your API key
export OPENAI_API_KEY=sk-...
# or for Anthropic:
export ANTHROPIC_API_KEY=sk-ant-...
Basic Usage
import asyncio
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langasync import batch_chain, BatchPoller, BatchStatus
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("user", "Explain {topic} in one paragraph.")
])
model = ChatOpenAI(model="gpt-4o-mini")
chain = prompt | model
batch_wrapper = batch_chain(chain)
async def main():
# Submit — returns immediately
job = await batch_wrapper.submit([
{"topic": "quantum computing"},
{"topic": "machine learning"},
{"topic": "blockchain"},
])
print(f"Batch submitted: {job.job_id}")
# Poll until complete — batch APIs typically take minutes to hours
poller = BatchPoller()
async for result in poller.wait_all():
if result.status_info.status == BatchStatus.COMPLETED:
for r in result.results:
print(r.content)
asyncio.run(main())
Features
Drop-in Batch Support
Wrap any LangChain chain with batch_chain(). Prompts, models, parsers — all work automatically.
chain = prompt | model | parser
batch_wrapper = batch_chain(chain)
Structured Output
Full support for Pydantic output parsers and schemas:
from pydantic import BaseModel
from langchain_core.output_parsers import PydanticOutputParser
class Analysis(BaseModel):
sentiment: str
confidence: float
parser = PydanticOutputParser(pydantic_object=Analysis)
chain = prompt | model | parser
Tool Calling
.bind_tools() works out of the box:
model = ChatOpenAI().bind_tools([my_tool])
chain = prompt | model
Multimodal (Images & PDFs)
Pass images and documents as part of your batch inputs:
from langchain_core.messages import HumanMessage, SystemMessage
batch_wrapper = batch_chain(model, settings)
await batch_wrapper.submit([
[
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content=[
{"type": "text", "text": "Describe this image."},
{"type": "image", "url": "https://example.com/photo.jpg"},
]),
],
])
Job Persistence
Batch jobs can take up to 24 hours. Job metadata persists automatically — resume after process restart:
from langasync import BatchPoller
# Later, in a new process
poller = BatchPoller()
async for result in poller.wait_all():
print(f"Job {result.job_id}: {result.status_info.status}")
Note: langasync persists job metadata (IDs, status) but not results. Save your results when you receive them — providers delete batch outputs after ~30 days.
Partial Failure Handling
Get successful results even when some requests fail:
result = await job.get_results()
for r in result.results:
if r.success:
print(r.content)
else:
print(f"Failed: {r.error}")
Supported Providers
| Provider | Status | Batch API | Savings |
|---|---|---|---|
| OpenAI | ✅ Supported | Batch API | 50% |
| Anthropic | ✅ Supported | Message Batches | 50% |
| Google Vertex AI | 🔜 Planned | — | — |
| Azure OpenAI | 🔜 Planned | — | — |
Documentation
API Reference
| Function / Class | Description |
|---|---|
batch_chain(chain) |
Wrap a LangChain chain for batch processing |
BatchPoller() |
Poll pending jobs and retrieve results |
LangasyncSettings |
Configuration via env vars or constructor |
BatchJobService |
Service layer — create(), get(), list() batch jobs |
BatchJobHandle |
Returned by submit() / create() — provides get_results(), cancel() |
BatchStatus |
Enum: PENDING, IN_PROGRESS, COMPLETED, FAILED, CANCELLED, EXPIRED |
Configuration
langasync reads configuration from environment variables or a .env file automatically:
# Provider API keys
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# Optional overrides
OPENAI_BASE_URL=https://api.openai.com/v1
ANTHROPIC_BASE_URL=https://api.anthropic.com
LANGASYNC_BATCH_POLL_INTERVAL=60.0
LANGASYNC_BASE_STORAGE_PATH=./langasync_jobs
Or configure programmatically:
from langasync import LangasyncSettings, batch_chain
settings = LangasyncSettings(
openai_api_key="sk-...",
batch_poll_interval=30.0,
base_storage_path="./my_jobs",
)
batch_wrapper = batch_chain(chain, settings)
Examples
See examples/ for complete working examples:
# Submit a batch job
python examples/openai_example.py run
# Fetch results (can run later, after restart)
python examples/openai_example.py fetch
Development
Setup
git clone https://github.com/langasync/langasync.git
cd langasync
pip install -e ".[dev]"
pre-commit install
Running Tests
# Unit tests (mocked, fast)
pytest tests
# Integration tests (requires API keys)
pytest tests/integration -o "addopts="
Contributing
Contributions are welcome!
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
pytest) - Submit a pull request
Community
- GitHub Issues — Bug reports and feature requests
- GitHub Discussions — Questions and ideas
License
langasync is licensed under Apache 2.0. Free to use for any purpose — personal projects, commercial products, production workloads.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langasync-0.1.6.tar.gz.
File metadata
- Download URL: langasync-0.1.6.tar.gz
- Upload date:
- Size: 167.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4bb410cac476728208bd119b83490b071d0d0b52213b3aea55fef88db755b3a
|
|
| MD5 |
dbfada1cea40a1544096a5bcf95dc644
|
|
| BLAKE2b-256 |
a4b7a44ac5bd27893998245e6611aa8f410f12e673b28d88e0b34613d0e91d89
|
Provenance
The following attestation bundles were made for langasync-0.1.6.tar.gz:
Publisher:
publish.yml on langasync/langasync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langasync-0.1.6.tar.gz -
Subject digest:
e4bb410cac476728208bd119b83490b071d0d0b52213b3aea55fef88db755b3a - Sigstore transparency entry: 932890220
- Sigstore integration time:
-
Permalink:
langasync/langasync@ac67a189379a4f96223a83b85cb6f56549801275 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/langasync
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ac67a189379a4f96223a83b85cb6f56549801275 -
Trigger Event:
release
-
Statement type:
File details
Details for the file langasync-0.1.6-py3-none-any.whl.
File metadata
- Download URL: langasync-0.1.6-py3-none-any.whl
- Upload date:
- Size: 31.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7ea3236aa1189e695c4e97a64eb77f07eb85da1a5882a5fc15eb70f314ab0f4
|
|
| MD5 |
3bbf8c2efbb179b5c65eabab5b412b44
|
|
| BLAKE2b-256 |
451c8446b95a50ad2f2e7f79c550ccf99186f61cddd6c566d1b10b8a2f791af0
|
Provenance
The following attestation bundles were made for langasync-0.1.6-py3-none-any.whl:
Publisher:
publish.yml on langasync/langasync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langasync-0.1.6-py3-none-any.whl -
Subject digest:
b7ea3236aa1189e695c4e97a64eb77f07eb85da1a5882a5fc15eb70f314ab0f4 - Sigstore transparency entry: 932890283
- Sigstore integration time:
-
Permalink:
langasync/langasync@ac67a189379a4f96223a83b85cb6f56549801275 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/langasync
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ac67a189379a4f96223a83b85cb6f56549801275 -
Trigger Event:
release
-
Statement type: