Blog/Learn Agentic AI Development/Execute Steps and Pass Results Between Them

Chapter 11 / 16 min read

Execute Steps and Pass Results Between Them

execute_step, results keyed by step ID, source_step references and resolve_arguments().

Execute steps and pass results between them

Planning tells us which step is eligible. The next question is how the application runs the step and stores its result so later steps can use it.

1. The problem with manually copying values

Consider this task:

  1. Add 10 and 20.
  2. Multiply the result by 5.

A manual experiment passed the first result into the second step:

Code
results = {}

step = TaskStep(
    step_id=1,
    tool_name="calculate_addition",
    arguments={"a": 10, "b": 20},
)

execution_results = execute_step(step)
print("Execution results:", execution_results)

step_results = execution_results.get(step.step_id)

if step_results and step_results.get("success"):
    results[step.step_id] = step_results.get("result")


step = TaskStep(
    step_id=2,
    tool_name="multiply_numbers",
    arguments={
        "a": results.get(1),
        "b": 5,
    },
)

execution_results = execute_step(step)
print("Execution results:", execution_results)

step_results = execution_results.get(step.step_id)

if step_results and step_results.get("success"):
    results[step.step_id] = step_results.get("result")

print("Execution result:", results)
print("Final status:", step.status)

The observed output was:

Code
Execution results: {
  1: {
    'success': True,
    'operation': 'addition',
    'inputs': {'a': 10.0, 'b': 20.0},
    'result': 30.0
  }
}

Execution results: {
  2: {
    'success': True,
    'operation': 'multiplication',
    'inputs': {'a': 30.0, 'b': 5.0},
    'result': 150.0
  }
}

Execution result: {1: 30.0, 2: 150.0}
Final status: StepStatus.COMPLETED

This proves the data flow: Step 1 produces 30; Step 2 consumes 30 and produces 150.

But manually writing results.get(1) into every later step does not scale. We need a generic resolver.

2. Store successful outputs by step ID

The results dictionary maps a step ID to its output value:

Code
results = {
    1: 30.0,
    2: 150.0,
}

Why use the step ID as the key? It gives later steps a stable reference to the output of a specific earlier operation.

The executor's full return value is also useful:

Code
execution_results = {
    2: {
        "success": True,
        "operation": "multiplication",
        "inputs": {"a": 30.0, "b": 5.0},
        "result": 150.0,
    }
}

The orchestration layer stores the successful result value in the compact results dictionary, while the full execution record can retain the inputs and other metadata.

3. Represent a reference instead of a literal

The next experiment introduced an argument dictionary:

Code
arguments = {
    "a": {"source_step": 1},
    "b": 5,
    "c": {"source_step": 2},
}

The value {"source_step": 1} means: “Do not use this dictionary as a literal argument. Retrieve the output of Step 1 and use that value here.”

The value 5 is an ordinary literal. The key b should remain 5.

The first REPL exercise inspected the dictionary:

Code
a {'source_step': 1}
b 5
c {'source_step': 2}

Then it checked each value:

Code
for key, value in arguments.items():
    if isinstance(value, dict):
        print(f"reference found: {key} -> {value}")
        source_step_id = value.get("source_step")
        print("source_step_id", source_step_id)
    else:
        print(f"normal argument: {key} -> {value}")

Observed output:

Code
reference found: a -> {'source_step': 1}
source_step_id 1
normal argument: b -> 5
reference found: c -> {'source_step': 2}
source_step_id 2

This introduced two Python concepts:

  • dict.items() iterates over key/value pairs.
  • isinstance(value, dict) checks whether a value is a dictionary.

4. Resolve references using the results store

The next experiment populated the output dictionary with the two successful results:

Code
results = {
    1: 30.0,
    2: 150.0,
}

Then it resolved the argument references:

Code
resolved_arguments = {}

for key, value in arguments.items():
    if isinstance(value, dict):
        source_step_id = value.get("source_step")
        resolved_arguments[key] = results.get(source_step_id)
    else:
        resolved_arguments[key] = value

print(resolved_arguments)

Expected output for the example:

Code
{
    'a': 30.0,
    'b': 5,
    'c': 150.0
}

During testing, the literal argument b was initially missing from the output dictionary. The corrected loop above includes the else assignment so that literal and resolved arguments are both preserved.

5. Extract the loop into a reusable function

The resolver implementation is:

Code
def resolve_arguments(
    arguments,
    results,
) -> dict:
    resolved_arguments = {}

    for key, value in arguments.items():
        if isinstance(value, dict):
            source_step_id = value.get("source_step")
            print(
                f"reference found: {key} -> "
                f"{results.get(source_step_id)}"
            )
            resolved_arguments[key] = results.get(source_step_id)
        else:
            print(f"normal argument: {key} -> {value}")
            resolved_arguments[key] = value

    return resolved_arguments

This function is the first implementation of argument resolution. It has not yet been integrated into the complete execution loop.

6. What still needs to be hardened?

The first resolver is intentionally small. Before using it in a robust orchestrator, we should add explicit checks:

  • Does the reference contain a source_step key?
  • Does that step ID exist in the results store?
  • Did the source step actually succeed?
  • Is the source output compatible with the destination argument?
  • Should nested paths be supported, such as a particular field within a structured result?
  • What should happen if the reference points to a failed or skipped step?

For example, this version fails clearly when a prerequisite output is unavailable:

Code
def resolve_arguments(arguments: dict, results: dict) -> dict:
    resolved = {}

    for key, value in arguments.items():
        if isinstance(value, dict) and "source_step" in value:
            source_step_id = value["source_step"]

            if source_step_id not in results:
                raise ValueError(
                    f"Result for step {source_step_id} is unavailable"
                )

            resolved[key] = results[source_step_id]
        else:
            resolved[key] = value

    return resolved

This is an explanatory hardening example, not code tested in the original session.

7. The complete flow we were approaching

Code
Task plan
   |
   v
Validate step IDs and dependency references
   |
   v
Find steps whose dependencies completed
   |
   v
Resolve source_step references from results
   |
   v
Validate resolved arguments with the tool's Pydantic model
   |
   v
Execute registered Python function
   |
   v
Update step status and execution history
   |
   +---- success ----> Store output by step ID
   |
   +---- failure ----> Mark dependent steps blocked/skipped
   |
   v
Find the next ready steps

The natural next milestone is to wire the resolver into this entire cycle. That integration is not included here, so the proposed orchestration is not presented as completed code.