Skip to content

05. Agents & Tool Calling ​

Augmentation·Chapter 05

Tool contracts, loops and fallbacks

6 posts

Where this chapter fits: the hard part of an agent is not knowing how to call a tool, but knowing when not to. This chapter covers tool-contract design, the plan–execute loop, sandboxes and permission boundaries, failure recovery and idempotency, and cost gates for unattended operation.

Questions this chapter answers ​

  • Does this task need an agent, or is a fixed flow more reliable?
  • How should tools be defined so the model does not misuse them?
  • When a multi-step run fails halfway, how do you recover without repeating side effects?

Article index ​

Hands-on4 min read2026-11-30

Compaction now runs in the background by default: what 'may increase token usage' actually costs

Since 2026-10-30 the Agents API compacts context asynchronously by default; compaction.async=false restores blocking behaviour. The same note says background compaction may increase token usage. Two compaction paths, three recovery constraints, and when to set it back to false.

Hands-on5 min read2026-11-30

Handing off 12 times made the last call 992× larger: the Agents SDK forwards the whole history by default

Measured with openai-agents 0.26.0 over 13 model calls and 6 handoffs: the default costs 256,353 input tokens, remove_all_tools saves 83.4% but drops every verifiable field, and nest_handoff_history costs 33.2% more.

Hands-on6 min read2026-11-30

Four retries, four refunds: where the idempotency key belongs, and why it still leaks

A local server that commits the side effect first and only then answers slowly, run across 12 timeout/duration combinations: with a 1-second client timeout and 1.5 seconds of server work, one tool call retries 4 times and leaves 4 side effects while the client reports failure. Add an idempotency key and side effects drop back to 1, with success returned on the second attempt. Then concurrency: ten parallel replays of one call_id leave 8-10 side effects when the check and the write are not atomic, and exactly 1 in all five rounds when they are.

Hands-on5 min read2026-11-30

A tool sandbox is more than a subprocess call: three boundaries, measured against 20 hostile inputs

The same file-reading tool scores 4, 3 and 0 escapes out of 10 depending on how you validate the path; 6 command injections leak 5 under shell=True; and of 4 runaway modes only one is caught by a timeout. Matrices and the exact check included.

Hands-on6 min read2026-11-30

Leaving strict off makes the schema a suggestion: 5 of 12 dirty arguments get through

A tool schema does not stop the model from inventing keys, dropping fields, or sending null for an optional value. Running both schema variants as validators over 12 dirty payloads with jsonschema 4.26.0: the hand-written loose version lets 5 through (41.7%), the strict-compliant version lets 1 through (8.3%). The same check turned up something else: the default for strict differs by endpoint — Chat Completions stays non-strict, Responses tries to upgrade and silently falls back. Plus the token cost of all-required arguments.

Hands-on4 min read2026-11-29

Streaming tool calls keep failing to parse: 2,236 of 2,262 byte cut points break a stateless reader

Eight argument fragments from the official example, assembled into a 2,263-byte SSE stream, then fed through three reader styles at every byte cut point: the stateless per-chunk reader fails at 2,236 of 2,262 cut points (98.85%), while line-buffered and event-buffered readers fail at none. In the delta layer, only 1 of the 8 argument fragments is accepted by json.loads, and that one is just a string.

Topic backlog ​

  • The plan–execute–reflect loop: when to stop
  • Memory tiers: context, summaries and external storage
  • Cost gates and circuit breakers: keeping unattended runs safe

OpenAIChat · Full-stack engineering for LLM-powered conversations and apps