{"slug": "dynamic-workflows-feature-that-enabled-the-bun-rewrite", "title": "Dynamic Workflows: feature that enabled the Bun rewrite", "summary": "Pydantic AI has introduced Dynamic Workflows, a new feature in its Pydantic AI Harness that allows AI models to orchestrate multiple agents by writing Python scripts at runtime. The feature was inspired by Jarred Sumner's use of a similar workflow to rewrite the Bun runtime from Zig to Rust in 11 days, using 64 Claude agents at peak to port over half a million lines of code while passing 100% of existing tests. Dynamic Workflows enable developers to hand a catalog of named agents to an orchestrator model, which then writes and executes scripts that call sub-agents as async functions, with results returned in a single tool call.", "body_md": "TL;DR: Models are pretty good at orchestrating more of themselves. With Pydantic AI\n\n`DynamicWorkflows`\n\nyou can easily create your swarm of agents.\n\nBun got rewritten in Rust. Jarred took \"Rewrite it in Rust bro\" quite seriously.\n\nThen he published [the blog post everyone was waiting for](https://bun.com/blog/bun-in-rust?utm_source=pydantic). A couple of things stood out to me.\n\n- Dynamic Workflows\n- Jarred being a cracked dev (who would have guessed)\n\nWhat Jarred actually did\n\nThe numbers are silly, but I will cite them anyway. Half a million lines of Zig ported, a diff of just over a million lines. 100% of the existing test suite still passing Eleven days from first commit to merge.\n\nI would love to imagine Jarred prompting, \"Yo dawg too many memory errors, can we do this in Rust?\".\n\nThe actual workflow however was more nuanced.\n\nHe set up workflows where agents wrote plans for other agents. One workflow figured out Rust lifetimes for every struct field, another ported files. About 50 of these workflows ran over the 11 days, 64 Claudes at a time at peak, every file reviewed by two adversarial agents, you get the picture.\n\nDo this in a loop and there you have it, Bun in Rust.\n\nModels, it turns out, are pretty damn good at orchestrating more of themselves.\n\nCan I get some of that in Pydantic AI?\n\nYes. Absolutely!\n\nYou could already get pretty close. We've had [Code Mode](https://pydantic.dev/articles/your-agent-would-rather-write-code) for a while: instead of picking tools off a menu one call at a time, the model writes a Python script that calls them. Wire your agents in as tools, and you'd get part of the way there.\n\nYou don't have to do that plumbing anymore. Dynamic workflows are now a first-class capability in the [Pydantic AI Harness](https://pydantic.dev/docs/ai/harness/).\n\nHow does it work?\n\n`DynamicWorkflow`\n\nis Code Mode moved up a level. Code Mode gives the model a script for its **tools**. `DynamicWorkflow`\n\ngives it a script for its **agents**.\n\nYou hand it a catalog of named agents. It hands the model a single tool. Inside that tool the model writes ordinary Python, where each sub-agent is an async function it can call, loop over, and combine or even graph(*if you know you know*). The whole script runs in one tool call, and only the last line finds its way back into context.\n\nIt looks something like this:\n\n``` python\nimport logfire\nfrom pydantic_ai import Agent\nfrom pydantic_ai_harness.experimental.dynamic_workflow import DynamicWorkflow\n\nlogfire.configure()\nlogfire.instrument_pydantic_ai()\n\nreviewer = Agent('anthropic:claude-sonnet-5', name='reviewer', description='Reviews code for bugs.')\nsummarizer = Agent('anthropic:claude-sonnet-5', name='summarizer', description='Summarizes findings.')\n\norchestrator = Agent(\n    'anthropic:claude-opus-4-8',\n    capabilities=[DynamicWorkflow(agents=[reviewer, summarizer])],\n)\n```\n\nThe script the orchestrator writes at runtime looks something like this. It fans out, chains the results, and only the result survives:\n\n``` python\nimport asyncio\n\nreports = await asyncio.gather(\n    reviewer(task=\"Review auth.py for bugs:\\n<file contents>\"),\n    reviewer(task=\"Review parser.py for bugs:\\n<file contents>\"),\n)\nawait summarizer(task=\"Summarize these findings:\\n\" + \"\\n\\n\".join(reports))\n```\n\nNeed to reveal agents during the run?\n\nWe got you.\n\n```\nworkflow = DynamicWorkflow(agents=[reviewer])\norchestrator = Agent('anthropic:claude-opus-4-8', capabilities=[workflow])\n\n# later, once a potato agent has been provisioned:\nworkflow.reveal(potato)\n```\n\n`potato`\n\nbecomes callable on the very next step. The model finds out through a short note carrying the new function's signature, and the tool's own description never changes, so the reveal doesn't bust the cache.\n\nWatch your agents\n\nEvery agent run and model call lands on the same [OpenTelemetry trace](https://pydantic.dev/logfire). The `run_workflow`\n\nspan carries the script the model wrote, so you can read what actually ran.\n\nRunning multiple agents with no trace is just an expensive way to generate slop. No judgment if you prefer the heartache, but **I** would much rather have a system I can debug.\n\nTry it out\n\n```\nuv add \"pydantic-ai-harness[dynamic-workflow]\"\n```\n\nPoint an orchestrator at a couple of agents (or a few hundred), hand it a task that's too big for one context, and watch it write its own workflow. Then [open it up in Logfire](https://pydantic.dev/docs/logfire/get-started/ai-observability/) and watch the swarm work.\n\nIf it saves you from hand-rolling your own orchestration glue, a star on [GitHub](https://github.com/pydantic/pydantic-ai-harness) helps other people find it.", "url": "https://wpnews.pro/news/dynamic-workflows-feature-that-enabled-the-bun-rewrite", "canonical_source": "https://pydantic.dev/articles/dynamic-workflows", "published_at": "2026-07-28 09:00:00+00:00", "updated_at": "2026-07-28 20:36:05.378240+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "generative-ai", "large-language-models"], "entities": ["Pydantic AI", "Bun", "Jarred Sumner", "Claude", "Pydantic AI Harness", "DynamicWorkflows", "Code Mode", "OpenTelemetry"], "alternates": {"html": "https://wpnews.pro/news/dynamic-workflows-feature-that-enabled-the-bun-rewrite", "markdown": "https://wpnews.pro/news/dynamic-workflows-feature-that-enabled-the-bun-rewrite.md", "text": "https://wpnews.pro/news/dynamic-workflows-feature-that-enabled-the-bun-rewrite.txt", "jsonld": "https://wpnews.pro/news/dynamic-workflows-feature-that-enabled-the-bun-rewrite.jsonld"}}