There’s been increasing interest in AI coding assistants across the software development field, and a number of us have been testing out a few of these agents for writing and understanding Mojo code. For example, at our recent GPU programming hackathon, we saw several teams incorporate these code generation tools into their workflow to help build impressive applications in a short period of time.
Mojo poses a unique challenge for these tools: it has many similarities to Python, for which there’s a large corpus of training data, but is a new language with its own distinct syntax and libraries. Mojo also evolves rapidly, so even an LLM trained on the last stable release might be slightly out of date on the latest Mojo nightly. Without the right guidance, you’ll see even the best coding-trained LLMs generate code that doesn’t compile due to being too Python-like, randomly mixing in C++ types, or even building with older Mojo patterns like our good friends DynamicVector
and let
.
In our experiments, we’ve found that a few key things are necessary to get coding LLMs to stay up to date with Mojo:
- make sure they use the use the modular repository as reference, especially with the massive amount of Mojo code we’ve recently open-sourced
- rely on docs.modular.com for API searches
- use detailed language-specific rules.
@joe and @adakkak have added some initial rules to our open-source modular
repository for Cursor and Claude Code that we’ve found to be very helpful for those tools. There are probably many ways these can be refined to better understand and generate Mojo code from our open-source repository, and we welcome contributions to help improve this experience.
In my experience with tools like Cursor, if you clone the modular
repository locally, add it to your project’s workspace, and set rules to have Cursor refer to that, it can often generate correct and modern Mojo code.
Do you have any tips or techniques that you’ve found to work well in generating idiomatic Mojo code for various use cases? Feel free to discuss in this thread, as well as ask any questions you might have about how to apply these tools to Mojo.