Fine-Tuning LLMs for Multi-Agent Orchestration in Code Generation

cosine · Tech · 2025 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: enterprise coding agents

Cosine needed coding agents that could perform long-horizon software engineering work for regulated enterprises with large codebases, niche stacks, and deployment constraints where frontier models or hosted egress-heavy systems were not always viable.

Constraints:

Architecture

Cosine built Genie as a specialized multi-agent coding system. A user submits a large coding task through web or CLI. An orchestrator model breaks the request into subtasks, calls worker agents as tools, inspects their chain-of-thought and git diffs, and either asks for follow-up work or completes the task with a reviewable pull request. Training starts with strong frontier software-engineering trajectories, distills successful trajectories into smaller orchestrator and worker models, then improves workers with supervised fine-tuning and reinforcement learning. For constrained deployments, multi-LoRA adapters let the same base model switch between orchestrator and worker roles while trajectories remain stored and auditable.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  developer-web-and-cli-surfaces[Developer web and CLI surfaces]
  orchestrator-model[Orchestrator model]
  worker-agents[Worker agents]
  sandbox-code-execution[Sandbox code execution]
  production-scaffolding-and-tools[Production scaffolding and tools]
  distillation-and-rl-training-pipeline[Distillation and RL training pipeline]
  multi-lora-adapter-runtime[Multi-LoRA adapter runtime]
  trajectory-store[Trajectory store]
  audit-playback[Audit playback]
  reviewable-pull-request[Reviewable pull request]
  developer-web-and-cli-surfaces --> orchestrator-model
  orchestrator-model --> worker-agents
  orchestrator-model --> trajectory-store
  orchestrator-model --> multi-lora-adapter-runtime
  worker-agents --> sandbox-code-execution
  worker-agents --> production-scaffolding-and-tools
  worker-agents --> trajectory-store
  production-scaffolding-and-tools --> distillation-and-rl-training-pipeline
  distillation-and-rl-training-pipeline --> orchestrator-model
  distillation-and-rl-training-pipeline --> worker-agents
  distillation-and-rl-training-pipeline --> trajectory-store
  multi-lora-adapter-runtime --> orchestrator-model
  multi-lora-adapter-runtime --> worker-agents
  trajectory-store --> audit-playback
  orchestrator-model --> reviewable-pull-request
  reviewable-pull-request --> developer-web-and-cli-surfaces
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Developer web and CLI surfaces Accept large asynchronous tasks or IDE-adjacent collaborative coding requests from developers. Orchestrator model, Reviewable pull request
Orchestrator model Breaks user tasks into subtasks, calls worker agents as tools, reviews worker outputs, and decides whether to iterate or finish. Worker agents, Trajectory store, Multi-LoRA adapter runtime, Reviewable pull request
Worker agents Traverse codebases, use tools, edit code, run sandboxed work, and return chains of thought and diffs. Sandbox code execution, Production scaffolding and tools, Trajectory store
Sandbox code execution Lets the agent test solutions iteratively while solving software engineering tasks. Worker agents
Production scaffolding and tools Provides the same tool interface during training and production to tightly couple model behavior with the runtime. Worker agents, Distillation and RL training pipeline
Distillation and RL training pipeline Uses strong-model trajectories, supervised fine-tuning, reinforcement fine-tuning, and verifiable rewards to train smaller agents. Orchestrator model, Worker agents, Trajectory store
Multi-LoRA adapter runtime Switches the same base model between orchestrator and worker personalities for constrained on-prem deployments. Orchestrator model, Worker agents
Trajectory store Stores orchestrator and worker trajectories for audit playback and future training signal. Distillation and RL training pipeline, Audit playback
Audit playback Allows enterprises to replay and inspect what happened across orchestrator and worker actions. Trajectory store
Reviewable pull request Packages final code changes into the developer's existing review workflow. Developer web and CLI surfaces

Data flow

A developer task enters the web or CLI product and is interpreted by the orchestrator. The orchestrator creates subtasks, assigns them to worker agents with tool and codebase access, receives worker chain-of-thought and git diffs, judges whether the changes solve the original intent, and either spawns follow-up subtasks or returns a reviewable pull request. Training data flows from strong RL-post-trained frontier models solving software-engineering problems, through stored successful trajectories, into supervised fine-tuning datasets for smaller orchestrator and worker models, with RL and real-world merged-PR trajectories reinforcing later runs.

