> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qwedai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain integration

> Use QWED with LangChain via QWEDTool for verified AI chains and agents. Includes verification callbacks for math, logic, and code validation in your workflows.

## Installation

```bash theme={null}
pip install qwed langchain
```

## Quick start

```python theme={null}
from qwed_sdk.langchain import QWEDTool, QWEDVerificationCallback

# Add QWED as a tool
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI

agent = initialize_agent(
    tools=[QWEDTool()],
    llm=OpenAI(),
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)

result = agent.run("Verify: Is 2+2 equal to 5?")
# Agent uses QWED tool to verify and corrects the answer
```

## Available components

### QWEDTool

General-purpose verification tool:

```python theme={null}
from qwed_sdk.langchain import QWEDTool

tool = QWEDTool(api_key="qwed_...")
print(tool.run("2+2=4"))
# ✅ VERIFIED: The statement is correct
```

### Specialized tools

```python theme={null}
from qwed_sdk.langchain import QWEDMathTool, QWEDLogicTool, QWEDCodeTool

tools = [
    QWEDMathTool(),   # Math expressions
    QWEDLogicTool(),  # QWED-Logic DSL
    QWEDCodeTool(),   # Code security
]
```

### QWEDVerificationCallback

Auto-verify all LLM outputs:

```python theme={null}
from langchain.chains import LLMChain
from qwed_sdk.langchain import QWEDVerificationCallback

callback = QWEDVerificationCallback(
    verify_math=True,
    verify_code=True,
    log_results=True,
)

chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    callbacks=[callback]
)

result = chain.run("Calculate 15% of 200")
# [QWED] ✅ MATH: verified=True
```

### QWEDVerifiedChain

Wrap any chain with verification:

```python theme={null}
from qwed_sdk.langchain import QWEDVerifiedChain

base_chain = LLMChain(llm=llm, prompt=prompt)
verified_chain = QWEDVerifiedChain(base_chain, auto_correct=True)

result = verified_chain.run("What is 2+2?")
print(result.output)    # "4"
print(result.verified)  # True
```

## LCEL integration

Use with LangChain Expression Language:

```python theme={null}
from langchain_core.runnables import RunnableLambda

def verify_output(text):
    from qwed_sdk import QWEDClient
    client = QWEDClient()
    result = client.verify(text)
    return text if result.verified else f"[UNVERIFIED] {text}"

chain = prompt | llm | RunnableLambda(verify_output)
```

## Best practices

1. **Use callbacks for monitoring** — Track verification rates
2. **Use tools for agent autonomy** — Let agents verify themselves
3. **Use wrappers for guarantees** — Ensure all outputs are verified
