5  AI for Programming

Published

February 19, 2026

Modified

April 9, 2026

There are many ways in which we can use AI for coding: doing what some call vibe coding, relying on autocompletion, using AI assistants for planning, and—my favorite—assigning tasks to AI agents. Each of these modes represents a different level of involvement between the human and the model, and each has its own strengths and limitations.

There is no secret formula for coding effectively with AI. Recently, the lead of Claude’s coding tool mentioned that he has not written a line of code in the last four months or so. Although that would be fantastic, people who are actively using AI for coding know it is not that simple. AI can be extremely helpful, but it does not remove the need for judgment, structure, and oversight.

Sure, when you are implementing simple tasks—deploying a website, creating a UI, or generating template code for data analysis—AI can be outstanding. But that is mostly because there are many examples of these tasks available online. When it comes to scientific usage, however, my impression is that the performance is not as strong. The more niche the method, the dataset, or the modeling framework, the more careful we need to be.

In this chapter, I will provide some general guidelines and best practices—the ones that work for me—for coding with AI. We will focus on GitHub Copilot, which is one of the most complete toolkits available today.


5.1 Fundamentals

The key to effective coding with AI is context.

We can always ask an AI assistant or LLM simple questions such as:

“Implement a function to run model XYZ.”

But the quality of the response depends heavily on the context the model has available.

In coding, context usually comes from two (or more) places.

The first is the local folder where the analysis or project is being executed. Unless you are starting from scratch, it is almost always better to use an AI agent that has access to your local codebase. If you are using GitHub Copilot inside VS Code, it automatically takes that information into account.

The second is an explicit set of instructions for your agents, typically provided through an AGENTS.md file. This simple markdown file has become something of a standard. The AGENTS.md file contains general instructions you provide to your agent. Typical elements include:

  • An overall description of the project
  • The intended role or knowledge type of the agent
  • Coding style guidelines
  • The usual workflow (e.g., “always create test files using package XYZ” or “ensure the code runs using this test dataset”)
  • Known errors, pitfalls, or things to avoid

This last part is particularly important because it usually reflects your past experience. Over time, the AGENTS.md file allows your agent to “evolve,” incorporating instructions that help prevent repeated mistakes.

When agents have strong contextual information, they are much better at understanding what you want to achieve. This reduces the amount of detail you need to provide in each prompt, since much of the guidance already lives in your project files.

Once you have both an AGENTS.md and a README.md in place, most AI agents—whether GitHub Copilot, ChatGPT Codex, Claude, or others—have enough information to proceed effectively.

To set up GitHub Copilot, you first need a GitHub account. If you have an .edu email address, you may qualify for a free premium account that includes Copilot. Otherwise, the annual plan (last time I checked) was around 100 USD.


5.2 Asking the AI to Generate Code

As mentioned in earlier chapters, AI systems have become significantly better at generating code that works. Still, they face limitations—many of which resemble those of a human research assistant:

  1. Niche code, models, or fields with few public examples
  2. Overly complicated tasks (smaller chunks work better)
  3. Tasks that require complex environments or configurations

In these situations, it is better to ask the AI to implement features in a modular way and, if possible, test them against known expectations.

For example, instead of asking:

“Analyze my data.”

You could ask:

“Write code to create a table with these characteristics. Here are a few example rows of the dataset.”

Providing structure and clear expectations improves reliability.

That said, directly asking for code in a conversational way is not the most efficient use of AI. This is one reason why OpenAI created ChatGPT Codex and why other providers are investing heavily in agentic workflows. The real advantage comes from Agentic AI, which we will explore in the next couple of chapters.


5.3 Code Completion

With code completion, AI helps you write code faster by predicting what you are about to type.

This feature works best when you already have a codebase. In that case, the context includes all the previous examples you have written. Some of the situations where I find this most useful include:

  • Starting a new function
  • Renaming variables consistently
  • Extending existing patterns in the code

The speed of code completion can usually be adjusted (faster or slower suggestions), and both VS Code and RStudio support this functionality.

In my experience, code completion performs best in a mature codebase, where multiple scripts and consistent conventions give the AI strong contextual signals.


5.4 Planning

A newer and very useful feature is planning mode.

You may not always trust the AI to write perfect code directly. However, it can be extremely helpful when planning changes. For example:

  • Implementing a new feature
  • Refactoring an existing module
  • Reorganizing a large codebase

When the number of lines to change becomes large, reviewing AI-generated edits can become overwhelming. Instead of asking for direct implementation, you can request a plan.

In planning mode, the AI creates a checklist of the steps required to implement the change. This helps you:

  • Understand the scope of the task
  • Break it into manageable components
  • Maintain control over the implementation process

Planning mode is particularly useful when the task is too complex to hand off blindly to an agent, which is the topic of our next chapter.