← All labs
D1·Fix the code
Losing tool_use blocks in history
This loop fails on the second iteration with an error about a tool_use id having no matching result — even though the code clearly appends a tool_result for every call.
response = client.messages.create(
model="claude-opus-5", max_tokens=16000, tools=tools, messages=messages
)
text = next((b.text for b in response.content if b.type == "text"), "")
messages.append({"role": "assistant", "content": text})
results = [
{"type": "tool_result", "tool_use_id": b.id, "content": run_tool(b.name, b.input)}
for b in response.content if b.type == "tool_use"
]
messages.append({"role": "user", "content": results})1.Why does the follow-up request fail?
2.What should be appended as the assistant turn?