# ReWOO versus ReAct: how many tokens an agent saves by planning its tool calls up front

ReAct calls the model after every tool result and resends the prompt and history each time. ReWOO plans all the tool calls in one pass, runs them, and answers in a second pass. Change the prompt sizes, step lengths and prices and see where the tokens go.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;ReWOO versus ReAct: tokens saved by planning the tool calls up front&#34;,&#34;sub&#34;:&#34;Twenty multi-hop questions in the style of HotpotQA, each needing a few searches. ReAct calls the model once per step and resends the history; ReWOO plans every call once and solves once. Change the prompt sizes and prices and compare the tokens.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;p_react&#34;,&#34;label&#34;:&#34;ReAct prompt (instructions and examples)&#34;,&#34;min&#34;:200,&#34;max&#34;:4000,&#34;step&#34;:50,&#34;value&#34;:1800,&#34;unit&#34;:&#34;tokens&#34;},{&#34;id&#34;:&#34;p_plan&#34;,&#34;label&#34;:&#34;ReWOO planner prompt&#34;,&#34;min&#34;:200,&#34;max&#34;:4000,&#34;step&#34;:50,&#34;value&#34;:1200,&#34;unit&#34;:&#34;tokens&#34;},{&#34;id&#34;:&#34;p_solve&#34;,&#34;label&#34;:&#34;ReWOO solver prompt&#34;,&#34;min&#34;:50,&#34;max&#34;:2000,&#34;step&#34;:50,&#34;value&#34;:300,&#34;unit&#34;:&#34;tokens&#34;},{&#34;id&#34;:&#34;a&#34;,&#34;label&#34;:&#34;ReAct thought and action per step&#34;,&#34;min&#34;:10,&#34;max&#34;:300,&#34;step&#34;:5,&#34;value&#34;:60,&#34;unit&#34;:&#34;tokens&#34;},{&#34;id&#34;:&#34;b&#34;,&#34;label&#34;:&#34;ReWOO plan line per tool call&#34;,&#34;min&#34;:10,&#34;max&#34;:300,&#34;step&#34;:5,&#34;value&#34;:45,&#34;unit&#34;:&#34;tokens&#34;},{&#34;id&#34;:&#34;pin&#34;,&#34;label&#34;:&#34;Input price&#34;,&#34;min&#34;:0.1,&#34;max&#34;:15,&#34;step&#34;:0.1,&#34;value&#34;:3,&#34;unit&#34;:&#34;$ / M tokens&#34;},{&#34;id&#34;:&#34;pout&#34;,&#34;label&#34;:&#34;Output price&#34;,&#34;min&#34;:0.4,&#34;max&#34;:75,&#34;step&#34;:0.1,&#34;value&#34;:15,&#34;unit&#34;:&#34;$ / M tokens&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;q&#34;,&#34;calls&#34;,&#34;q_tok&#34;,&#34;obs_tok&#34;],&#34;rows&#34;:[[1,2,22,260],[2,2,18,310],[3,3,27,280],[4,2,31,220],[5,3,24,340],[6,4,29,300],[7,2,16,180],[8,3,35,410],[9,5,33,290],[10,2,20,350],[11,3,26,240],[12,4,38,330],[13,2,19,270],[14,3,23,390],[15,6,41,310],[16,2,25,200],[17,3,30,360],[18,4,28,250],[19,2,21,430],[20,3,34,320]],&#34;note&#34;:&#34;Illustrative HotpotQA-style questions: tool calls needed (k), question tokens and mean tokens per search result. The answer is 20 tokens. Export the same numbers from your agent traces.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;kbar&#34;,&#34;label&#34;:&#34;Mean tool calls per question&#34;,&#34;expr&#34;:&#34;mean(calls)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;qbar&#34;,&#34;label&#34;:&#34;Mean question tokens&#34;,&#34;expr&#34;:&#34;mean(q_tok)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;obar&#34;,&#34;label&#34;:&#34;Mean tokens per observation&#34;,&#34;expr&#34;:&#34;mean(obs_tok)&#34;,&#34;fmt&#34;:&#34;i&#34;}],&#34;row&#34;:[{&#34;id&#34;:&#34;react_in&#34;,&#34;label&#34;:&#34;ReAct input&#34;,&#34;expr&#34;:&#34;(calls + 1)*(p_react + q_tok) + (a + obs_tok)*calls*(calls + 1)/2&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;I^{ReAct} = (k+1)(P_R + q) + (a + o)\\,\\frac{k(k+1)}{2}&#34;},{&#34;id&#34;:&#34;react_out&#34;,&#34;label&#34;:&#34;ReAct output&#34;,&#34;expr&#34;:&#34;(calls + 1)*a + 20&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;react_tok&#34;,&#34;label&#34;:&#34;ReAct tokens&#34;,&#34;expr&#34;:&#34;react_in + react_out&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;rewoo_in&#34;,&#34;label&#34;:&#34;ReWOO input&#34;,&#34;expr&#34;:&#34;(p_plan + q_tok) + (p_solve + q_tok + calls*(b + obs_tok))&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;I^{ReWOO} = (P_P + q) + \\big(P_S + q + k\\,(b + o)\\big)&#34;},{&#34;id&#34;:&#34;rewoo_tok&#34;,&#34;label&#34;:&#34;ReWOO tokens&#34;,&#34;expr&#34;:&#34;rewoo_in + calls*b + 20&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;T^{ReWOO} = I^{ReWOO} + k\\,b + 20&#34;},{&#34;id&#34;:&#34;saved&#34;,&#34;label&#34;:&#34;tokens saved&#34;,&#34;expr&#34;:&#34;react_tok - rewoo_tok&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;ratio&#34;,&#34;label&#34;:&#34;ReAct / ReWOO&#34;,&#34;expr&#34;:&#34;react_tok/rewoo_tok&#34;,&#34;tex&#34;:&#34;r_i = T^{ReAct}_i / T^{ReWOO}_i&#34;}],&#34;derived&#34;:[{&#34;id&#34;:&#34;react_mean&#34;,&#34;label&#34;:&#34;ReAct tokens per question&#34;,&#34;expr&#34;:&#34;mean(react_tok)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;rewoo_mean&#34;,&#34;label&#34;:&#34;ReWOO tokens per question&#34;,&#34;expr&#34;:&#34;mean(rewoo_tok)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;eff&#34;,&#34;label&#34;:&#34;Token efficiency (ReAct / ReWOO)&#34;,&#34;expr&#34;:&#34;react_mean/rewoo_mean&#34;,&#34;tex&#34;:&#34;\\frac{\\sum_i T^{ReAct}_i}{\\sum_i T^{ReWOO}_i}&#34;},{&#34;id&#34;:&#34;cut&#34;,&#34;label&#34;:&#34;Share of tokens saved&#34;,&#34;expr&#34;:&#34;1 - rewoo_mean/react_mean&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;prompt_share&#34;,&#34;label&#34;:&#34;Share of ReAct input that is the repeated prompt&#34;,&#34;expr&#34;:&#34;(sum(calls) + n)*p_react/sum(react_in)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\frac{\\sum_i (k_i+1)\\,P_R}{\\sum_i I^{ReAct}_i}&#34;},{&#34;id&#34;:&#34;llm_react&#34;,&#34;label&#34;:&#34;LLM calls per question, ReAct&#34;,&#34;expr&#34;:&#34;kbar + 1&#34;},{&#34;id&#34;:&#34;cost_react&#34;,&#34;label&#34;:&#34;ReAct cost per 1,000 questions ($)&#34;,&#34;expr&#34;:&#34;(sum(react_in)*pin + sum(react_out)*pout)/1000/n&#34;,&#34;tex&#34;:&#34;C = \\frac{1000}{n}\\sum_i \\frac{I_i p_{in} + O_i p_{out}}{10^6}&#34;},{&#34;id&#34;:&#34;cost_rewoo&#34;,&#34;label&#34;:&#34;ReWOO cost per 1,000 questions ($)&#34;,&#34;expr&#34;:&#34;(sum(rewoo_in)*pin + (sum(calls)*b + 20*n)*pout)/1000/n&#34;},{&#34;id&#34;:&#34;max_ratio&#34;,&#34;label&#34;:&#34;Largest saving ratio (one question)&#34;,&#34;expr&#34;:&#34;amax(ratio)&#34;}],&#34;chart&#34;:{&#34;title&#34;:&#34;Tokens per question against the number of tool calls&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Tool calls in the question (k)&#34;,&#34;from&#34;:1,&#34;to&#34;:7},&#34;series&#34;:[{&#34;label&#34;:&#34;ReAct (mean question and observation)&#34;,&#34;expr&#34;:&#34;(t + 1)*(p_react + qbar) + (a + obar)*t*(t + 1)/2 + (t + 1)*a + 20&#34;},{&#34;label&#34;:&#34;ReWOO (mean question and observation)&#34;,&#34;expr&#34;:&#34;p_plan + p_solve + 2*qbar + t*(b + obar) + t*b + 20&#34;}],&#34;fit&#34;:{&#34;x&#34;:&#34;calls&#34;,&#34;y&#34;:&#34;react_tok&#34;,&#34;label&#34;:&#34;ReAct tokens of each question&#34;}},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Tokens ReWOO saves on each question&#34;,&#34;y&#34;:&#34;saved&#34;,&#34;label&#34;:&#34;q&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;ReAct: one model call per step&#34;,&#34;text&#34;:&#34;ReAct interleaves thought, action and observation. A question with k tool calls needs k + 1 model calls, {llm_react} on average here, and each call resends the prompt and the whole history so far.&#34;,&#34;show&#34;:[&#34;llm_react&#34;]},{&#34;title&#34;:&#34;Count what ReAct resends&#34;,&#34;text&#34;:&#34;Across the questions {prompt_share:p} of ReAct&#39;s input is the same instructions and examples sent again; observations of {obar} tokens are also resent at every later step.&#34;,&#34;show&#34;:[&#34;prompt_share&#34;,&#34;obar&#34;]},{&#34;title&#34;:&#34;ReWOO: plan once&#34;,&#34;text&#34;:&#34;The planner writes every step at once as Plan and #E1 = Tool[argument] lines, using variables for results it has not seen yet. Workers run the tools without calling the model.&#34;},{&#34;title&#34;:&#34;Solve once&#34;,&#34;text&#34;:&#34;The solver reads the plan and the evidence and answers. ReWOO uses {rewoo_mean} tokens per question against {react_mean} for ReAct.&#34;,&#34;show&#34;:[&#34;rewoo_mean&#34;,&#34;react_mean&#34;]},{&#34;title&#34;:&#34;Compare&#34;,&#34;text&#34;:&#34;ReAct spends {eff} times as many tokens ({cut:p} saved by ReWOO): ${cost_react} against ${cost_rewoo} per 1,000 questions.&#34;,&#34;show&#34;:[&#34;eff&#34;,&#34;cut&#34;,&#34;cost_react&#34;,&#34;cost_rewoo&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

