#!/usr/bin/env bash # Context editing (beta context-management-2025-06-27): clear_tool_uses_20250919 with a tiny trigger so the edit # fires on a fabricated 3-tool-call history; prints the context_management.applied_edits response field, then # previews the effect with /v1/messages/count_tokens (original_input_tokens vs input_tokens). # STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): applied_edits [{clear_tool_uses_20250919, # cleared_tool_uses 2, cleared_input_tokens 174}]; count_tokens 838 vs original 928 (~$0.001). # Run: bash examples/anthropic/context-management/context_editing.sh set -eo pipefail cd "$(dirname "$0")/../../.." && source scripts/lib.sh OUTF="$(mktemp)"; trap 'rm -f "$OUTF"' EXIT # `ant` sets ANT_STATUS; calling it inside $(...) would lose it, so write the body to a file instead. call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; } MODEL="${ANTHROPIC_MODEL:-claude-haiku-4-5-20251001}" BODY="$(jq -n --arg m "$MODEL" --arg pad "$(printf 'x%.0s' $(seq 1 400))" '{model:$m, max_tokens:8, tools:[{name:"lookup", description:"Looks up a record.", input_schema:{type:"object", properties:{id:{type:"string"}}}}], messages:([{role:"user", content:"Look up records r0, r1, r2 then reply OK."}] + ([0,1,2] | map([{role:"assistant", content:[{type:"tool_use", id:("toolu_atlas"+tostring), name:"lookup", input:{id:("r"+tostring)}}]}, {role:"user", content:[{type:"tool_result", tool_use_id:("toolu_atlas"+tostring), content:("record "+$pad)}]}]) | add) + [{role:"user", content:"Reply with OK. Do not call tools."}]), context_management:{edits:[{type:"clear_tool_uses_20250919", trigger:{type:"input_tokens", value:1}, keep:{type:"tool_uses", value:1}, clear_at_least:{type:"input_tokens", value:1}}]}}')" call POST /v1/messages -H 'anthropic-beta: context-management-2025-06-27' -d "$BODY" echo "messages HTTP $ANT_STATUS: $(echo "$OUT" | jq -c '{context_management, input_tokens: .usage.input_tokens}')" log_request anthropic POST /v1/messages "$ANT_STATUS" 0.001 "example context-management/context_editing.sh clear_tool_uses $MODEL beta=context-management-2025-06-27" call POST /v1/messages/count_tokens -H 'anthropic-beta: context-management-2025-06-27' -d "$(echo "$BODY" | jq 'del(.max_tokens)')"; CNT="$OUT" echo "count_tokens HTTP $ANT_STATUS: $CNT" log_request anthropic POST /v1/messages/count_tokens "$ANT_STATUS" 0 "example context-management/context_editing.sh count_tokens"