Optimization Technique

TextGrad

Borrow the idea that trained neural networks with backpropagation, but do it in words: pass natural-language feedback backward through an AI system so each part, a prompt, a solution, a component, gets a concrete suggestion for how to improve.

*These are learning tools, not prompts. They teach you to write your own, think of them as training wheels that guide you while you learn, then fall away once you can ride on your own.

The Core Insight

Gradients, But in Natural Language

Neural networks learn because backpropagation sends a signal backward through the system telling each parameter how to change. Modern AI systems are increasingly built from multiple pieces, a prompt, a tool call, a model response, chained together, but those pieces are text, not numbers, so ordinary gradients do not apply.

TextGrad brings the same idea to text. Instead of numerical gradients, it uses natural-language feedback: an LLM critiques a component and produces a rich, specific suggestion for how to improve it, a “textual gradient.” That feedback is passed backward through the system, so each part receives guidance shaped by how it contributed to the final outcome, exactly the role a gradient plays in a neural network.

It is packaged as a framework that follows PyTorch’s familiar syntax and abstractions, so the same backward-pass idea works out of the box across many tasks. You define the pieces of your compound AI system, run it forward to get a result, evaluate that result, and then let TextGrad backpropagate the textual feedback to optimize prompts, solutions, or whatever components you have marked as improvable.

Why Text Feedback Works Like a Gradient

A numerical gradient answers “which direction improves the loss?” A textual gradient answers the same question in words: “this step failed because it ignored the constraint; state the constraint explicitly.” Because the feedback is specific and tied to the outcome, and because it flows backward through the whole chain, each component is improved in the context of how it affected the final result, not in isolation. That is what makes TextGrad an optimizer, not just a one-off critique.

The TextGrad Process

A forward pass, an evaluation, and a backward pass of textual feedback

1

Build the System and Run It Forward

Define your compound AI system as a chain of components, prompts, model calls, tools, and mark which ones are improvable variables. Run it forward on an input to produce an output, just as a forward pass through a network produces a prediction.

Example

A system where a solver prompt turns a hard question into an answer; the solver prompt is the variable you want to optimize.

2

Evaluate the Output

Assess the result to get a loss signal, but a textual one. An evaluator, often an LLM with a rubric or a ground-truth check, describes what is wrong or could be better in natural language. This critique is the text equivalent of a loss value.

Example

“The answer reached the wrong final value because it applied the formula before checking the units.”

3

Backpropagate Textual Gradients

The critique is passed backward through the system. At each improvable component, an LLM converts the downstream feedback into a concrete suggestion for that component, a textual gradient describing how to change it to improve the outcome.

Example

The solver prompt receives: “Add an instruction to verify units before applying any formula.”

4

Update and Iterate

Each variable is updated by applying its textual gradient, revising the prompt, the solution, or the component. Run the system forward again and repeat, and the components improve over iterations, the same optimize-evaluate-update loop that trains a network, carried out entirely in language.

Example

After a few rounds, the solver prompt reliably checks units first, and accuracy on the task climbs.

See the Difference

Hand-tuning components versus backpropagating textual feedback

Manual Tuning

Approach

Notice the system got something wrong, guess which prompt or step to edit, tweak it by hand, and re-run, repeating by intuition.

Limitation

In a multi-step system it is hard to know which component caused the failure, so tuning is slow, manual, and easy to get wrong.

Guesswork, no principled way to assign blame across components
VS

TextGrad

Approach

Evaluate the final output, then backpropagate textual feedback so each component receives a targeted, outcome-aware suggestion for how to improve.

Benefit

Blame and improvement flow to the right components automatically, and the same PyTorch-like loop optimizes prompts and solutions across tasks.

Principled, automatic optimization of a compound AI system

Practice Responsible AI

Always verify AI-generated content before use. AI systems can produce confident but incorrect responses. When using AI professionally, transparent disclosure is both best practice and increasingly a legal requirement.

Most US states are actively legislating AI transparency and accountability. Critical thinking remains your strongest tool against misinformation.

TextGrad in Action

Backpropagating textual feedback across different kinds of components

Setup

A question-answering prompt that scores well on easy questions but stumbles on ones requiring careful, multi-step reasoning.

TextGrad Loop

Evaluate: “Wrong answers skip intermediate steps and jump to a conclusion.”

Textual gradient to the prompt: “Instruct the model to lay out each reasoning step and to state its assumptions before answering.”

Update: the prompt is revised accordingly. Over a few iterations, the prompt reliably elicits step-by-step reasoning and accuracy improves, without anyone hand-guessing the wording.

Setup

A draft solution to a coding problem, treated as the optimizable variable, that passes some tests but fails on edge cases.

TextGrad Loop

Evaluate: the test harness reports which edge cases fail and why (empty input, off-by-one at boundaries).

Textual gradient to the solution: “Handle the empty-input case explicitly and fix the boundary condition in the loop.”

Update: the solution is rewritten to address exactly those failures, then re-tested. The feedback targets the real defects rather than a blind rewrite.

Setup

A pipeline that retrieves context, drafts an answer, and then summarizes it, where the final summary is sometimes off-target.

TextGrad Loop

Evaluate: “The summary omits the key figure the question asked for.”

Backward pass: the feedback flows through the chain, the summarizer prompt is told to preserve requested figures, and the drafting step is told to surface them clearly for the summarizer.

Update: because the textual gradient reaches each improvable component in context, the whole pipeline improves together rather than one piece in isolation.

When to Use TextGrad

Best for optimizing systems built from multiple text components

Perfect For

Compound AI Systems

Pipelines built from several prompts, tools, and model calls, where you need a principled way to improve the pieces in concert.

Automated Prompt Optimization

When you want prompts improved by feedback rather than by hand, TextGrad turns evaluation into concrete, targeted prompt edits.

Iterative Solution Refinement

Problems where a draft answer or artifact can be scored and improved over rounds, code, plans, or structured outputs.

Teams Familiar With PyTorch

Because it mirrors PyTorch’s syntax and abstractions, engineers who know autograd can adopt the same forward-backward workflow quickly.

Skip It When

A Single Static Prompt

For one prompt you will write once and not iterate on, the overhead of setting up a forward-backward optimization loop is unnecessary.

No Reliable Evaluator

The optimization is only as good as the feedback. If you cannot judge output quality consistently, the textual gradients will be noisy.

Tight Latency or Budget

Each iteration runs the system and calls an LLM for feedback. When calls must be minimized, the iterative loop may be too costly.

Use Cases

Where TextGrad delivers the most value

Prompt Engineering at Scale

Optimize prompts automatically against a metric, replacing manual trial-and-error with feedback-driven edits that target the actual failure modes.

Code Solution Improvement

Refine code against a test suite, letting failure feedback flow back as targeted fixes instead of blind regeneration.

LLM Pipeline Tuning

Improve retrieval-augmented and multi-stage pipelines end to end, so every component is adjusted in light of the final output quality.

Scientific and Technical Optimization

Iteratively improve structured artifacts, plans, designs, analyses, where an evaluator can describe what is lacking in words.

Research and Experimentation

Prototype optimization ideas for text-based systems quickly, reusing a familiar autograd-style workflow for prompts and components.

Teaching Optimization Concepts

Illustrate backpropagation and optimization intuitively by showing gradients as readable feedback rather than opaque numbers.

Where TextGrad Fits

From hand-tuned prompts to autograd-style optimization in language

Manual Prompting Hand Tuning Edit prompts by intuition and re-run
Automatic Prompt Search Generate and Score Propose candidate prompts, keep the best
TextGrad Textual Backpropagation Feedback flows backward to improve each component
Self-Improving Systems Optimized Pipelines Compound AI systems that tune themselves from feedback
Autograd for Words

TextGrad’s contribution is an abstraction: treat natural-language feedback as a gradient and backpropagate it through a compound AI system, using PyTorch-like syntax so the pattern generalizes across tasks. That reframing turns messy, manual prompt-and-component tuning into a principled optimize-evaluate-update loop. It sits alongside other prompt-optimization frameworks in this library, but where many search over candidate prompts, TextGrad assigns credit and improvement to each component through an explicit backward pass.

Optimize With Feedback, Not Guesswork

Explore how feedback-driven optimization sharpens prompts and pipelines, and build better prompts with our interactive tools.