Zig and AI coding

The other AI coding thread got closed post haste, but maybe there’s room to discuss how AI would better work with Zig and maybe people have ideas about this?

I love Zig but am forced to take long breaks due to customer projects in other languages and other commitments. During breaks the language often changes a lot and I’d like to use AI to get back into a Zig mindset when prototyping a new idea, or to deal with new issues in my own code after a version upgrade while trying to keep the project afloat and avoid accumulating legacy issues over time.

I get that core devs and many people continuously working with Zig consider AI a waste of time. It can’t produce sufficient quality and Zig is about code crafted with love and thought. AI has a tendency to generate slop inspired by existing code and solutions of variable quality, while Zig is about breaking new ground that AI has never seen or been trained on.

That said, I do benefit from AI a lot in my consulting work, speeding up learning new tools, reviewing code from another POV or approaching research papers through examples tailored to my use case. It also helps in a very limited way every time I want to get back to Zig.

Just now I learned that a popular JavaScript framework called Svelte has published documentation specifically to be consumed by AI / LLM:
https://svelte.dev/docs/llms

It has a particular writing style often seen in AI prompts, leaked or otherwise, and could serve to steer the model away from outdated syntax learned from countless legacy examples. Would that kind of documentation, if maintained and fed to AI prompts or local rules files, help new users approach the latest release through tailored examples and answers to newbie questions that are more likely to compile?

I’m sort of interested to learn about AI prompt design for coding. It seems to be maturing into best practices usable across different LLM models. Improving LLM reliability in successfully producing working Zig code seems like an interesting application, but I really won’t have time to produce something very broad not to mention maintain it, since I’m just now looking at Zig again after a 6-month break. Continued maintenance of the AI rules/documentation after each release would be their whole point and best handled by people who actually enjoy using such tools. Does anyone have thoughts or experience about these things?

I asked ChatGPT about an example rules file (also using the entire text above as a prompt). It thought 0.13 is newest. Details are irrelevant but I thought the structure, style and topics are interesting. Overall content illustrates its current understanding of contemporary Zig and issues with AI:
https://gist.github.com/jjrv/2cecbf9700f490075773b9f220e5ed51

5 Likes

I’m in a similar position, that I’m coming back to Zig periodically and learning about what’s changed, then updating all of my projects for the latest versions.

I’d like better tooling to spot (and ideally fix) use of deprecated APIs or patterns. I’m thinking something like “zig fmt”, with the ability to say “LinearFifo has been deprecated in 0.16.x, do you want to automatically upgrade to std.Deque?”.
If those actions were based on something machine readable and formal, they’d also be potentially useful to LLMs - if that’s your preference.

1 Like

This one line in CLAUDE.md makes a huge difference for me:

use zig env to get the path to the Zig standard library, if you need to check something

5 Likes

A reliable migration tool sounds like a lot of work of a similar sort as developing the compiler, standard library and language services themselves, so there’s probably a question of priorities and motivation before 1.0.

It’s a good point that the AI can also read the standard library code directly. I asked it to compare the code of std/fmt.zig between 0.11 and now, summarizing the required changes to calling code. The result looks reasonable. If it then had that summary in its context, it might have more success assisting in migration and refrain from producing outdated output.

It might be possible to automate analyzing old and new versions of the standard library implementation file by file, producing rules and examples to use as context much denser than the complete source. Totally informal, unreliable and unofficial, but also very low effort and flexible with regard to changes in syntax and internal compiler API. In a way let the model itself discover its knowledge gaps compared to the latest Zig version and produce its own cheat sheet.

Maybe this would be a doable, low-maintenance and future-proof approach to potentially improve AI-generated Zig. Even if it won’t be production quality, it might be more likely to run.

Claude Sonnet 4.5 is much better than anything I’ve been trying previously. You need to spend some time tweaking your CLAUDE.md but then it can be very useful.

It’s not like that you give it a feature description and let it code but it can already help you with repetitive tasks, it can write more unit-tests, you can ask it for non-trivial refactorings, etc.

TBH, it sometimes feels like cheating. But to be clear, it’s still nowhere close to what it can do with JS.

