D1 · Agentic Architecture › Cluster 1 · Foundations

1.6 · Appending the Whole Assistant Turn

Append the full response.content — dropping blocks breaks the next call

CONCEPT Append the entire turn, verbatim

When you continue a conversation, you must append the whole response.content — the complete list of blocks — as the assistant turn. That includes not just text but any tool_use and thinking blocks the model produced.

⚠️ The #1 beginner bug: appending only the text (e.g. content[0].text) instead of the full block list. This silently drops the model's tool_use and thinking blocks — and the very next call breaks.

Why it breaks: after a tool_use stop, your next turn sends a tool_result block keyed by tool_use_id. If the matching tool_use block isn't in the history (because you dropped it), the request is malformed — the tool_result refers to a tool_use the server can't see. That produces the classic 400 error. Dropping thinking blocks similarly loses reasoning context the model relied on.

ApproachWhat you storeResult
Wrong append .text onlyA string; tool_use & thinking lostNext tool_result has no matching tool_use → 400
Right append response.contentThe full block list, verbatimtool_use/thinking preserved → next call valid

DIAGRAM Correct vs. broken append

flowchart TD R["🤖 response.content
[thinking, text, tool_use]"] --> C{"What do you append?"} C -->|"WHOLE list ✅"| G["messages += assistant turn
with ALL blocks"] G --> G2["Send tool_result (tool_use_id) →
🟢 matches → valid next call"] C -->|"only .text ❌"| B["messages += text-only turn
tool_use DROPPED"] B --> B2["Send tool_result (tool_use_id) →
🔴 no matching tool_use → 400"] classDef good fill:#d4edda,stroke:#28a745,color:#155724; classDef bad fill:#f8d7da,stroke:#dc3545,color:#721c24; class G,G2 good; class B,B2 bad;

CODE The correct append pattern

# ✅ RIGHT: append the whole response.content (all blocks, verbatim).
resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    tools=TOOLS,
    messages=messages,
)
messages.append({"role": "assistant", "content": resp.content})
# resp.content still holds any tool_use & thinking blocks → history stays valid.

if resp.stop_reason == "tool_use":
    for block in resp.content:
        if block.type == "tool_use":
            messages.append({
                "role": "user",
                "content": [{
                    "type": "tool_result",
                    "tool_use_id": block.id,      # must match a tool_use in history
                    "content": run_tool(block.name, block.input),
                }],
            })

# ❌ WRONG — do NOT do this: it drops tool_use & thinking blocks.
# messages.append({"role": "assistant", "content": resp.content[0].text})
Rule: pass resp.content straight through into the assistant turn. It's already the correct block-list shape — don't unwrap it to a string.

TRADE-OFFS Full turn vs. text-only append

✅ Append full content

  • tool_use IDs stay resolvable → no 400s
  • thinking context preserved for the next turn
  • One line: content: resp.content — nothing to reconstruct

⚠️ Append .text only

  • Drops tool_use → next tool_result is orphaned → 400
  • Drops thinking → loses reasoning the model built on
  • Silently "works" until the first tool call, then breaks

EXAM TRAPS

Trap: Appending only response.content[0].text (or .text). → The #1 beginner bug. It drops tool_use and thinking blocks; the next call's tool_result then references a missing tool_use → classic 400. Append the whole response.content.
Trap: Reconstructing the assistant turn by hand from the text. → Unnecessary and error-prone. Pass resp.content through as-is; it's already the correct block list.
Trap: Stripping thinking blocks "to save tokens." → Removing them breaks the turn's integrity for the next call. Keep the full turn in history; manage cost via pruning/ compaction (7.5), not by mangling individual turns.

PRACTICE

Q1 · After a tool call, your next request 400s. History was built by appending content[0].text. Why?

You dropped the assistant's tool_use block by storing only its text. Your subsequent tool_result (keyed by tool_use_id) now references a tool_use that isn't in messages[], so the request is malformed → 400. Fix: append the whole resp.content.

Q2 · What exactly should you pass as the assistant message's content?

The full response.content block list, verbatim — text, thinking, and tool_use blocks included. Don't unwrap it to a string; {"role":"assistant","content": resp.content} is the pattern.

Q3 · Is it safe to delete thinking blocks from a stored assistant turn to cut tokens?

No — dropping blocks from a turn breaks its integrity for the next call. Keep each turn whole in history; reduce cost with pruning/compaction strategies (7.5) instead of editing the blocks within a single turn.