---
title: "LangChain OpenAI compatible API for pentest models"
canonical: https://pentestllm.ai/guides/langchain
generated: 2026-11-18T03:35:58.068Z
source: https://pentestllm.ai/guides/langchain
base_url: https://api.pentestllm.ai/v1
---

# LangChain on an OpenAI compatible API, for security work

Python ChatOpenAI(base_url, api_key) or JavaScript configuration.baseURL, an uncensored build id, and the create_agent tool loop on it: usage, cost, errors.

Steps checked November 2, 2026 · prices as of November 18, 2026

A code-review agent that stops at the first exploit-shaped question is useless on an engagement. LangChain’s `ChatOpenAI` accepts a base URL and a key, so the chains and agents you already have can run on an uncensored build instead: one constructor in Python or JavaScript, an id from the [models page](https://pentestllm.ai/models), and `create_agent` drives its tool loop on it.

## What you need

Nothing to click through: the endpoint and the key go into the constructor of LangChain’s OpenAI chat class. In Python that is `ChatOpenAI` from `langchain-openai` with `base_url` and `api_key`; in JavaScript it is `ChatOpenAI` from `@langchain/openai` with `apiKey` plus `configuration.baseURL`. `init_chat_model` and `initChatModel` accept the same pair alongside `model_provider="openai"`.

Agents take that same instance. In LangChain 1.x the factory is `create_agent` (`createAgent` in JavaScript); LangGraph’s older `create_react_agent` now forwards to it. Either way you configure the endpoint on the model object once and every chain and agent inherits it.

Want to see a build answer before wiring anything? The prompt box on the home page gives a few free turns, and the [playground](https://pentestllm.ai/playground) runs any build in the browser with a key.

## Shortest path to a first reply

1. Install the chat model package. Add the agent package too if you want the tool loop:

```bash
pip install -U langchain langchain-openai
```

```bash
npm install langchain @langchain/openai @langchain/core zod
```

1. Make a key in the [console](https://app.pentestllm.ai/console) and export it. Every snippet reads the variable, so the key is never written into a file that ends up in a repo.

```bash
export PENTESTLLM_API_KEY="sk-ptl-live-…"
```

1. Python: construct `ChatOpenAI` with three things, the id, the key and this base URL; `invoke` gives back an `AIMessage` whose `usage_metadata` holds the billed token counts; `stream` hands over chunks as they are generated. The ids used here (glm-5.3-uncensored, glm-5.3-flash-uncensored, deepseek-v4.1-flash-abliterated) are the three LangChain callers pick most on this API; swap in any id from the [models page](https://pentestllm.ai/models) the same way.

```
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="glm-5.3-uncensored",
    api_key=os.environ["PENTESTLLM_API_KEY"],
    base_url="https://api.pentestllm.ai/v1",
    max_tokens=1024,
    stream_usage=True,  # token counts on streamed answers too; off by default once base_url is set
)

response = model.invoke("Explain, in three sentences, how SSRF reaches a cloud metadata endpoint.")
print(response.content)
print(response.usage_metadata)  # {'input_tokens': …, 'output_tokens': …, 'total_tokens': …}

for chunk in model.stream("List the response headers worth checking on an in-scope web target after recon."):
    print(chunk.content, end="", flush=True)
```

`init_chat_model` produces the same object from a provider string, handy when the build comes from config:

```
from langchain.chat_models import init_chat_model

model = init_chat_model(
    model="glm-5.3-flash-uncensored",
    model_provider="openai",
    base_url="https://api.pentestllm.ai/v1",
    api_key=os.environ["PENTESTLLM_API_KEY"],
)
```

You can also leave both values to the environment. LangChain reads `api_key` from `OPENAI_API_KEY` and `base_url` from `OPENAI_API_BASE`, then `OPENAI_BASE_URL`; once those are exported, `ChatOpenAI(model="…")` on its own hits this API. If the same process also calls OpenAI, keep the explicit arguments, or both clients end up on one base URL.

```bash
export OPENAI_API_KEY="sk-ptl-live-…"
export OPENAI_BASE_URL="https://api.pentestllm.ai/v1"
```

1. JavaScript: identical three values, except the URL sits under `configuration.baseURL`. That `configuration` object is forwarded to the OpenAI SDK client, so any other client option belongs there as well.

```
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "glm-5.3-uncensored",
  apiKey: process.env.PENTESTLLM_API_KEY,
  configuration: { baseURL: "https://api.pentestllm.ai/v1" },
  maxTokens: 1024,
});

const response = await model.invoke("Explain, in three sentences, how SSRF reaches a cloud metadata endpoint.");
console.log(response.content);
console.log(response.usage_metadata); // { input_tokens, output_tokens, total_tokens }

const stream = await model.stream("List the response headers worth checking on an in-scope web target after recon.");
for await (const chunk of stream) process.stdout.write(String(chunk.content));
```

1. Agent: pass the instance and your tools to `create_agent`. The loop asks the model, executes the tool it requested, appends the output and asks again until the model replies in plain text. The example is a code reviewer for an authorized engagement that reads the file before it judges it. Use an id whose model page shows tool calls (the table further down says which).

```
import os
from langchain.agents import create_agent
from langchain.tools import tool
from langchain_openai import ChatOpenAI

@tool
def read_file(path: str) -> str:
    """Read a UTF-8 text file and return its contents."""
    with open(path, encoding="utf-8") as f:
        return f.read()

model = ChatOpenAI(
    model="glm-5.3-uncensored",
    api_key=os.environ["PENTESTLLM_API_KEY"],
    base_url="https://api.pentestllm.ai/v1",
)

agent = create_agent(
    model=model,
    tools=[read_file],
    system_prompt="You review Python code for vulnerabilities on an authorized engagement. Read a file before you comment on it.",
)

result = agent.invoke({"messages": [{"role": "user", "content": "Review ./app.py for injection and auth flaws."}]})
print(result["messages"][-1].content)
```

Existing code on LangGraph’s `create_react_agent` runs unchanged with the same `model` and `tools` (there the system prompt is the `prompt` argument); its deprecation message points at the import used above. In JavaScript the agent looks the same:

```
import { readFile } from "node:fs/promises";
import { ChatOpenAI } from "@langchain/openai";
import { createAgent, tool } from "langchain";
import * as z from "zod";

const readTextFile = tool(async ({ path }) => readFile(path, "utf8"), {
  name: "read_file",
  description: "Read a UTF-8 text file and return its contents.",
  schema: z.object({ path: z.string() }),
});

const model = new ChatOpenAI({
  model: "glm-5.3-uncensored",
  apiKey: process.env.PENTESTLLM_API_KEY,
  configuration: { baseURL: "https://api.pentestllm.ai/v1" },
});

const agent = createAgent({
  model,
  tools: [readTextFile],
  systemPrompt: "You review TypeScript code for vulnerabilities on an authorized engagement. Read a file before you comment on it.",
});

const result = await agent.invoke({ messages: [{ role: "user", content: "Review ./app.ts for injection and auth flaws." }] });
console.log(result.messages.at(-1)?.content);
```

## Each setting, explained

- model: glm-5.3-uncensored the id exactly as the models page writes it; sent as each request’s model field
- api_key / apiKey: os.environ["PENTESTLLM_API_KEY"] the full sk-ptl- key, sent as Authorization: Bearer; when unset, Python reads OPENAI_API_KEY instead
- base_url / configuration.baseURL: https://api.pentestllm.ai/v1 the request target; the client adds /chat/completions, which is why the trailing /v1 is required
- max_tokens / maxTokens: 1024 ceiling on output tokens per reply; each model page states the build’s maximum
- stream_usage: True Python only: requests the usage chunk on a stream. LangChain disables it whenever base_url is set; this API returns it, so re-enable it. JavaScript has it on by default as streamUsage
- streaming: False when True, invoke streams under the hood and assembles the reply; stream() always streams
- temperature, timeout, max_retries: optional sampling temperature, per-request timeout, and the number of automatic retries after a failed call
- tools / system_prompt: create_agent which functions the agent is allowed to call, and the message placed first in every call; in createAgent the field is systemPrompt

## Which build to put in model

Requests leave `ChatOpenAI` in OpenAI format, which each build on the models page understands, so for plain `invoke` and `stream` any id is fine. The agent loop needs tool calls; so does `bind_tools`, and so does `with_structured_output` in its default method, since that is function calling underneath. Late in a review session a single turn carries the whole conversation, every file read and every tool result, so the input side dominates the bill and the build with the lowest input price has the lowest cost per turn.

| Model | Cost per turn | Takes tools |
| --- | --- | --- |
| [GLM-5.3 Uncensored](https://pentestllm.ai/models/glm-5.3-uncensored) | $0.01 | [see model page](https://pentestllm.ai/models/glm-5.3-uncensored) |
| [GLM-5.3 Flash Uncensored](https://pentestllm.ai/models/glm-5.3-flash-uncensored) | $0.0047 | [see model page](https://pentestllm.ai/models/glm-5.3-flash-uncensored) |
| [DeepSeek V4.1 Flash Abliterated](https://pentestllm.ai/models/deepseek-v4.1-flash-abliterated) | $0.04 | [see model page](https://pentestllm.ai/models/deepseek-v4.1-flash-abliterated) |
| [DeepSeek V4 Pro Uncensored](https://pentestllm.ai/models/deepseek-v4-pro-uncensored) | $0.04 | [see model page](https://pentestllm.ai/models/deepseek-v4-pro-uncensored) |
| [GLM-5.3 Abliterated](https://pentestllm.ai/models/glm-5.3-abliterated) | $0.09 | [see model page](https://pentestllm.ai/models/glm-5.3-abliterated) |
| [GLM-5.3 Flash Code Uncensored](https://pentestllm.ai/models/glm-5.3-flash-code-uncensored) | $0.25 | [see model page](https://pentestllm.ai/models/glm-5.3-flash-code-uncensored) |

A turn of 40,000 input and 1,200 output tokens at the catalog prices of 2026-11-18. The tool column is what the catalog publishes; where it publishes nothing, the model page decides.

| Model | Build | Input $/M | Output $/M |
| --- | --- | --- | --- |
| [GLM-5.3 Uncensored](https://pentestllm.ai/models/glm-5.3-uncensored) glm-5.3-uncensored | Uncensored | $0.282 | $0.846 |
| [GLM-5.3 Flash Uncensored](https://pentestllm.ai/models/glm-5.3-flash-uncensored) glm-5.3-flash-uncensored | Uncensored | $0.1053 | $0.3685 |
| [DeepSeek V4.1 Flash Abliterated](https://pentestllm.ai/models/deepseek-v4.1-flash-abliterated) deepseek-v4.1-flash-abliterated | Abliterated | $0.8488 | $1.6975 |
| [DeepSeek V4 Pro Uncensored](https://pentestllm.ai/models/deepseek-v4-pro-uncensored) deepseek-v4-pro-uncensored | Uncensored | $0.97 | $1.94 |
| [GLM-5.3 Abliterated](https://pentestllm.ai/models/glm-5.3-abliterated) glm-5.3-abliterated | Abliterated | $2.1825 | $4.365 |
| [GLM-5.3 Flash Code Uncensored](https://pentestllm.ai/models/glm-5.3-flash-code-uncensored) glm-5.3-flash-code-uncensored | Uncensored | $6.00 | $6.00 |

Prices per million tokens, read from the live catalog on 2026-11-18.

For agents that write and fix code, [the uncensored coding AI guide](https://pentestllm.ai/guides/uncensored-coding-ai) sets the code builds side by side and prices a full day on each. The same ids go into [Cursor](https://pentestllm.ai/guides/cursor), [OpenCode](https://pentestllm.ai/guides/opencode) and [Claude Code](https://pentestllm.ai/guides/claude-code), and the [refusals page](https://pentestllm.ai/claude-code-refuses-security-research) explains why testers move an agent off the stock model in the first place.

## Prove the key works

Ask the API which ids your key may call; the one you set in `model` has to appear:

```bash
curl https://api.pentestllm.ai/v1/models -H "Authorization: Bearer sk-ptl-live-…"
```

Next, make one request shaped like the ones `ChatOpenAI` sends. With `-i` the headers print too: one names the request, and on a non-streamed reply another states the charge in USD.

```bash
curl -i https://api.pentestllm.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-ptl-live-…" \
  -H "Content-Type: application/json" \
  -d '{"model": "glm-5.3-uncensored", "messages": [{"role": "user", "content": "Reply with the single word ok."}]}'
```

Now execute either snippet above. `usage_metadata` on the reply carries the billed input and output tokens, and the console’s usage page shows the same request with its charge. On a stream the counts ride on the final chunk, and in Python only when `stream_usage=True`.

## When it fails

- `401 invalid_api_key`: a wrong or revoked key, or `PENTESTLLM_API_KEY` is missing from the shell that launched the script. Re-export it or recopy it from the console. If the 401 body talks about OpenAI, `base_url` never took effect and your key went to OpenAI.
- `404 model_not_found`: the `model` value is not an id on the models page; compare the spelling. `404 route_not_found`: the request landed on a path this API does not serve. Either the base URL lacks `/v1`, so the client posted `/chat/completions` at the root, or it went to `/responses` because you set `use_responses_api=True`, or enabled `reasoning` or a built-in tool, both Responses-only. Only `/v1/chat/completions` is served here; leave those off.
- `402 insufficient_balance`: too little credit for the request; add some in the console. `402 monthly_limit_reached`: the monthly cap you set on the key is used up.
- `403 scope_denied`: the key was restricted to a set of models that does not include this one.
- `429 rate_limited`: the key’s rate limit tripped. `retry-after` says how long; with `max_retries` set the client waits and retries by itself.
- `usage_metadata` comes back `None` on a Python stream: LangChain switches `stream_usage` off as soon as `base_url` is set. Give the constructor, or the `stream` call, `stream_usage=True`.
- The agent writes prose and never touches a tool, or the request errors on `tools`: this build does not accept tool calls. Switch to a build whose page shows them. The default `with_structured_output` method has the same requirement; `method="json_schema"` instead needs a build that accepts `response_format`.
- The reply cuts off and `finish_reason` is `length`: `max_tokens` is below what the answer needs. Raise it, up to the model page’s maximum.

## Questions

### Is LangChain tied to OpenAI’s servers?

No. ChatOpenAI speaks the OpenAI request format and only reaches OpenAI when no base URL is given. Point base_url (Python) or configuration.baseURL (JavaScript) at this API with a console key, and your existing class, chains and agents then run on an uncensored build without any OpenAI account.

### Will ChatOpenAI try the Responses API here?

Only if you ask it to, with use_responses_api=True, or by enabling a Responses-only option like reasoning or a built-in tool. Its default route is Chat Completions, which is what this API serves; keep those off and invoke, stream, bind_tools and create_agent stay on Chat Completions.

### Which builds can I name in the model argument?

Any id on the models page. Each responds on /v1/chat/completions in OpenAI format under one key; write the id as the page does. If you need the agent loop, bind_tools or default structured output, pick one whose page shows tool calls; the table above shows which do. See the models page.

### What makes an API "OpenAI compatible"?

It accepts OpenAI-shaped requests on /v1/chat/completions and sends OpenAI-shaped responses back, so an OpenAI client needs only a new base URL and key. This API qualifies, ChatOpenAI is one of those clients, and the usage_metadata on every reply holds the billed tokens, charged per token against your prepaid credit.

Every model above is on the [uncensored model list](https://pentestllm.ai/models) with its live price per million tokens. [Per-token pricing](https://pentestllm.ai/pricing) sets the whole catalog side by side, and the [family directory](https://pentestllm.ai/uncensored) groups the builds by base model.

[Get a key](https://app.pentestllm.ai/console) Prepaid from $10, no subscription.

## New models and price changes, once a month.

New uncensored builds with their prices, price moves, and what the daily model check found.