Each row is a multi-hop question in the style of HotpotQA, such as "Which magazine was started first, Arthur's Magazine or First for Women?". The white columns say how many tool calls (searches) the question needs, how long the question is and how many tokens a search result has on average. The orange columns count the tokens two agent designs spend on the same calls: ReAct, which thinks and acts one step at a time, and ReWOO (Reasoning WithOut Observation), which plans every call first.

The chart shows tokens per question against the number of tool calls. The upper curve is ReAct with the average question and observation, and it bends upwards; the lower, straight line is ReWOO. The dots are the ReAct totals of the 20 questions. The bars show the tokens ReWOO saves on each one. For the ReAct loop itself, see the [ReAct simulation](/ai-simulations/react-tool-calling) and the [ReAct loop](/tools/react-loop) page.

## The algorithm

ReAct. For a question that needs k tool calls the model is called k + 1 times: k times to choose an action and once to answer. Every call is stateless, so it resends the instructions and examples (P_R), the question (q) and every earlier thought, action and observation. With a tokens of thought and action per step and o tokens per observation, the input is

I_ReAct = (k + 1)(P_R + q) + (a + o) · k(k + 1)/2

The first term repeats the prompt; the second grows with the square of the steps, as in the [agent loop cost simulation](/ai-simulations/agent-loop-token-cost).

ReWOO. The paper splits the agent into three modules. The Planner writes the whole plan in one call, as lines like "Plan: find the start year of Arthur's Magazine. #E1 = Wikipedia[Arthur's Magazine]", using variables such as #E1 for results it has not seen. Workers run the tools and substitute the variables, without a model call. The Solver reads the plan and the evidence and answers. With b tokens per plan line and prompts P_P and P_S:

I_ReWOO = (P_P + q) + (P_S + q + k(b + o))

Each observation is read once, and each prompt is sent once.

Worked example. With the defaults (ReAct prompt 1,800 tokens, planner prompt 1,200, solver prompt 300, a = 60, b = 45) question 1 has k = 2, q = 22 and o = 260. ReAct sends 3 × 1,822 + 320 × 3 = 6,426 input tokens and writes 3 × 60 + 20 = 200, 6,626 in all. ReWOO sends 1,222 + (322 + 2 × 305) = 2,154 and writes 2 × 45 + 20 = 110, 2,264 in all: 2.93 times fewer. Question 15 needs six searches: ReAct spends 21,097 tokens and ReWOO 4,002, 5.27 times fewer. Over the 20 questions ReAct averages 9,988 tokens and ReWOO 2,759, a token efficiency of 3.62 and 72.4% saved; 74.0% of ReAct's input is the same prompt sent again. At $3 and $15 per million tokens that is $33.08 against $10.14 per 1,000 questions. The ReWOO paper reports about 5 times better token efficiency on HotpotQA together with a 4% accuracy gain; real ReAct runs often take extra steps after failed searches, which this model leaves out.

## Things to try

- Shrink both the ReAct and planner prompts to 600 tokens. The efficiency falls to 2.40 and the saving to 58.4%: most of ReWOO's advantage is not resending a long prompt with examples.
- Raise ReAct's thought and action to 150 tokens, as with verbose reasoning. ReAct rises to 10,942 tokens per question and the efficiency to 3.97.
- Raise the solver prompt to 1,000 tokens. ReWOO costs 3,459 tokens and the efficiency drops to 2.89.
- Change question 15 to 8 tool calls. Its ReAct cost jumps to 30,449 tokens against 4,802 for ReWOO, a ratio of 6.34: the longer the task, the bigger the gap.
- Drop the input price to $0.3. ReAct costs $6.82 and ReWOO $3.11 per 1,000 questions: when input is cheap or cached, the saving in dollars shrinks.

## How it works in real systems

The saving comes with a trade-off. ReWOO's planner commits to a plan before it sees any result, so it cannot react to a search that returns nothing or a surprise in the evidence. ReAct can. The ReWOO paper also notes robustness to tool failure and that the planner, because its training data does not depend on tool outputs, can be fine-tuned into a much smaller model: it offloaded the planning from GPT-3.5 (175B) to a 7B LLaMA model.

The [LangGraph](/tools/langgraph) tutorial implements ReWOO as three nodes, planner, tool executor and solver, sharing a state with the task, the plan string, the parsed steps and the results. It lists two limits from the paper: with little context about the environment the planner uses tools badly, which few-shot examples or fine-tuning help, and the tasks still run one after another, so latency is the sum of every tool call. Running independent calls in parallel is the next step, see [parallel tool calls](/ai-simulations/parallel-tool-calls-critical-path).

In practice many agents sit between the two: [plan-and-execute](/tools/plan-and-execute) agents plan up front and re-plan after each step or when something fails, which gives back part of ReAct's flexibility for part of its cost. Prompt caching also changes the sums: if the repeated ReAct prefix is cached, its input is billed at a fraction of the price, see the [prompt cache simulation](/ai-simulations/prompt-cache-hits-agent-session). Count both tokens and accuracy on your own traces before choosing.

## Sources

- [ReWOO code and abstract (billxbf/ReWOO)](https://github.com/billxbf/ReWOO)
- [LangGraph ReWOO tutorial notebook](https://github.com/langchain-ai/langgraph/blob/23961cff61a42b52525f3b20b4094d8d2fba1744/docs/docs/tutorials/rewoo/rewoo.ipynb)
- [ReAct prompting code (ysymyth/ReAct)](https://github.com/ysymyth/ReAct)
- [Gentopia, the maintained ReWOO implementation](https://github.com/Gentopia-AI/Gentopia)
