Skip to content
← All codeCode sample

five-agent-control-flow-patterns

Five Strands Agents control-flow patterns over one fake web shop, each with the failure it causes: a single loop, a validated router, a bounded checkout graph, a planner with a fresh executor per step, and a supervisor with typed handoff tools.

View on GitHub

ShopSite is a fake web shop whose open_url and click tools return a full page snapshot of about 5,100 characters, as a browser tool does. All five patterns run against it.

build_single_loop() is one Strands Agent with Limits and a SlidingWindowConversationManager. On context overflow it trims the task first unless pin_task is set. Router makes one tool-less classification, checks the label against the Route enum and escalates anything else to Route.HUMAN. build_checkout_graph() is a GraphBuilder checkout. find_product and fix_cart are agents, while verify_cart and place_order are FunctionNodes that run plain Python. The repair cycle is bounded by set_max_node_executions.

PlannerExecutor keeps a tool-less planner that replies NEXT: or DONE:. It builds a fresh executor Agent for each step and passes back a report of at most 300 characters, so the planner never sees a snapshot. build_supervisor() wires workers with as_tool(); build_typed_supervisor() replaces them with typed tools, find_products(query, max_price) and buy_product(sku, max_price), that reject a call without the limit or over it.

The 20 tests reproduce each pattern's failure mode: the trimmed task, the substring router that fires two branches, the unbounded repair cycle, and the supervisor that drops a price limit. They run offline with a scripted fake model and never call a provider.

Bash
git clone https://github.com/tensorupai/tensorup-examplescd tensorup-examples/five-agent-control-flow-patterns && uv sync && uv run pytest

To run a pattern live, put OPENAI_API_KEY or ANTHROPIC_API_KEY in .env, then run TENSORUP_LIVE=1 uv run python -m controlflow planner (or loop, router, graph, supervisor). TENSORUP_PROVIDER picks openai (default) or anthropic, and TENSORUP_MODEL overrides the default model.

To use it in your own agent, replace ShopSite and make_tools() with your own tools, keep Limits on every agent call, and move every step you can describe in advance into a FunctionNode. For planner/executor, keep report_chars small so observations stay inside the executor.