LangChain handles agent orchestration. Sentoria adds a governance boundary around each run: estimate the cost, ask whether the fleet and agent may proceed, then record the actual spend for monitoring and enforcement.

Prerequisites

This guide stays at the integration boundary. Your LangChain application owns the agent invocation and talks to Sentoria over HTTP; there is no Sentoria SDK to install.

  • A Python runtime with langchain, langchain-openai, and requests installed. The example does not specify a minimum Python version.
  • A LangChain v1 agent built with create_agent.
  • A configured ChatOpenAI provider with its OPENAI_API_KEY credential available to the application.
  • Sentoria's SENTORIA_BASE_URL and SENTORIA_FLEET_ID environment values.
  • A stable agent_id for the workflow, not a per-request label.
  • A cost estimate before preflight and a way to calculate or read the actual model-call cost from provider usage or billing data.
The boundary

Send both HTTP governance calls over HTTPS from your server or a trusted worker. LangChain remains responsible for tools, prompts, and model invocation; Sentoria is the fleet-level decision and spend ledger.

Setup: preflight, invoke, record

Generate a run_id for correlation, send the expected cost to the run endpoint, and stop if the response is not successful. Only after a successful preflight should the LangChain agent run. Once the provider returns usage, send a numeric amount to the cost-record endpoint.

Install the example's dependencies with pip install langchain langchain-openai requests. Before running it, set SENTORIA_BASE_URL, SENTORIA_FLEET_ID, and OPENAI_API_KEY. Keep these values and both governance calls on your server or a trusted worker; the model-provider credential must not be exposed to an untrusted client.

Python
langchain_sentoria.py
import os
from uuid import uuid4
import requests

from langchain.agents import create_agent
from langchain_openai import ChatOpenAI

BASE_URL = os.environ["SENTORIA_BASE_URL"].rstrip("/")
fleet_id = os.environ["SENTORIA_FLEET_ID"]
agent_id = "support-triage-v1"
run_id = str(uuid4())
estimated_cost_usd = 0.04  # budget for this run

preflight = requests.post(
    f"{BASE_URL}/api/fleet/{fleet_id}/run",
    json={
        "agent_id": agent_id,
        "estimated_cost_usd": estimated_cost_usd,
        "run_id": run_id,
    },
    timeout=10,
)
if preflight.status_code not in range(200, 300):
    raise RuntimeError(
        f"Sentoria blocked run: {preflight.status_code} {preflight.text}"
    )

agent = create_agent(
    model=ChatOpenAI(model="gpt-4o-mini"),
    tools=[],
    system_prompt="Triage the support request and explain your decision.",
)
result = agent.invoke({
    "messages": [{"role": "user", "content": "Classify this request."}]
})

actual_amount = 0.0031  # example only: calculate from provider usage or trusted billing
requests.post(
    f"{BASE_URL}/api/fleet/{fleet_id}/cost-records",
    json={
        "agent_id": agent_id,
        "amount": float(actual_amount),
        "event_type": "cost_record",
        "metadata": {
            "run_id": run_id,
            "framework": "langchain",
            "model": "gpt-4o-mini",
        },
    },
    timeout=10,
).raise_for_status()

Estimate before; reconcile after. The preflight estimate can come from expected input/output tokens and your provider's current price sheet. The 0.0031 value in the snippet is illustrative only: in production, calculate the actual amount from provider usage or trusted billing data rather than hardcoding it. Keep the same run_id in metadata so a dashboard investigation can connect the decision to the recorded cost.

Configuration and workflow outcomes

The integration has a small set of identifiers and amounts to keep consistent:

  • fleet_id is the stable Sentoria fleet identifier supplied by SENTORIA_FLEET_ID. Inspect the monthly response surface at /api/fleet?period=month or open Fleet Monitor to see spend, agents, and alerts.
  • agent_id is the stable identity for this LangChain workflow. Use the same value for the preflight and cost record across runs.
  • run_id is a per-run correlation value. Generate one for each invocation and reuse it in the cost metadata.
  • estimated_cost_usd is the non-negative expected cost sent to POST /api/fleet/:fleetId/run before the agent starts.
  • amount is the numeric actual cost sent to POST /api/fleet/:fleetId/cost-records after the model call, calculated from provider usage or trusted billing data.
  • SENTORIA_BASE_URL and SENTORIA_FLEET_ID configure Sentoria; OPENAI_API_KEY configures the model provider used by ChatOpenAI.
  • monthly_cap_usd is the optional fleet cap. Configure it on the fleet and treat it as enforcement, not just a dashboard target.
Response Meaning for the integration
201 accepted The preflight passed. Invoke the agent, then post the actual cost record. The response includes the run and cap context.
201 with warning A cost record can return warning_fired: true when month-to-date spend reaches 80% of monthly_cap_usd. Surface that signal to operators.
409 cap rejection The fleet cap or configured agent cap would be exceeded. Do not invoke LangChain; the fleet may be paused and the response identifies the cap breach.
409 anomaly rejection A recent agent anomaly is active. Do not retry blindly; the response uses mechanism: agent_anomaly and records the governance event.
Treat non-2xx as a blocked run

Any non-2xx preflight response is a governance decision, not a transient LangChain error. This includes cap and anomaly rejection: stop before agent.invoke(...), log the response, and alert the owning team until the fleet or agent is eligible again.

Troubleshooting

Classify the failure before changing the LangChain retry behavior. Provider authentication, Sentoria governance, and network or agent connection errors have different owners and fixes.

Authentication failures

A model-provider credential error is separate from a Sentoria fleet-owner check. Verify the server-side OPENAI_API_KEY and provider configuration first; keep credentials off the client. Owner-only anomaly status and block-lift calls use GET /api/fleet/:fleetId/anomaly-status and POST /api/fleet/:fleetId/agents/:agentId/anomaly-block/lift: supply a valid actor that matches the fleet owner. A missing or invalid actor returns 401; a valid actor who is not the owner returns 403. Use Fleet Monitor to confirm the fleet owner and current state before retrying an owner action.

Governance-policy rejections

A 409 from POST /api/fleet/:fleetId/run is an intentional policy decision, not a LangChain retry condition. Inspect fleet_id, estimated_cost_usd, agent_id, and run_id, then read the response's mechanism, audit_event_type, paused, and cap or anomaly details. Check the /api/fleet?period=month response or Fleet Monitor to resolve the fleet cap, agent cap, or active 24-hour anomaly before retrying. The cost-governance guide explains the enforcement model.

Agent connection errors

Check connectivity in this order: confirm SENTORIA_BASE_URL has no trailing path or typo, confirm SENTORIA_FLEET_ID, verify HTTPS and network reachability, then allow for the existing 10-second request timeout. Use the /health endpoint and /api/fleet?period=month as smoke checks, then verify the LangChain/provider model connection separately. Sentoria preflight must succeed before agent.invoke(...); if the cost-record request fails afterward, log and reconcile it rather than silently treating the run as successfully recorded.

Keep the evidence

For support and debugging, capture the unchanged run_id and the full response body in your logs, including when the response is an error.

Where this fits

Sentoria's fleet governance concepts explain the cap and per-call enforcement model, while cost monitoring covers the live attribution and anomaly signals that make the workflow useful after deployment. For the surrounding orchestration trade-offs, see the Temporal and Inngest comparison.

Govern production agent workflows with confidence

Sentoria is the governance layer for production LangChain agent workflows, keeping spend and policy visible from every run to the live fleet.