2AI Refactorings
35 AI refactorings with an eval before and after
How to change the structure of an LLM system without changing what users get. Every refactoring starts and ends with an eval.
- items
- 35
- Groups
- 6
Composing prompts 7
Split, merge, and reorganize prompts so each one has a single job.
- Refactoring
Extract Subprompt
Pull one responsibility out of the main prompt and give it its own call.
- Refactoring
Inline Trivial Step
Remove a call that does not change the result and fold its work into the neighboring step.
- Refactoring
Replace Rules with Examples
Replace a pile of rules with a few well chosen examples that show the behavior.
- Refactoring
Move Instruction to Tool Description
Move a tool's usage rule into its description, right where the model makes the choice.
- Refactoring
Extract Versioned Template
Gather scattered prompt fragments into one template with a name, version, and variables.
- Refactoring
Separate Instructions from Data
Clearly mark what is a system instruction and what is content to be read.
- Refactoring
Move Few-Shots to a Dataset
Take examples out of the code into a versioned set, picked at request time.
Moving control 7
Move decisions out of the prompt and into places where code can guarantee them.
- Refactoring
Replace Prompt Rule with Graph Edge
Turn an IF this, do that written in the prompt into an explicit transition in code.
- Refactoring
Replace Agent with Chain
When the path is always the same, fix the order of steps in code.
- Refactoring
Extract Subagent
Give a large task its own agent with a clean context and only the tools it needs.
- Refactoring
Add Loop Budget
Cap steps, time, and money for each run and define what happens at the limit.
- Refactoring
Parallelize Independent Steps
Run calls that do not depend on each other at the same time.
- Refactoring
Introduce Router
Classify the input first and send it to a specialized prompt or agent.
- Refactoring
Collapse Agents
Merge agents that only pass text along into one agent with the right tools.
Organizing data 5
Give shape to what goes in and out of each step.
- Refactoring
Replace Free Text with Structured Output
Define a schema for the step's output and validate it before moving on.
- Refactoring
Replace History with Summary
Replace old turns with a summary and with facts stored outside the context.
- Refactoring
Move Retrieval Out of the Prompt
Stop pasting documents into the system prompt and fetch only the relevant passage.
- Refactoring
Replace RAG with Direct Context
When the material fits in the window, send it whole and use prompt caching.
- Refactoring
Normalize Tool Output
Make every tool return only the useful fields in a stable format.
Simplifying tools 5
Fewer, clearer tools with the least power they need.
- Refactoring
Consolidate Tools
Merge similar tools and remove the ones nobody uses.
- Refactoring
Replace LLM Call with Code
Move calculation, formatting, and validation into a function the model calls.
- Refactoring
Narrow Tool Permissions
Give each tool only the access it needs for its use case.
- Refactoring
Split God Tool
Break a generic tool (run_sql, call_api) into named, safe operations.
- Refactoring
Expose Tool via MCP
Publish the integration as an MCP server so any agent uses the same contract.
Safety and reliability 6
Put gates, quarantines, and fallbacks where mistakes are expensive.
- Refactoring
Add Approval Before Side Effect
Pause the run and show the action to a human before paying, sending, or deleting.
- Refactoring
Quarantine Untrusted Content
Read outside content with a tool-less model and pass along only structured data.
- Refactoring
Validate Before Execute
Check generated code, SQL, or commands and run them in an isolated environment.
- Refactoring
Add Fallback Path
Define what happens when the model, the tool, or the provider fails.
- Refactoring
Classify Errors Before Retry
Separate transient errors from permanent ones and retry only what can succeed.
- Refactoring
Move Secret Out of the Prompt
Keep credentials inside the tool code, where the model never sees them.
Operations 5
Measure, observe, and cut cost before you scale.
- Refactoring
Add Eval Before Changing the Prompt
Build a test set and measure the current version before any tweak.
- Refactoring
Add Tracing
Record every call, tool, prompt version, and cost in a browsable trace.
- Refactoring
Introduce Cache
Reuse prompt prefixes and answers for equivalent questions.
- Refactoring
Introduce Model Cascade
Try a cheap model first and escalate only when confidence is low.
- Refactoring
Move to Durable Execution
Persist state at every step so the run can resume after a crash, a wait, or an approval.