Why Anthropic Chose Code as Universal Intent Serialization
TRIGGER
Agents need to produce complex, structured outputs (spreadsheets, presentations, rule definitions) but natural language descriptions or JSON configs lack the precision and composability to reliably express intricate formatting and logic.
APPROACH
Claude.AI's file creation feature has agents write Python scripts to generate Excel spreadsheets, PowerPoint presentations, and Word documents instead of directly outputting these formats. Input: user request for a formatted document. Output: Python script that generates the document with precise formatting. Example: email agent creates inbox rules by generating code that runs on the email-received event, rather than configuring a rules engine via parameters.
PATTERN
“Prompt engineering won't fix malformed Excel or broken PowerPoint schemas—LLM outputs are probabilistic and can't guarantee structural correctness. Generate code that produces the output instead, so linters and type checkers catch errors that prompting cannot prevent.”
✓ WORKS WHEN
- Output requires conditional logic or loops (different formatting based on data values)
- Output format has complex structure that benefits from programmatic generation (nested tables, calculated fields)
- Generated code can be linted or type-checked before execution (TypeScript > JavaScript for feedback)
- User can inspect or modify the generated code (transparency requirement)
✗ FAILS WHEN
- Output is simple enough that code generation adds unnecessary indirection (plain text email)
- Execution environment for generated code isn't available or trusted
- Latency of code generation + execution exceeds direct output by unacceptable margin
- Users need to edit output directly without programming knowledge