Skip to main content

Overview

Chains in PromptL allow you to break complex workflows into smaller, manageable steps. Each step generates a response, which can then be used in subsequent steps. This approach improves the model’s ability to perform complex tasks and provides greater control over dynamic conversations. With Chains, you can:
  • Process tasks incrementally to guide the model step-by-step.
  • Store and reuse intermediate results dynamically.
  • Customize step-specific configurations for more efficient execution.
  • Isolate steps to minimize context overhead or confusion.

Syntax

Define steps in your prompt using the <step> tag. The engine pauses after each step, waits for the model’s response, and adds it as an assistant message before continuing.

Basic Syntax

Step with Custom Configuration

Override the default configuration by adding attributes to the <step> tag:

Advanced Features

Storing Step Responses

You can store the text response of a step in a variable using the as attribute. This allows you to reuse the response in later steps or logic blocks.

Parse Step Responses as JSON

The response of a step will be automatically parsed as JSON if the JSON output schema is defined.
Learn more about JSON Output.

Storing Raw Messages

Some providers will return additional metadata along with the response. To store the entire message object, instead of just the text response (e.g., role, content, and additional metadata), use the raw attribute:
The raw response will return an object with the full message details, which contains the role, content, and other metadata provided by the model. The content attribute will always be defined as an array of content objects, which can include text, images, tool calls and any other types of content returned by the LLM.

Isolating Steps

Use the isolated attribute to prevent a step from inheriting context from previous steps. This can reduce unnecessary costs or confusion for the model.
In this example, the final step does not need to conside the full texts used in previous steps, so isolating the first two steps can help reduce context overhead, resulting in cheaper and more efficient processing.

Limiting the number of steps

This feature is only available on the Latitude platform.
Latitude automatically applies a maxSteps limit of 20 to all prompts with configuration. This helps prevent infinite loops or excessive processing in long chains when creating complex workflows with steps within loops. You can customize this limit by explicitly setting the maxSteps attribute on the main configuration section:
Read more about this configuration in the Latitude Prompt Configuration guide.

Real-World Use Cases

Multi-Step Workflow

Chains are ideal for breaking down tasks like:
  1. Analyzing data.
  2. Generating intermediate results.
  3. Combining results for a final output.

Decision Trees

Use logic to adapt workflows based on intermediate results:

Implementation

To execute chains, use the Chain class. The chain evaluates the prompt step-by-step, waiting for the model’s response at each step. To run a step, execute the step method of the chain instance. The first time step is called, it should not include any arguments. Subsequent calls must always pass the model response message from the previous step.

Example: Using the Chain Class


Debugging Chains

  1. Log Intermediate Steps:
    • Use the raw attribute to inspect full responses for debugging.
  2. Handle Errors Gracefully:
    • Implement fallback logic for unexpected responses or failures.
  3. Test Edge Cases:
    • Ensure your chains handle empty inputs, invalid configurations, or incomplete data.

Summary

Chains and Steps provide powerful tools for breaking complex tasks into manageable parts. With features like custom configurations, variable storage, and step isolation, you can design robust, dynamic workflows tailored to any use case.