Multi-Agent Role-Playing Harness: Principles and Practice
This article explains how a multi-agent role-playing Harness works and how to build one. It covers the context assembly loop, why per-role isolated runtimes hit memory limits, and three core modules: loop scheduling, prefix caching, and a context compression pipeline. It also outlines how to connect the Harness to general cloud capabilities and offers a practical checklist for keeping roles separated at low resource cost.
1. The Problem: Roles That Feel Like One Person
A common approach in multi-agent conversation is to have the same large language model play different roles in turn within a single conversation. In practice, the problem shows up quickly: the roles are not separated enough. Their language style and way of thinking are highly similar, so it looks like one person imitating many. For scenarios such as novel writing, where role differentiation is critical, this distortion is almost fatal.
A more practical constraint is that only a limited number of large models are available, and the style differences between them are not enough to support true “personality isolation.” As a result, many people turn to a self-built solution: writing their own scheduling layer, which is the Harness discussed in this article. Once you start implementing it, the mystery around the Harness quickly disappears, because it is essentially not complicated.
2. What a Harness Actually Is: Demystifying It
The core actions of a Harness are just three steps:
- Assemble the context: the system prompt (the role persona), the conversation history, and any external information to inject.
- Send this context to the large model and get the reply for the current turn.
- Append the new reply to the context and enter the next loop iteration.
A more direct analogy: you copy the full chat log with a certain role, send it to the model together with that role’s persona description, and ask, “What would this role say next?” After the model answers, you splice this turn’s result back into the context and continue asking for the next line.
The key point is that the front-end user sees a clean, continuous conversation, while behind the scenes each turn actually sends the ever-growing full context. Understanding this is the prerequisite for understanding the caching and compression mechanisms that follow.
Illustrative pseudocode:
context = [system_prompt(role_A), system_prompt(role_B)]while True:reply = call_model(context) # general large model APIcontext.append(reply)if should_compress(context):context = compress(context) # third-party compression pipelinedispatch(reply) # hand off to the corresponding role/front end
3. Resource Bottlenecks of Early Approaches and Selection Lessons
The most common early approach was to create a separate profile for each role in a runtime environment that supports multiple configuration profiles, assigning each role its own persona, memory, and file space to achieve isolation of capability and context. This works when there are only a few roles, but it quickly hits a bottleneck: each profile keeps hundreds of megabytes of memory resident, and once there are many roles, even a machine with 24 GB of memory cannot handle it.
This yields a general lesson: the cost of a multi-role system lies not only in model calls but also in the architectural choice of “one resident runtime per role.” A lighter route is to demote roles to “configuration data” rather than “independent processes/profiles,” and have a unified scheduling loop assemble the context on demand. This way, memory usage is largely decoupled from the number of roles.
Another observation worth recording is that when roles are given capabilities beyond role-playing (for example, reading and writing files or modifying other roles’ configurations), they may overstep and interfere with each other. This suggests that role capability boundaries must be explicitly constrained at the architecture level, rather than relying on the prompt’s self-discipline.
4. Three Core Modules of a Self-Built Harness
1. Loop Scheduling and Context Assembly
The scheduler decides which role speaks in each turn and what information to inject. The context usually includes each role’s system prompt, a recent conversation window, and any necessary long-term memory summary. The assembly order affects model performance, so it is recommended to keep a stable structure to favor cache hits.
2. Cache Reuse: Driving Repeated Cost Close to Zero
Since most of the content sent in each turn overlaps with the previous turn, the parts with an identical prefix can hit the cache. In practice, the cache hit rate can often reach around 99%—this is exactly the value of the caching mechanism: you make the conversation “loop,” the repeated prefix is reused, and the marginal cost is extremely low. In engineering terms, you should keep the context prefix stable (put the unchanging system prompt first, then the history, and finally the new content for the current turn) and avoid frequently rewriting the earlier content, which would invalidate the cache.
3. Context Compression Pipeline
As the conversation grows longer, the context will inevitably overflow. At this point, you connect an independent compression pipeline that summarizes the earlier history, keeps only key information and role state, and then sends the compressed context to the model. Compression must balance “information fidelity” and “controllable length.” It is recommended not to compress stable information such as role settings, and to apply rolling summaries only to the interaction history.
5. Connecting to General Cloud Capabilities
Beyond the modules above, a complete Harness also needs an adapter layer to communicate with the large model API, abstracting away details such as authentication, retries, and rate limiting. To make it an operable service, you can leverage general cloud product categories: use a container platform to host the scheduling service, use object storage to save role configurations and memory snapshots, use a message queue for asynchronous decoupling between roles, and use logging and monitoring/alerting services to track cache hit rate, context length, and call latency. General cloud product categories from providers such as Baidu AI Cloud can all serve as the foundation for this kind of architecture.
6. Practical Recommendations Checklist
- Roles as data: describe personas with configuration, and avoid one resident runtime per role.
- Stable prefix: keep the context structure fixed to maximize cache hits.
- Explicit boundaries: constrain role capabilities with architecture to prevent unauthorized cross-modification.
- Rolling compression: do not compress stable information; apply rolling summaries to historical information.
- Observability: build monitoring for cache hit rate, context length, and per-turn latency.
In summary, the “sophistication” of multi-agent role-playing does not come from some mysterious framework, but from solid engineering of context assembly, looping, caching, and compression. Get these things right, and you can obtain a stable, scalable self-built Harness with controllable role separation at extremely low resource cost.
