Skip to content
PALM SOUQ

Agent framework

LangChain and LangGraph logoPALM SOUQ for LangChain and LangGraph

Put PALM SOUQ under LangChain chat models and LangGraph agents by setting the base URL on the model class. Loop heuristics then watch every step of the graph, not just the final answer.

One base URL change · tenant headers · <5 ms added
langchain · setup
import os
from langchain_openai import ChatOpenAI

def model_for(tenant, node: str) -> ChatOpenAI:
    return ChatOpenAI(
        model="gpt-4.1-mini",
        base_url="https://gateway.palmsouqltd.com/v1",
        api_key=os.environ["OPENAI_API_KEY"],
        max_retries=1,
        default_headers={
            "X-PalmSouq-Key": os.environ["PALMSOUQ_KEY"],
            "X-PalmSouq-Tenant": tenant.id,
            "X-PalmSouq-Tier": tenant.plan,
            "X-PalmSouq-Feature": node,        # planner, researcher, writer
            "X-PalmSouq-User": tenant.session_id,
        },
    )

planner = model_for(tenant, "planner").bind_tools(tools)

Wiring LangChain models to the gateway

LangChain's provider packages wrap the official SDKs, and they expose the base URL and default headers. ChatOpenAI takes base_url and default_headers; ChatAnthropic takes base_url as well. Set them to the PALM SOUQ gateway and every chain, tool-calling agent and LangGraph node that uses the model is metered and governed.

Tenant identity usually changes per request in a multi-tenant app. The simplest pattern is a small factory that builds the model with the current tenant's headers, as in the example below. Model objects are cheap to construct, and the underlying HTTP client is pooled.

Loops in graphs

LangGraph has its own recursion_limit, which stops a graph after a set number of supersteps. That is a useful backstop, but it counts steps, not money, and it resets per invocation. An agent that re-invokes itself from a queue, or a graph with expensive nodes, can stay under the step limit and still burn through a budget.

PALM SOUQ adds three checks the framework cannot see from inside one run: identical tool calls across invocations in the same session, token velocity per tenant across all their sessions, and dollar budgets per tenant per day or month. Pass a session id in X-PalmSouq-User or a custom metadata header and the heuristics group calls correctly.

Handling a tripped breaker in your graph

When a breaker trips, the gateway returns HTTP 429 with X-PalmSouq-Reason: circuit_breaker_triggered. The OpenAI and Anthropic SDKs raise a rate limit error, which LangChain propagates. Disable automatic retries for that reason (set max_retries low, or catch the error and inspect the header) so your graph routes to a human review node or a graceful message instead of retrying into the same breaker.

Feature tags make LangGraph cost reports readable. Set X-PalmSouq-Feature per node, such as planner, researcher or writer, and the PALM SOUQ console shows which node of which graph spends the most for each tenant.

Rolling it out safely

Introduce the model factory in one graph and tag each node with a feature name. Run the graph through the gateway in staging and check the per-feature view: it usually shows that one node, often a planner or a reflection step, accounts for most of the cost.

Then decide on breaker behaviour per node. A researcher node that calls search tools benefits from the tool thrashing heuristic, while a writer node is better protected by a dollar budget per session. Both are policies on the gateway, not code in your graph.

Govern LangChain and LangGraph usage per tenant, starting today.

Route your agent traffic through PALM SOUQ in 2 minutes. Change one base URL, pass a tenant header, and every token is attributed and governed.

14-day free production trial · 100k requests included · no card required