Metadata-Version: 2.5
Name: langchain-levanto
Version: 0.1.0
Summary: Levanto for LangChain: Sage decisions and Levanto's LLMs.
Project-URL: Homepage, https://docs.levanto.ai/integrations/langchain
Project-URL: Repository, https://github.com/levantolabs/langchain-levanto
Project-URL: Changelog, https://github.com/levantolabs/langchain-levanto/blob/main/python/CHANGELOG.md
Author-email: Levanto <marco@levanto.ai>
License-Expression: MIT
License-File: LICENSE
Keywords: decision model,langchain,levanto,llm,router,sage
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: <4.0,>=3.11
Requires-Dist: httpx<1.0,>=0.27
Requires-Dist: langchain-core<2.0.0,>=1.8.0
Requires-Dist: langchain-openai<2.0.0,>=1.1.0
Description-Content-Type: text/markdown

# langchain-levanto

Levanto for LangChain: Sage decisions and Levanto's LLMs.

```bash
pip install langchain-levanto
```

It reads your key from `LEVANTO_API_KEY`.

## Decisions

`LevantoDecisionModel` is a LangChain decision model. It answers yes/no (`Noul`), `Choice` and `Score` questions about text, JSON or chat messages, with full probabilities.

```python
from langchain_core.decisions import Choice, Noul, Score
from langchain_levanto import LevantoDecisionModel

sage = LevantoDecisionModel()
response = sage.invoke({
    "state": "I was charged twice. Please refund the duplicate today.",
    "questions": {
        "refund": Noul(instructions="Is the customer asking for money back?"),
        "team": Choice(
            instructions="Which team should handle this?",
            criteria={"billing": "Charges and refunds", "technical": "Bugs and outages"},
        ),
        "urgency": Score(instructions="How urgent is it?", criteria=["Can wait", "This week", "Today"]),
    },
})

response.nouls["refund"].noul      # 0.996
response.choices["team"].choice    # "billing"
response.scores["urgency"].score   # 1.99, from 0 to 2
```

Options: `reasoning` (`"off"`, `"auto"`, `"on"`), `grounding`, `timeout` (15 s by default, 60 s with reasoning), `max_retries`. `Box` questions and images aren't supported; for images, call [`POST /decide`](https://docs.levanto.ai/decision-model/quickstart).

## Guard and route agents

LangChain's agent middleware takes any decision model.

```python
from langchain.agents import create_agent
from langchain.agents.middleware import AutoModeMiddleware

# Sage blocks risky tool calls the user didn't ask for.
agent = create_agent(
    model,
    tools=[read_file, delete_file],
    middleware=[AutoModeMiddleware(tools=[delete_file], classifier=LevantoDecisionModel())],
)
```

`ModelRouterMiddleware(choices=..., instructions=..., classifier=LevantoDecisionModel())` picks a model per run.

## LLMs

```python
from langchain_levanto import ChatLevanto

llm = ChatLevanto(target="economy")   # model="auto" lets the router pick
reply = llm.invoke("Summarize this ticket in one sentence: ...")
reply.response_metadata["model_name"]  # the model that answered
```

`ChatLevanto` is `ChatOpenAI` pointed at `https://api.levanto.ai/v1`: streaming, tools and `with_structured_output` work as usual. It never reads `OPENAI_API_KEY`.

## Development

```bash
uv venv && uv pip install -e . --group test
pytest tests/unit_tests
LEVANTO_API_KEY=... pytest -m live tests/integration_tests
```

Docs: https://docs.levanto.ai/integrations/langchain
