DEV Community

Railtracks
Railtracks

Posted on Edited on AI-assisted

Agent splitting

Ask one model to review code for everything at once, and you get one model's idea of what matters.

So we tried splitting a code review into three jobs. One model only looks for security problems, one only for speed, and one only for readability. All three run at the same time, and a fourth model merges their notes into one review.

This is the sectioning pattern, one of the two kinds of parallelization in lecture 6 of Stanford's CS329Z. Below is the example built with Railtracks, with the real numbers from the run.

Sectioning vs. voting

Part 3 of this series was voting: we asked three models the same question ("is a hot dog a sandwich?") and took the majority.

Sectioning uses the same parallel calls for a different purpose:

Voting Sectioning
What each model gets The same task A different part of the task
How you combine results Count them Merge them
What it buys you More confidence in one answer Focused attention on each part

The code under review

A small function with one problem for each reviewer:

def get_user(db, name):
    rows = db.execute(f"SELECT * FROM users WHERE name = '{name}'").fetchall()
    for row in rows:
        if row[1] == name:
            return row
Enter fullscreen mode Exit fullscreen mode
  • Security: the query is built from user input with an f-string, so it allows SQL injection.
  • Speed: it loads every matching row with fetchall(), then loops over them in Python.
  • Readability: row[1] is an index with no name, and the loop repeats a check the query already did.

The code

import asyncio

import railtracks as rt


def make_reviewer(name, llm, focus):
    return rt.agent_node(
        name,
        llm=llm,
        system_message=f"You review Python code for {focus} only. Ignore everything else. One sentence.",
    )


reviewers = [
    make_reviewer("Security", rt.llm.AnthropicLLM("claude-sonnet-5-5"), "security problems"),
    make_reviewer("Speed", rt.llm.OpenAILLM("gpt-6-luna"), "performance problems"),
    make_reviewer("Readability", rt.llm.GeminiLLM("gemini-3.8-flash"), "readability problems"),
]
Editor = rt.agent_node(
    "Editor",
    llm=rt.llm.OpenAILLM("gpt-6-luna"),
    system_message="Merge these code review notes into one review. Most serious first. At most 3 bullets.",
)


@rt.function_node
async def review(code: str) -> str:
    # All three reviewers run at the same time.
    calls = [rt.call(reviewer, code) for reviewer in reviewers]
    responses = await asyncio.gather(*calls)

    notes = ""
    for reviewer, response in zip(reviewers, responses):
        notes += f"{reviewer.name()}: {response.text}\n"
    print(notes)

    response = await rt.call(Editor, notes)
    return response.text


flow = rt.Flow(name="Code Review", entry_point=review)
print(flow.invoke(CODE))  # CODE is the get_user function above, as a string
Enter fullscreen mode Exit fullscreen mode

Here's what each part does:

  • make_reviewer builds an agent_node with a one-job system prompt. The only differences between the three reviewers are the model and the focus.
  • calls is a list of rt.call(...) coroutines. Nothing runs yet.
  • asyncio.gather(*calls) starts all three at once and waits until every one is done. responses comes back in the same order as reviewers, so zip pairs each reviewer with its note.
  • The editor is a normal rt.call that runs after the reviewers. It needs all three notes, so it can't start earlier.

What happened

Each reviewer found the problem it was given:

Reviewer Model Note
Security claude-sonnet-5-5 The f-string interpolates name directly into the SQL query, creating a SQL injection vulnerability. Use a parameterized query.
Speed gpt-6-luna Use fetchone() instead of fetchall() and the Python loop.
Readability gemini-3.8-flash Intent is obscured by redundant Python-level filtering and the ambiguous magic index row[1].

The editor turned those into three bullets, with security first:

  • Security: name is interpolated directly into the SQL query, allowing SQL injection. Use a parameterized query, such as db.execute("SELECT * FROM users WHERE name = ?", (name,)).
  • Performance: Use fetchone() instead of fetchall() and a Python loop to avoid loading and scanning all matching rows.
  • Readability: Remove the redundant Python-level filtering and replace the ambiguous row[1] access with a named column or clearly documented field.

The numbers

These come from the run's trace in the Railtracks visualizer (railtracks viz --beta):

Step Model Time Cost
Security claude-sonnet-5-5 1.7s $0.0010
Speed gpt-6-luna 2.7s $0.00006
Readability gemini-3.8-flash 5.6s $0.0019
Editor gpt-6-luna 2.5s $0.0001
Whole review 8.2s $0.0031
  • The three reviewers took 5.6s together. That's the slowest one alone. Run one after another, the same three calls add up to 10.0s.
  • The whole review took 8.2s: 5.6s for the reviewers, then 2.5s for the editor.
  • Running in parallel doesn't change the cost. You make the same calls either way. It only changes how long you wait.

One surprise: Gemini Flash was both the slowest and the most expensive step. It wrote 495 output tokens for a one-sentence note, while the other two wrote 80 and 98. That's most likely hidden thinking tokens. I didn't dig into it, but if you use a thinking model for a small job, check its output token count.

Things to watch

  • The slowest reviewer sets the pace. Here, Gemini took more than twice as long as the next one. If one section is always slow, give it a faster model or a narrower job.
  • One failure stops the batch. By default, asyncio.gather raises the first exception it gets. If you'd rather keep the notes that did come back, pass return_exceptions=True and check each response.
  • Sections should really be independent. Sectioning works when each part can be done without seeing the others. If one reviewer needs another's output, that's a chain, not a section.
  • I didn't compare against one model doing the whole review. It might find the same three problems. The case for sectioning is that each model only has one thing to pay attention to, and you can pick a different model for each part.

When to use it

Sectioning fits when a task splits into parts that don't depend on each other, and each part benefits from its own prompt or model. Code review, checking a document against several policies, and pulling different kinds of facts from the same text all fit.


Railtracks is open source: github.com/RailtownAI/railtracks.
This is part of a series on Stanford's CS329Z agent patterns. Lecture 6 Slides

Questions, or want to see a pattern next? Come hang out in our Discord: https://discord.gg/2JUb2TxjJv

Top comments (0)