Code Migration with Agents: Rewrites Across Languages and Platforms

Rewriting code into another language or platform is the task agents do best: it has a reference source, a target stack, and the result is verified by tests. The browser-use case sets the scale: it rewrote the architecture, the library shrank by 90%, Python + Chrome DevTools instead of the old stack — quality up 50%, tasks 2–5 times cheaper. We break down the migration methodology: five steps, prompts, verification commands and task classes — from swapping requests for httpx to architectural rewrites.

Why migration is the ideal task for an agent

Migration has three properties that “just write an app from a description” lacks:

  • The source defines the behavior. Requirements don’t have to be invented — working code already answers the question “what is correct”. The agent doesn’t fantasize; it ports.
  • Tests provide an objective criterion. Equivalence is verified automatically: reference scenarios must produce the same results on the new stack.
  • Iterations are cheap. The “edit → test → compare” cycle takes minutes and doesn’t require rebuilding the project from scratch.

An agent in the “ported → ran the tests → fixed the discrepancies” cycle converges on a working result faster than a manual rewrite: the model doesn’t tire of routine porting, and the human steps in only at the points of divergence. The stability of such long cycles depends on the harness — if you don’t yet tell a “model” apart from a “shell”, start with the piece on what an LLM agent harness is.

The five-step method: from reference tests to the diff audit

  1. Reference tests first. Before the migration, freeze the source’s behavior: tests on the key scenarios, “input → output” examples. This is the readiness criterion — without it the agent will “translate”, and you won’t know what exactly broke. If coverage is weak, the agent’s first assignment is to fill in the tests on the source, even before the port.
  2. A migration PRD. The target stack, the constraints, what gets ported and what doesn’t. The method is the same as in coding: overview, technologies, stages — the full template is described in the breakdown of the PRD method for agents.
  3. The core first. The agent migrates the central scenario together with its tests — you verify equivalence against the reference. The periphery (utilities, rare branches, integrations) comes only after the core is confirmed.
  4. Review of architectural decisions. The agent ports the mechanics by itself; the decisions of “what is correct in the new language” are checked by a human — you need the target’s idioms, not a literal copy of the source.
  5. A final diff audit. A comparison of behavior: tests green, a manual run of the key scenarios, performance under a typical load.

All the work goes into a separate branch — that is the standard precaution: the agent migrates the core, you run the tests between the iterations.

git checkout -b migration/python-to-typescript
git diff main --stat        # the change footprint at a glance

What to pin down in the PRD before the start, so you don’t have to redo work:

  • the target stack and the tool versions — so the agent doesn’t guess;
  • the readiness criterion — “the reference tests pass on the target”;
  • what is not ported — deprecated features, dead code, questionable dependencies;
  • the order of the stages — core, periphery, integrations, documentation.

The migration prompt: PRD format and three rules

The task is phrased in PRD format. An example from practice — porting Python utilities to TypeScript in a single pass:

Agent prompt (PRD format):
1. Port utils/date_helpers.py to TypeScript (src/lib/date.ts).
2. Preserve behavior: the reference tests test_date_helpers.py —
   port them to vitest, all of them must pass.
3. Use dayjs instead of hand-rolled parsers.
4. DO NOT change the public API of the calls.
Acceptance: all reference cases pass on TypeScript.

Three rules from migration practice — candidates for permanent project rules:

  • “Minimal changes” — the agent ports, it doesn’t rewrite: no “while we’re at it” refactoring and no renames unrelated to the migration.
  • “Don’t change the public API” — it protects the calling code: signatures and contracts stay as they are, otherwise migrating one module breaks a dozen neighboring ones.
  • “Target idioms” — the agent is allowed to replace hand-rolled solutions with the standard libraries of the target language: dayjs instead of manual date parsers, built-in structures instead of simulated ones.

To avoid repeating these rules in every prompt, put them into the project’s memory file: how CLAUDE.md-based agent memory works is covered in a separate practice piece.

Migration classes: from requests to architectural rewrites

