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

# LangChain

> Use Kyma API with LangChain for building AI applications, chains, and agents.

## Install

<CodeGroup>
  ```bash Python theme={null}
  pip install langchain-openai
  ```

  ```bash JavaScript theme={null}
  npm install @langchain/openai
  ```
</CodeGroup>

## Setup

<CodeGroup>
  ```python Python theme={null}
  from langchain_openai import ChatOpenAI

  llm = ChatOpenAI(
      base_url="https://kymaapi.com/v1",
      api_key="ky-your-api-key",
      model="qwen-3.6-plus",
  )
  ```

  ```typescript JavaScript theme={null}
  import { ChatOpenAI } from "@langchain/openai";

  const llm = new ChatOpenAI({
    configuration: { baseURL: "https://kymaapi.com/v1" },
    apiKey: "ky-your-api-key",
    model: "qwen-3.6-plus",
  });
  ```
</CodeGroup>

## Basic Usage

<CodeGroup>
  ```python Python theme={null}
  response = llm.invoke("Explain RAG in simple terms")
  print(response.content)
  ```

  ```typescript JavaScript theme={null}
  const response = await llm.invoke("Explain RAG in simple terms");
  console.log(response.content);
  ```
</CodeGroup>

## Streaming

<CodeGroup>
  ```python Python theme={null}
  for chunk in llm.stream("Write a short story"):
      print(chunk.content, end="", flush=True)
  ```

  ```typescript JavaScript theme={null}
  const stream = await llm.stream("Write a short story");
  for await (const chunk of stream) {
    process.stdout.write(chunk.content);
  }
  ```
</CodeGroup>

## With Prompt Template

<CodeGroup>
  ```python Python theme={null}
  from langchain_core.prompts import ChatPromptTemplate

  prompt = ChatPromptTemplate.from_messages([
      ("system", "You are a {role}."),
      ("user", "{question}")
  ])

  chain = prompt | llm

  response = chain.invoke({
      "role": "Python expert",
      "question": "How do decorators work?"
  })
  print(response.content)
  ```

  ```typescript JavaScript theme={null}
  import { ChatPromptTemplate } from "@langchain/core/prompts";

  const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are a {role}."],
    ["user", "{question}"],
  ]);

  const chain = prompt.pipe(llm);

  const response = await chain.invoke({
    role: "Python expert",
    question: "How do decorators work?",
  });
  console.log(response.content);
  ```
</CodeGroup>

## Tool Calling

<CodeGroup>
  ```python Python theme={null}
  from langchain_core.tools import tool

  @tool
  def get_weather(city: str) -> str:
      """Get weather for a city."""
      return f"Sunny, 72°F in {city}"

  llm_with_tools = llm.bind_tools([get_weather])
  response = llm_with_tools.invoke("What's the weather in Tokyo?")
  print(response.tool_calls)
  ```

  ```typescript JavaScript theme={null}
  import { tool } from "@langchain/core/tools";
  import { z } from "zod";

  const getWeather = tool(
    async ({ city }) => `Sunny, 72°F in ${city}`,
    { name: "get_weather", description: "Get weather for a city", schema: z.object({ city: z.string() }) }
  );

  const llmWithTools = llm.bindTools([getWeather]);
  const response = await llmWithTools.invoke("What's the weather in Tokyo?");
  console.log(response.tool_calls);
  ```
</CodeGroup>

## RAG Example

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(
    base_url="https://kymaapi.com/v1",
    api_key="ky-your-api-key",
    model="gemini-2.5-flash",  # 1M context for large documents
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "Answer based on the following context:\n\n{context}"),
    ("user", "{question}")
])

chain = prompt | llm

response = chain.invoke({
    "context": "Your retrieved documents here...",
    "question": "What is the main topic?"
})
```

## Recommended Models

| Use Case        | Model              | Why                   |
| --------------- | ------------------ | --------------------- |
| General chains  | `qwen-3.6-plus`    | Best overall quality  |
| RAG / long docs | `gemini-2.5-flash` | 1M context window     |
| Tool calling    | `kimi-k2.6`        | Best function calling |
| Fast responses  | `qwen-3-32b`       | Ultra-fast inference  |

<Tip>
  Kyma's API is OpenAI-compatible, so `langchain-openai` / `@langchain/openai` work directly. No special package needed.
</Tip>
