Skip to content

LangChain with Keln

Run Keln models in LangChain using the langchain-keln package. It provides ChatKeln, which supports chains, agents, tool calling, and structured output.

Before you start

  • Python 3.10 or newer.
  • A Keln API key. Sign up at app.keln.ai, then go to Keys → Create key.

Connect LangChain to Keln

1. Install the package

pip install langchain-keln

2. Set your API key

ChatKeln reads your API key from the KELN_API_KEY environment variable.

export KELN_API_KEY=sk-keln-…
$env:KELN_API_KEY = "sk-keln-…"

Add credit under Billing → Add credit before making your first request. Keln returns a 402 error if your balance is empty.

3. Send a message

from langchain_keln import ChatKeln

model = ChatKeln(model="zai-org/glm-5.3")
reply = model.invoke("What is retrieval-augmented generation? Answer in 2 sentences.")
print(reply.content)

To stream responses, use model.stream(...) instead of model.invoke(...).

Tool calling

Use bind_tools(...) to give the model access to your tools. ChatKeln returns the tool calls in the response.

from langchain_core.tools import tool
from langchain_keln import ChatKeln


@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"It is 21°C and sunny in {city}."


model = ChatKeln(model="zai-org/glm-5.3").bind_tools([get_weather])
reply = model.invoke("What is the weather in Paris?")
print(reply.tool_calls)

ChatKeln also supports structured output with with_structured_output(...). For reasoning models, it preserves reasoning across turns in a tool-calling loop.

Reasoning

Control how much a model reasons with reasoning_effort, such as "low", "high", or "max".

model = ChatKeln(model="zai-org/glm-5.3", reasoning_effort="low")

Each model supports different reasoning levels, listed in GET /v1/models. If you don't set a level, the model uses its default. See Reasoning control.

Models

ChatKeln supports all Keln chat models. Pass a model ID such as moonshotai/kimi-k3 or deepseek-ai/deepseek-v4.1-flash. See Models & pricing for available models and pricing.

Usage

Track spending, token usage, latency, and error rates on your Keln Usage page. Keln stores request metadata only, not prompts or responses. See Zero data retention.

The Keln Usage page with sample data: spend, requests, tokens, time to first token, error rate and a spend-by-day chart

Where to go next

Try Keln

Create a Keln account and API key to run open models in LangChain.

Get an API key Models & pricing ↗