And just to be clear, it can still do silly mistakes, or say things like “typecheck went fine so there are no bugs” which gave me a good laugh :slight_smile:

EDIT: I forgot, the most crazy thing is that you can ask Claude to make changes in its CLAUDE.md whenever you go over something which it couldn’t do or you were not very happy about it. So it’s a process, you start with /init and then start working together, and then you continuously improve your CLAUDE.md until it gets good enough (and at the same time you develop a hunch at what tasks it might be good at and what’s unlikely to succeed)

3 Likes

This is a great topic. I view LLM as a tool just like LSP. Both help develop software. As Zig is still changing, most LLM’s are outdated in regarding to the latest changes. They are still useful on the vast existing concepts and api.

In addition I think Zig should have a greater participation in making AI advances. In that regard, I have been building a MCP SDK for Zig. The project has been on and off for a while. It’s getting to the point of usable now and I’m going to get it released. Hopefully it will allow people to write MCP servers in Zig, and perhaps MCP clients in Zig.

2 Likes

Indeed good coding models don’t have much difficulty in figuring out latest zig version usage when pointed to local std source, eg Codex picked up 0.15 idioms right away. Reading the clean std helps them as much as a human.

I had performance issue with the std hashmap tombstone accumulation. Andrews suggested that instead of fixing it, the arrayhashmap should just replace hashmap, but that makes no sense, as that has completely different performance characteristics. But no problem, codex just ported a hashmap implementation I needed in no time to Zig 0.15 in minutes. I would have not spent a few hours doing that, but I could proceed without this being a problem.

I think it is not smart of Andrew and Loris to dismiss AI to this degree. Zig needs all the momentum it can get. Rust is already everywhere, Zig isn’t. Zig needs to move fast. I believe Zig is the best high performance oriented language by far, there is nothing like comptime anywhere, that is such a winner. But Zig needs an ecosystem, ie libraries. AI assisted coding could help build up an ecosystem that could give momentum to Zig that it lacks today. AI assistance would make it easier to build a bigger ecosystem and roll it along with the evolving language faster. With proper human oversight its not all slop at all. Andew and Loris should understand and let it bring advantage to Zig. So I think LLM geared documentation would be a great accelarator.

Again: LLM not to think instead of Andrew about what Zig should be. Not for cargo cult programmers either. But for the right people for the right task it does accelerate.

1 Like

It really depends a lot on what you are doing, it might be (subtly) helpful for stdlib devel but I wouldn’t expect it to be good for the compiler part, as that is likely going to be out-of-distribution.

Karpathy recently talked about this, and why he mostly wrote the nanochat himself instead of using LLMs. I think it’s also not very good in the data-structure design either, ie. I have a semi-working B+ tree which I really stitched together quickly (and never had time to finish it) and I was hoping that maybe Claude could fix it but man it was just spinning around introducing new mistakes and fixing those and congratulating itself, only to end up with exactly the same code it started with :slight_smile:

I made zigdoc specifically to help agents figure out APIs just a bit faster. I generally just put something like “Use zigdoc to discover zig APIs. Assume your knowledge of zig APIs is out of date” in an AGENTS.md file

This generally helps quite a bit. I’ve got a ~35k SLOC zig project that’s almost all vibes and it’s gone exceedingly well, about as well as I do with typescript and agents.

5 Likes

Awesome, thanks for the insight! I didn’t know improvements that big had already been made and in ways that are so quick to explain.

Does the tooling use consume more requests, does the agent spend resources faster in practice when producing Zig than TypeScript due to checking docs more often? Or is it negligible?

1 Like

I’ve been using Zig for about 2.5~ years at this point full time. I’ve chosen it over my background language of C++ coming from graphics. Now a founder working on infra at a startup. (Biotech).

I have not once gotten AI to spit out code useful for me outside of boiler plate. Maybe it’s the combination of domain and Zig, I don’t know. Last night at 1am ,(first kid, 6 weeks old), I broke down and tried Claude and ChatGPT to help with an optimization problem. They both gave 10 similar ideas and they were so bad I didn’t need to test them. I figured something out myself after some sleep.