“The orchestrator is never actually on the ground solving the problem. The orchestrator will always delegate the actual doing of things to a sub-agent, it merely coordinates them.”transcript, stated
“the tool response is the sub-agents work, essentially the worker's work. And that can, in our case, that that looks like chains of thought and also get diffs”transcript, stated

Why this architecture

Cosine chose multi-agent specialization because small models were the only feasible option in some regulated or air-gapped deployments, but monolithic small models struggled with long-horizon coding. Orchestration decomposed work into smaller subtasks, multi-LoRA reduced hardware footprint, and distillation/RL transferred frontier-model software-engineering behavior into deployable smaller models.

“Small models are the only way that you can go and putting them together in a multi-agent system.”transcript, stated
“Building an orchestrator fills that intelligence void, essentially allows these large long horizon tasks to be broken down into smaller, manageable subtasks”transcript, stated

Models used: Llama 3.3 70B · gpt-oss-120b · Kimi K2 Thinking · DeepSeek V3.1 · custom frontier coding models · small specialized worker and orchestrator models

Scale

Alternatives considered (roads not taken)

use one monolithic worker model

Rejected because: the base worker alone underperformed the multi-agent setting on SWE-Lancer and lacked the orchestrator's review and iteration loop.

“that is the base worker on its own. That is literally no orchestrator, no nothing, just give the problem to the worker and see how it does.”transcript, stated

deploy large frontier models everywhere

Rejected because: regulated and air-gapped customers often could not deploy or use those models for origin, GPU, isolation, or policy reasons.

“particularly for enterprises that could never dream to deploy or use an O3 for all the various reasons that surrounds that.”transcript, stated

manually create supervised fine-tuning trajectories

Rejected because: manual trajectory creation was too expensive at scale compared with distilling successful trajectories from stronger models.

“it took a team of five people about three months manually creating trajectories and labeling how we would solve these problems.”transcript, stated

skip supervised fine-tuning and go straight to RL

Rejected because: weak base models needed too much RL time to learn useful behavior from scratch.

“If those models are already quite bad and struggle to solve these problems effectively, you are gonna need a lot of time in an RL environment”transcript, stated

Patterns

multi-agent-orchestration (agent-architecture)

Cosine uses an orchestrator model to decompose coding tasks, delegate to worker agents, inspect their outputs, and continue iteration until the task is complete.

“the orchestrator agent is taking global tasks from a user breaking that down into subtasks and doling it out into these specific specialized subtask agents”transcript, stated

tool-use-orchestration (agent-architecture)

Sub-agents are exposed to the orchestrator as tool calls, and the tool response contains worker reasoning and diffs.

“So it calls sub-agents as tools. If any of you have ever done any tool calling with LLMs, it's just a tool essentially.”transcript, stated

model-distillation (model-operations)

Cosine uses trajectories from strong frontier coding models as supervised fine-tuning data for smaller orchestrator and worker models.

“we can use that very strong model to create trajectories that we can then use to distill through supervised fine-tuning into these smaller models.”transcript, stated

fine-tuning-adaptation (model-operations)

The worker model is first moved into a software-engineering-agent frame with supervised fine-tuning before reinforcement learning.

“we want to get this model into the rough frame of mind of a software engineering agent. And the quickest way to do that to start with before you start doing RL, is just to do a supervised fine-tune.”transcript, stated

observability-tracing (operations)

Cosine stores orchestrator and worker trajectories so enterprises can replay agent behavior and understand why actions happened.

“having the auditable trajectories for the orchestrator and all of the workers allows you to essentially play back exactly what happened”transcript, stated

human-in-the-loop-gate (human-oversight)

The agent's terminal output is a reviewable pull request that fits into existing developer review workflows.

“the agent produces a reviewable pull request that can be, you know, reviewed and iterated on and slots right into developers existing software development life cycles.”transcript, stated

Tradeoffs

Use a multi-agent architecture instead of a single small model.

Chose orchestrator plus specialized workers over one monolithic worker model.

Rationale: The orchestrator decomposes long-horizon tasks, reviews worker outputs, and improves SWE-Lancer performance without adding GPUs.

“we have no more GPUs running, we have the same number of GPUs running, we're just swapping out a LoRA adapter and then we get, you know, 31% more improvement.”transcript, stated

Use multi-LoRA adapters for constrained deployments.

