SALE! Use code blackfriday for 40% off everything!
Hurry, sale ends soon! Click to see the full catalog.

Building a Multi-Agent Verification Loop in Python with Small Open Models

In this article, you will learn how to build a Proposer-Critic-Judge verification loop in Python with a single small open model, and how to measure what the extra accuracy costs in latency and tokens.

Topics we will cover include:

  • How the Proposer, Critic, and Judge roles divide the work of generating, verifying, and finalizing an answer.
  • How persona multiplexing lets one 4-bit quantized Qwen2.5-7B-Instruct model play all three roles on a Colab T4 GPU.
  • How to instrument each step to compare the loop’s latency and token usage against a single-agent baseline.

Building a Multi-Agent Verification Loop in Python with Small Open Models

Introduction

The AI landscape is dominated by multi-agent frameworks, with prominent actors such as AutoGen and LangGraph at the center of the stage. This brings the promise of high-accuracy solutions, but sequentially stacking LLMs (large language models) introduces an issue: a “token tax”, in the form of computational cost and compounding latency that worsens with each node added to your framework’s graph.

Thus, the challenge is not only about wiring agents together, but rather proving that the coordination overhead pays off. This article shows the building blocks to construct a multi-agent verification loop, concretely under a Proposer-Critic-Judge approach. We will run a Python notebook aided by a small open-source language model, instrumenting it to measure the extent to which latency losses are worth accuracy wins.

The Proposer, Critic, Judge Architecture

The Proposer, Critic, Judge architecture is a design pattern that has gained relevance in multi-agent systems. It is characterized by separating three cognitive roles or steps:

  • Proposer: this is the agent that receives the raw user prompt or requested task and acts as the domain expert that generates a preliminary, unverified solution or response.
  • Critic: the rigorous member of the team. It is responsible for verifying the proposer’s solution to the user prompt. The critic is also tasked with calculating the answer independently beforehand, so it can compare it to the proposer’s.
  • Judge: the judge is like a “final executive” that takes the prompt, the proposer’s response, and the subsequent critique to synthesize everything into a final, likely correct answer.

Python Implementation

Running the three models underlying the agents simultaneously has massive memory requirements, even on Google Colab’s Nvidia T4 GPU environment. To avoid a nearly instant out-of-memory error while loading the three models, we will use persona multiplexing: this consists of loading one highly capable baseline model, such as Qwen2.5-7B-Instruct, with 4-bit quantization. After that, its system prompts — infused with each of the three aforesaid roles — are dynamically swapped to switch the model’s behavior.

Before that, you may need to pip install some libraries — if running this on Google Colab, you will only need to explicitly install bitsandbytes, but be sure to switch the runtime to a GPU:

The first step is to download our target model and shrink it using 4-bit quantization to fit it on Colab’s GPU.

We then build a custom function called generate_and_measure(). This function acts as a measuring tape for our use case, interacting with the model while it secretly runs a token counter and a stopwatch behind the scenes. In other words, the exact cost of each prompt will be carefully tracked.

The next step is where we define the three different “hats” to be worn by our single model, i.e. the three system messages for the Proposer-Critic-Judge protocol.

Now it’s time to wire the three system messages together into an automated assembly workflow. After that, we finalize by performing a targeted test consisting of a tricky question:

Output:

Analysis of Results and Conclusion

It seems the initially erroneous response was successfully corrected. In small models, zero-shot scenarios often lead them to connect words like “100” and “half” and lazily assume that the answer requires simple, proportion-based math like 100/2 = 50. Meanwhile, our multiplexed three-tier architecture handled the whole task elegantly and effectively, by emulating a multi-agent verification and correction loop, as shown previously in the code. This is largely thanks to the critic, which is inherently asked to think step-by-step before delivering a judgment.

In terms of the cost of the accuracy gained, there’s an engineering trade-off. Passing a simple factual query through the loop would mean a waste of compute, but for complex-logic scenarios involving reasoning, the Proposer-Critic-Judge loop makes the most sense: not because of efficiency, but because of robustness. We had to pay a much higher token tax and wait extra seconds, but the cognitive gain turned a certain failure into a verified success.

 
 

No comments yet.

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.