LangChain has max_iterations. That’s not a budget limit — here’s how to add one.

LangChain’s AgentExecutor accepts a max_iterations parameter (default 15) that stops the agent after N tool-calling steps. That’s a step-count backstop, not a budget limit. A 15-step cap on a GPT-4o agent that uses 20,000-token prompts on each step can still cost $7.50 for a single run. max_iterations=15 is useful for preventing runaway duration, but it doesn’t prevent runaway cost. LangGraph has no built-in iteration or budget limit at all — it runs until the graph reaches a terminal node or the process is killed. This page covers three approaches to add a genuine per-run budget limit to LangChain-based agents, in order of increasing protection.

Approach 1 — max_iterations with a token-size prompt guard (lightweight)

Approach 2 — Callback-based cost accumulation (accurate but post-call)

Approach 3 — RunGuard wrapped LLM (pre-call enforcement + loop detection)

Which approach to use

ApproachAccuracyDetects loops?Works with LangGraph?Complexity
max_iterations + max_tokensApproximateNoPartialLow
BudgetCallbackHandlerGood (1-call overshoot)NoYesMedium
GuardedChatOpenAI (RunGuard)Exact (pre-call)YesYesMedium