Chose adapter-swapped orchestrator and worker personalities on one base model over separate large models for each role.

Rationale: Adapter swapping reduces GPU footprint while preserving role specialization for on-prem environments.

“we essentially have different personalities encoded in into each adapter that we can just in time swap out.”transcript, stated

Distill trajectories from stronger models.

Chose frontier-model trajectory distillation over manual trajectory creation for every small-model dataset.

Rationale: Manual trajectory construction was possible but expensive and slow, while distillation transferred stronger model abilities into smaller models.

“So distillation is a huge tool to bring the abilities of these frontier models into these smaller models.”transcript, stated

Store and replay trajectories.

Chose fully auditable orchestrator and worker traces over opaque agent execution.

Rationale: Regulated enterprises needed to understand why an agent took each action, and stored successful trajectories also create training signal.

“importantly for these enterprises, everything that is done is stored and fully auditable. And this is one of the things that we've put a lot of thought and time into”transcript, stated

Use reviewable pull requests as the delivery object.

Chose developer review workflow integration over unreviewed direct code changes.

Rationale: PR output lets developers review and iterate on agent changes inside familiar software development lifecycle practices.

“the agent produces a reviewable pull request that can be, you know, reviewed and iterated on and slots right into developers existing software development life cycles.”transcript, stated

Gotchas (what bit them)

Small models struggled with long-horizon coding tasks in constrained enterprise environments.

Root cause: They were not always trained for long trajectories or tool calling, and some attention mechanisms could make them forget earlier actions.

Resolution: Cosine used an orchestrator to decompose tasks, keep workers iterating, and fill intelligence gaps in smaller models.

“that model can feel like it has amnesia, it can call a tool, do a bunch of stuff afterwards and then actually call the same tool again with the same input”transcript, stated

Multi-agent systems can fail catastrophically when coordination breaks down.

Root cause: Sub-agents can get stuck, or the orchestrator can become incoherent and unable to recover the whole system.

Resolution: The architecture emphasizes specialized orchestrator training, stored trajectories, and orchestrator review of worker diffs.

“It's possible to encounter doom loops where like sub-agents get stuck or even worse, where the orchestrator becomes totally incoherent”transcript, stated

Independent orchestrator and worker training can produce policy drift.

Root cause: The two model roles may learn to think about and operate on problems differently.

Resolution: Cosine treats orchestrator and worker training as separate disciplines and experiments with training the whole system online.

“there is the risk that you get like significant policy drift between those two models, meaning they think about problems and operate in very different ways”transcript, stated

Poor context management degrades smaller agent models.

Root cause: Context pollution makes long trajectories harder for smaller models to reason over.

Resolution: The orchestrator/worker split scopes context per task and stores trajectories for audit and reinforcement signal.

“if context isn't managed well, you can end up in situations where your context is polluted significantly and that degrades model performance quite significantly”transcript, stated

Skipping SFT before RL leaves weak models stuck for too long.

Root cause: The base model's initial software-engineering ability limits how quickly RL can discover good trajectories.

Resolution: Cosine uses SFT to put the model into a software-engineering-agent frame before online RL.

“So the SFT sort of shortens that process. Then we start the actual RL process”transcript, stated

Evaluation

Cosine evaluates coding agents on SWE-Lancer, stores successful trajectories such as merged PRs as future training signal, uses verifiable rewards for software-engineering problems, and compares base-worker performance against the multi-agent setting.

Metrics: 35% reduction in average trajectory length from training/runtime scaffolding coupling · 3x latency boost versus a generic multi-agent framework · 60% GPU footprint reduction for on-prem deployments · about 20% fewer errors in final code · 31% SWE-Lancer improvement from multi-agent setup versus base worker · deployable on as few as about four H100 GPUs

“SWE-Lancer is a benchmark that measures a coding agent's ability to do economically valuable work.”transcript, stated
“if a PR gets merged for instance, that's a good sign that that task went well. We can use that as training data for subsequent runs”transcript, stated

Outcomes

The system targets highly regulated enterprise customers and constrained deployments, including cloud, VPC, and fully air-gapped on-premise hardware, with a design goal of running on as few as about four H100 GPUs.

“we have models where there is no egress from that customer's VPC, we're then able as a business to target highly regulated industries.”transcript, stated
“fine-tuning these smaller models in this way, you can often outperform like the large monolithic models.”transcript, stated