I do see the use of it as a good reference for docs. Grabbing some info on something I don’t understand since search engines have been slowly dying. Except it doesn’t do a good job all the time. I’ve had more luck reading through release notes when it comes to Zig. However, I see this as a good place for an LLM to do some decent work. Most of the time.

And that’s one core issue that I find myself agreeing with the ‘naysayers’ on.
“Sometimes it works most of the time” is not a good place to build an ecosystem on. It does scare me a bit when I see that people want to pump out a bunch of libraries using AI, let alone pumping out knowledge bases with it a la aforementioned locked post.

I don’t want the same ecosystem that C++ has now. Plenty of examples and libraries. And most of it horrible.

This doesn’t mean that it’s a zero sum game. The std docs could use many more examples. It could have the migrations from past versions displayed. And that would be a benefit to both a programmer and a mindless stochastic model.

However, a rush to push things so that it works well for an LLM specifically does feel like it ends up as a detriment to the core Zig users who don’t use those tools. It inevitably steers resources. With a further issue of potentially putting out misleading information that simply confuses a new user.

Something like zigdoc seems like the most obvious answer here along with an eventual fully flushed doc.

14 Likes

The other problem is that it then puts a burden on us humans to correct it’s failures, and LLMs have a tendency to output very subtle mistakes that only show up far into the future. Not to mention creating an ecosystem that primarily caters to LLMs and not humans creates stagnation and quality that trends downwards to the lowest common denominator.

4 Likes
6 Likes

Ultimately LLM tries to predict a sentence. Any reasoning the model does is still based on this premise. If you can’t wrap your head around a solution then it’s likely not on stack overflow or any source it’s trained on. If it’s an unknown problem and you can’t start a sentence leading to the solution then ai is still far from being helpful.

Only ask ai when the answer is searchable or when you already know the technical solution to guide it. Then you’ll discover what a time saver it is.

Just don’t take the answer at face value, always do at least a serious review or even better a bit of refactoring.

2 Likes

I was specifically thinking it’s up to the part of.the user community interested in making productive use of AI with Zig, to figure out how, through better third-party tooling and methods. zigdoc and information how to use it is an excellent development in this direction.

Nobody who dislikes or disagrees with AI use should be burdened with it, but others will use it and have questions. Maybe this thread has some pointers now with more to come.

2 Likes

Perfect example of what ai really excels at, a well defined task with lots of references. Porting it from any reference language to zig is easy for a good LLM most likely it even got the allocation parts right in first go.

Another example success story, I used ChatGPT to get OpenCV libraries installed with CUDA-accelerated optical flow, resolving various local driver issues.

Then I had it produce Zig code to upload bitmaps from CPU to GPU, compute the flow and fetch the result back. Again strange errors were involved.

It saved a lot of time deciphering driver issues and limitations especially when all existing examples seemed to use Python, while its virtual environments are another irrelevant source of problems.

1 Like

I’d be interested in seeing that code. Did it end up brilliantly simple or did it have to go through hoops?

Looking at it again, sadly a C++ shim is involved because OpenCV didn’t seem to provide an API callable directly from Zig. I think ChatGPT wrote the whole shim and then it took a lot longer to figure out the command line args to compile and link it:
https://gist.github.com/jjrv/d781eb15bf15094c814ef8024ae65702

It really wasn’t a meaningful intellectual exercise in the way writing the Zig code to produce the original data was, so I don’t feel like having lost anything by leaving entirely to LLM the fight to get a single library function compiled, linked and called.

Exactly. And LLMs make producing misinformation/BS pretty much cost-free. They’re great if you understand the domain your working in at a high degree, but the moment you want them to help you with something that you aren’t knowledgeable in, they break down completely. I kid you not: I know several people who literally think AI is good enough to teach beginners programming. Seriously. These are people who don’t have degrees and who have never written a line of code in their lives. They probably at most know what a programming language is and that’s it. Maybe they know which one to learn from a web search but that’s as far as they’ve gotten. And these pro-AI people think that LLMs can teach these guys programming. To say I was appalled is, well, a bit of an understatement.