The task classes differ in complexity — and in the price of a mistake:

ClassExampleComplexityLesson
Language → languagePython → TypeScriptLow to mediumReference tests decide everything
Library → libraryrequests → httpxLowExplicitly allow replacement with equivalents
Architectural rewritebrowser-use: −90% of the libraryHighHuman on decisions, agent on porting
Platform → platformdesktop → webHighStaging: core → UI → integrations

The “platform → platform” class — moving a desktop application to the web — drags a paradigm shift along with it: direct file system access disappears, requests become asynchronous, the interface is assembled from components. Here the staging from the table is not a recommendation but a requirement: first the core with the logic and the tests, then the interface layer, and the integrations with the environment last. The agent handles each stage on its own; mixing stages in one prompt is a typical cause of failure.

Library → library: requests to httpx

The most frequent and simplest class — replacing one library with another. The mechanics are transparent:

import requests
r = requests.get(url, headers=headers)      # before
r.raise_for_status()
data = r.json()

import httpx
with httpx.Client(timeout=10) as client:    # after
    r = client.get(url, headers=headers)
    r.raise_for_status()
    data = r.json()

The pitfall the agent must call out: requests has no default timeout (a request waits forever), while httpx defaults to five seconds — behavior on slow servers changes, so set the timeout explicitly, as in the example.

Architectural rewrite: the browser-use case

The browser-use case in detail: this is not a “translation” but an architectural rewrite for a new way of working — an agent writes code that calls tools. The result: the library shrank by 90%, input tokens halved, the cost of a task is about 71% of what it used to be, quality grew by 50%, and the tasks themselves come out 2–5 times cheaper. The stack simplified to Python + Chrome DevTools. The lesson: migration is the moment to simplify the architecture, not only to change the language. Ask yourself “how would I write this from scratch today” — and let the agent port the intent, not a literal copy.

Verifying the result: commands and a checklist

The final audit is concrete commands, not the impression that it “seems to work”:

npx tsc --noEmit                   # TypeScript: type check
npx vitest run src/lib/__tests__/  # TypeScript: reference tests
pytest test_date_helpers.py -v     # Python: the original tests are still alive
  • All reference cases pass on the target stack — automatically, not “by eye”.
  • A manual run of the key scenarios — following the list from the migration PRD.
  • Performance hasn’t degraded: measured before and after on the same load.
  • A diff with no unrelated edits: renames and “while we’re at it” refactoring are a red flag.
  • Documentation and call examples are updated for the new stack.

For routine porting of the mechanics you can use a cheaper model, for an architectural rewrite — a flagship; the comparison of Opus and GLM on coding tasks will come in handy.

Common questions

Will the agent break working code? Migration in a new branch plus reference tests plus a human reviewing the architecture — the risk is controlled. A migration “straight into production” without tests — no: simply don’t run it.

How many iterations does it take? The core with tests — usually 2–5 agent iterations; the periphery — refinements as the runs proceed. If there are more than ten iterations, the problem is in the PRD or in weak reference tests.

What doesn’t migrate through an agent? Binary dependencies, hardware bindings, licensing constraints — pin the constraints map in the PRD in advance: it is cheaper than a discovery in the middle of the project.

Migration or a rewrite from scratch? If there are no reference tests and the behavior is loosely described — a rewrite with a PRD; if tests exist — migration by an agent. The middle ground (“we’ll rewrite it and ship it straight to production”) is the worst option.

Code migration is a mature agent task: reference tests define the behavior, the PRD sets the boundaries, the core goes first, and the human stays on the architectural decisions. The browser-use case shows it: properly migrated code is cheaper and better than the original. Concrete action: pick one module that has long been asking for a different stack, build reference tests for it and launch the agent’s first iteration in a separate branch — if the harness isn’t set up yet, the Claude Code starter guide will walk you through the installation in half an hour. The methodology is current as of fall 2026.

Like this post? Please share to your friends:
11AI - Your Free LLM
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: