Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Prompt Chaining

Prompt chaining splits a task into a sequence of model calls, building each prompt from the previous output, with code checks between steps.

Prompt chaining splits a task into a sequence of calls to a large language model (LLM) or other model, where each prompt is built from the output of the previous call. Instead of one long prompt that must read a support email, find the order number, check the order, and draft a reply, a chain runs a separate, narrower prompt for each step, and ordinary code passes the results along and checks them in between. Every intermediate output can be inspected, tested, and fixed on its own. Tongshuang Wu, Michael Terry, and Carrie Cai studied the pattern in AI Chains, presented at CHI 2022, where chaining improved the quality of results and participants' sense of transparency and control in a 20-person user study.

How a Prompt Chain Works#

A chain is a fixed program. The developer decides the steps, their order, and the prompt template for each one, and code moves data between them, often as JSON that a structured output schema guarantees can be parsed. Anthropic's Building Effective Agents guide classifies prompt chaining as a workflow, a system whose path is set in code, and describes programmatic checks, called gates, between steps to confirm the process is still on track. Its examples include writing marketing copy and then translating it, and drafting an outline, checking it against criteria, and only then writing the document. The trade is latency for accuracy: each call is easier to get right, and every step adds a round trip.

Gates matter because errors compound. A wrong value extracted in step one becomes a confident fact in step three, so chains usually validate formats, ranges, or required fields before passing anything on. Longer chains add loops like the one above, retrying a step with the failure message included in the prompt, or a routing step in which a first call labels the request and code picks the prompt that handles it. Frameworks such as LangChain provide building blocks for composing chains, though many production chains are a few function calls in plain Python.

Real-World Applications#

  • Code generation: AlphaCodium chains problem analysis, test generation, solution drafting, and test-driven repair, and raised GPT-4's pass@5 on CodeContests validation problems from 19% with a single direct prompt to 44%.
  • Document processing: Invoice and contract pipelines extract fields in one call, validate them in code, and generate summaries or emails from the validated fields in another.
  • Retrieval-augmented generation (RAG): A first call rewrites the user's question into a search query, retrieval runs in code against a vector database, and a second call answers from the retrieved passages.
  • Number plate recognition: A YOLO model detects license plates in video frames, and each cropped plate goes to GPT-4o mini, which reads the characters as text, so the language model only ever sees plates.

Ultralytics Platform Agents builds this kind of chain on a visual canvas. Its YOLO → VLM Monitor template runs YOLO26 on an image, passes the detections to a Gate block that continues only when at least one person is found, and sends the image with the upstream results to a vision-language model in an LLM block. The same chain in Python, up to the prompt for the second link:

from ultralytics import YOLO

model = YOLO("yolo26n.pt")

# Link 1: detect objects and list their class names
results = model("https://ultralytics.com/images/bus.jpg")
result = results[0]
names = [result.names[int(c)] for c in result.boxes.cls]

# Gate: call the language model only when a person is present
if "person" in names:
    # Link 2: build the next prompt from link 1's output
    prompt = f"Objects detected: {', '.join(names)}. In one sentence, describe what the people are doing."
    print(prompt)

The printed prompt carries the object detection results into the next call, and an image with no person never reaches it.

  • Chain-of-thought prompting: Asks one model call to write out its reasoning steps. A prompt chain spreads the steps across several calls, with code in between.
  • AI agents and ReAct prompting: The model chooses its next action and decides when it is done. In a chain, the developer fixed the steps in advance, which makes chains easier to test and their cost predictable.
  • Agentic workflows: The broader family of LLM systems built from multiple steps and tools, which includes chaining alongside routing, parallel calls, and orchestrator-worker setups.
  • Prompt engineering: The craft of writing each individual prompt. Chaining is a design decision about how many prompts to use and how data flows between them.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning