‹ Blog
Juri Strumpflohner
Juri StrumpflohnerJuri Strumpflohner

Let AI Agents Extract Reusable Nx Generators

Consistency has become an even bigger problem now that a major part of the code is produced by AI agents. Nx generators were built for an era when writing code was hard, so you could argue they're now obsolete. Turns out they're even better in combination with AI agents.

Using Nx generators for code consistency

In a nutshell, an Nx generator is a parameterized template you can invoke to scaffold code. Nx plugins usually ship them as part of the package so you can generate new plugin-specific packages that are properly wired up.

Built-in plugin generators are all good and nice, but the more powerful part is that you can create your own workspace plugins with their own, so-called "local generators". These live in your monorepo and are designed to automate recurring operations, mostly when it comes to code scaffolding.

So you'd install the @nx/plugin:

nx add @nx/plugin

Then generate one into your workspace:

nx g @nx/plugin:plugin packages/workspace-tools/my-plugin

And finally create a new generator:

nx generate @nx/plugin:generator packages/workspace-tools/my-plugin/src/generators/my-generator

All this does is scaffold the base structure. You then have to implement it with your own logic.

And finally, you can invoke it from within your Nx monorepo just like any other generator:

nx g @myorg/myplugin:my-generator

You can leverage this same mechanism for automating all sorts of things in an Nx monorepo workspace, including adding language support as we discussed in our latest blog series on polyglot Nx monorepos.

Why not just use AI agents to scaffold code

That's what they're great at, right? Let me put it simply:

  • token cost
  • consistency

Scaffolding is a deterministic operation, and I try to move that outside the model as much as I can.

AI agents are non-deterministic. The combination is where the sweet spot is. Since invoking an Nx generator is a CLI command, AI agents are able to read and run it without issues. In addition, an agent is able to interpret the output of the generator and adjust it to the specific workspace situation if needed. That's where the win is, in my opinion.

Have the agent write the Nx generator

At one point, creating and maintaining these generators took time, but now we can let agents take over that part, and they're good at it, because it is a very well-defined task.

You can now point an agent at a library setup you want to standardize and ask it to encode the conventions and configuration as a local generator.

The workspace in the video is a plain pnpm workspace with Vite-based TypeScript libraries. It has one package set up the way I want every future package to look. The prompt is roughly:

Example prompt

Use the Nx Devkit to create a local workspace generator. Take the current project in packages/products as the scaffold: its Vite config, its TypeScript setup, and the way it declares exports. I want new packages in this workspace generated the same way.

There's more

Nx has always been built for customization and extensibility, which has become a major advantage now with AI agents.

Explore the Extending Nx docs page for more examples of how to automate Nx.

Learn More