Skip to content
mentoor

Topic 3 of 4 · 5 min read

Preserving reasoning state across tool calls

Goal: Keep reasoning state intact across a multi-step tool loop by passing thinking or reasoning items back unchanged.

In a tool loop, send the model's thinking back exactly as you received it. Rebuilding the history from only the text and the tool calls throws away the reasoning state, and the model either errors or loses its plan.

Check yourself · 1 of 3

In a Claude tool loop, what should go into history after a response that contains a thinking block and a tool_use block?

Choose one answer

Why it matters

A tool call splits one task over several requests. The model thinks, asks for a tool, you run it, and you call the API again with the result. Without the earlier thinking, the model must work out its plan again, and some APIs reject the request.

What each vendor expects

  • Claude. The response content can contain thinking blocks, each with a signature. When the turn ends in a tool_use, return the whole assistant content, thinking blocks included, followed by your tool_result. Do not edit, trim or reorder the blocks. The signature lets the API verify they are unmodified.
  • OpenAI. The response output contains reasoning items next to function calls. Pass those items back with the tool outputs, or chain requests with previous_response_id so the server keeps them for you.
  • Gemini. Responses carry thought signatures. Send them back in the same position in the next turn.

The exact fields change, so check the page for your SDK. The rule does not change: pass back what you got, untouched.

How to build the loop

  1. Append the full assistant message to your history, not a copy that keeps only the text.
  2. Run each tool call and append a result that matches its call id.
  3. Call the API again with the complete history.
  4. Stop when the model returns a normal answer with no tool call.

Before and after

# Before: keeps only text and tool_use, drops thinking blocks
history.append({"role": "assistant", "content": [
    b for b in resp.content if b.type in ("text", "tool_use")
]})

# After: the assistant turn goes back unchanged
history.append({"role": "assistant", "content": resp.content})
history.append({"role": "user", "content": tool_results})

What goes wrong

  • Filtering the content. Dropping blocks to save tokens can cause a 400 error or a model that forgets why it called the tool.
  • Editing thinking text. Changing or summarising a block breaks its signature.
  • Mixing parts of turns. Keeping reasoning from an earlier turn while dropping the matching tool calls leaves a history that does not fit together.
  • Lost state in custom stores. A database column that saves only text loses the blocks, and the next request fails after a restart.

Examples

The dropped block

resp = client.messages.create(**params, tools=tools)
# resp.content = [thinking, tool_use]
history.append({"role": "assistant", "content": [resp.content[1]]})

Only the tool_use was kept. The next request omits the thinking block that belongs to that turn. Keep the full resp.content list.

Chaining on the server

first = client.responses.create(model=MODEL, input=task, tools=tools)
second = client.responses.create(
    model=MODEL,
    previous_response_id=first.id,
    input=[{"type": "function_call_output",
            "call_id": call.call_id, "output": result}],
)

With previous_response_id the server already holds the reasoning items from the first call, so you send only the tool output.

Key points

  • In a tool loop, append the whole assistant response to history, including thinking blocks or reasoning items.
  • Never edit, trim or reorder returned thinking; Claude signatures let the API detect changes.
  • OpenAI can keep reasoning for you through previous_response_id, or you pass reasoning items back yourself.
  • A history store that saves only the text of a turn will break tool loops after a restart.
  • Missing reasoning state shows up as 400 errors or as a model that repeats or abandons its plan.

Common mistakes

  • Storing only the text of an assistant turn in the database.
  • Stripping thinking blocks to save input tokens.
  • Editing or summarising returned thinking before sending it back.
  • Sending a tool result whose call id does not match the call in the history.

Check yourself · 2 of 3

What does previous_response_id do in the OpenAI Responses API?

Choose one answer

Check yourself · 3 of 3

After a server restart, tool loops start failing with 400 errors. The database stores only each assistant turn's text. What is the likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic