Skip to content
prune.

Prune plus LangChain

One base_url on ChatOpenAI. Chains and agents keep working. Cache and cheaper-model routing run before the provider. Tool allow lists fail closed.

Quick answer

A LangChain agent with OPENAI_API_KEY in .env is one git push or Discord paste from a runaway bill. Vault the provider key in Prune. Point ChatOpenAI at https://api.withprune.com/v1 with a prune_ key. Cache and cheaper-model routing sit on the path. Tool deny lists return 403 before the model runs.

Why not put the provider key in LangChain

Chains, notebooks, and LangGraph workers all copy the same env. Without a vault, every process is a leak surface. Without Shield, a looped tool agent hits the provider until finance notices. Without receipts, you cannot prove what cached, routed, or blocked.

1. Python

pip install langchain-openai. Sign up, vault a provider, copy the Prune key. About five minutes to first receipt.

langchain_prune.py
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o-mini",
    api_key=os.environ["PRUNE_API_KEY"],
    base_url=os.environ.get("PRUNE_BASE_URL", "https://api.withprune.com/v1"),
    default_headers={
        "X-Prune-Model-Policy": "balanced",
        "X-Prune-PII-Mode": "redact",
    },
)

msg = llm.invoke("Reply in one short sentence: what is a spend cap?")
print(msg.content)

2. TypeScript (LangChain.js)

langchain_prune.ts
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "gpt-4o-mini",
  apiKey: process.env.PRUNE_API_KEY,
  configuration: {
    baseURL: process.env.PRUNE_BASE_URL || "https://api.withprune.com/v1",
    defaultHeaders: {
      "X-Prune-Model-Policy": "balanced",
    },
  },
});

3. Claude through LangChain

Drop the /v1. The Anthropic client appends /v1/messages itself. If the package asks for anthropic_api_url, use the same host.

langchain_claude.py
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-sonnet-4-6",
    base_url="https://api.withprune.com",  # host only, no /v1
    api_key=os.environ["PRUNE_API_KEY"],
)

4. Tool firewall for agents

Governance checks the tools list even when tool_choice is "none". Denied tools never reach the provider. Set X-Prune-Cache: off when verifying policy so a prior hit cannot mask a deny.

agent_tools.py
llm = ChatOpenAI(
    model="gpt-4o-mini",
    api_key=os.environ["PRUNE_API_KEY"],
    base_url="https://api.withprune.com/v1",
    default_headers={
        "X-Prune-Tool-Allow": "search,fetch,calculator",
        "X-Prune-Tool-Deny": "shell_exec,browser",
        "X-Prune-Max-Steps": "40",
        "X-Prune-Run-Id": "agent-run-001",
    },
)

With a single line of code, Prune cuts your AI bill and protects your team from runaway spend, leaked keys and dangerous calls.

Related

Have an account? Sign in · PyPI.