LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 35 lessons

Lesson 1 of 9

Module 3 · Coding with AI assistants

What AI coding assistants are

What AI coding assistants are

What AI coding assistants are

An AI assistant is a large language model: a system trained on huge amounts of text and code to predict what comes next. That makes it fluent and fast, and explains both what it does well and how it fails.

⏱ 10 min readModule 3 · Coding with AI assistantsFree
Lesson at a glance3 steps · about 10 min
  1. How a language model writes
  2. Three ways to use one
  3. What it does well, and how it fails

Module 1 gave you enough Python to read a script line by line, and Module 2 gave you the judgement to tell an honest figure from a misleading one. Both matter here. An AI assistant can now write much of that code for you, and this module is about using it without handing over control: knowing what the tool is, asking it well, reading and testing what it gives back, and staying responsible for the result.

How a language model writes

A large language model (LLM) is a very large mathematical model that has been shown enormous amounts of text and code and adjusted, step by step, to get better at one task: given the text so far, predict what comes next. It works in tokens, short pieces of text that are often a whole word, part of a word or a symbol such as (. For each possible next token, it computes a probability. One token is chosen, added to the text, and the whole process repeats, token by token, until the answer is complete.

Two consequences follow, and they explain most of this module. First, the model writes what is likely given its training data, not what it has checked to be true. Second, the model itself does not run the code it writes. Unless the tool you use is connected to somewhere code can be executed, nobody has tested the answer before you see it.

A third point is what the model can see. Everything it uses to answer, your question, any code you pasted and the conversation so far, is called its context. It has no view of your files, your data or the versions of the libraries on your computer unless you put them in that context. When it writes pd.read_csv("samples.csv"), it is guessing the file name, the column names and the separator from what is typical, not from your file.

It helps to picture what that means for a small task. Ask for a function that returns the reverse complement of a DNA sequence and you will very likely get working code, because that exact function appears in countless tutorials. Ask for a function that reads your lab's own plate-reader export and you will get code that reads a plausible plate-reader export. It may be close, but only your file can say whether it is right.

One common pitfall: a model can say "I have tested this and it works" because that sentence often follows code in its training text. Unless you can see that code was actually run, with its output, treat such statements as words, not evidence.

What AI coding assistants are · LSMA free course The text "The complement of A is" goes into a language model, which gives a probability for each possible next piece of text: T is most likely, U and G much less likely. The chosen piece is added to the text and the model predicts again. 1 · TEXT SO FAR 2 · MODEL 3 · WHAT COMES NEXT The complement of A is … Language model TUGother 0.860.080.030.03 4 · Add the chosen piece, "T", to the text, then predict again The probabilities are illustrative, not taken from a real model.
A language model never looks anything up: it scores every possible next piece of text and picks a likely one. "U" gets a small share because the complement of A in RNA is U, a pattern also common in its training text. Schematic, not to scale.

The three kinds of tool below are all built on the same kind of model. What differs is how much of your work the model can see, and how much it is allowed to do.

The kind of tool also changes what can go wrong. A chat answer cannot change anything until you copy it. An in-editor suggestion lands in your file with a single key press, so it is easy to accept lines you have not read. A coding agent can change many files and run commands before you look, so a mistake can spread further before anyone notices.

Go deeper: why the same question gives different code

Most tools do not always pick the single most likely next token. They sample: they choose at random, but in proportion to the probabilities. A setting often called temperature controls how adventurous that choice is. A low temperature sticks closely to the most likely tokens and gives more repeatable answers; a higher one gives more varied answers.

This is why asking the same question twice can give two different functions. Both will look plausible, and one may be right while the other is wrong. It also means that a second answer agreeing with the first is weak evidence: both came from the same model and the same patterns. A test with an answer you worked out yourself is independent evidence. A second opinion from the same assistant is not.

Three ways to use one

KindHow it worksGood for
Chat assistantYou describe a task in a chat window; it replies with code and explanationLearning, explaining, small scripts, debugging
In-editor assistantSuggests the next lines as you type in VS Code, RStudio or JupyterRoutine code, boilerplate, finishing a pattern you started
Coding agentReads your project, edits files and runs commands to complete a taskLarger changes across many files, with your review

How to read this table

The rows run from least to most independent. A chat assistant sees only what you paste into the conversation and can only reply. An in-editor assistant sees the file you are working on and offers a few lines at a time, which you accept or ignore. A coding agent can open files across your whole project, change them and run commands, such as installing a package or running your tests, often several steps in a row before it reports back. The more a tool can do on its own, the more you need to review what it did. Boilerplate, in the second row, means the routine code that looks almost the same every time, such as loading packages or reading a CSV file.

Take one task, adding a GC-content column to a table of sequences, and see how each kind of tool would handle it. In a chat, you paste a few rows of the table and ask for the code; you then copy it into your notebook and run it. In the editor, you type def gc_content(seq): and the assistant offers the body of the function, which you read before accepting. An agent might be asked to "add a GC column and a test for it", and it would open the script, write both, run the test and report. The task is the same; the amount you have to check afterwards grows from one cell to several files.

A sensible way to start is with a chat assistant, where every line passes through your hands before it runs. Move to tools that act on their own once you have the habits from the next lessons: asking clearly, reading before running, and testing. If you use an agent, check what it is allowed to do. Commands that install packages, delete files or send data elsewhere should need your approval each time.

What it does well, and how it fails

Strengths
  • Writes common code quickly in most languages
  • Explains unfamiliar code and error messages
  • Translates between R and Python
  • Never tires of “why?”
Typical failures
  • Code that looks right and runs, but gives wrong answers
  • Invented functions, options or packages
  • Outdated library versions
  • Missing edge cases: empty input, lower case, missing values

Why the strengths and failures go together

Both columns come from the same mechanism. Common code, such as reading a CSV file with pandas, appears many thousands of times in public code, so the model has seen the pattern often and usually reproduces it correctly. Explaining an error message works for the same reason: the same messages, and explanations of them, are everywhere.

The failures are the other side of that coin.

  • Plausible but wrong. In Read before you run you will see a GC-content function that runs without complaint and returns 0 for a lower-case sequence. Nothing about the code looks odd; it does not do what was needed.
  • Invented names. If many libraries have a function with a certain kind of name, the model may produce one that sounds right but does not exist in the library you are using. This is often called a hallucination: confident output that is not grounded in fact.
  • Outdated versions. A model's training data stop at a cut-off date, and libraries keep changing after it. Code written for an older version of pandas or seaborn may give warnings or errors, or behave differently, with the version you have installed.
  • Edge cases. An edge case is an unusual but possible input, such as an empty sequence or a lower-case one. Typical examples in training data rarely include them, so generated code often does not handle them.

A second example of "plausible but wrong" is a detail of standard deviations. In pandas, df["len"].std() divides by n − 1 by default, which is the usual choice for a sample. NumPy's np.std() divides by n unless you pass ddof=1. Both are common in public code, so an assistant may swap one for the other while rewriting your analysis. On large samples the difference is tiny; on three replicates it is not. Nothing crashes, and the error bars quietly change size.

The practical response to invented names is quick. Before you rely on a function or argument you do not recognise, look it up: help(sns.boxplot) in Python, or the library's own documentation online. If it is not there, it does not exist in your version, however confident the answer sounded. The __version__ attribute, as in pd.__version__, tells you which version you actually have.

Worked example · why fluent is not the same as correct

the model predicts likely next words, based on patterns in its training data
a common pattern is usually right · a rare detail (Phred+33 offsets, a package's newest argument) is easy to get subtly wrong
so: the more specialised the task, the more carefully you check

Step by step. In a FASTQ file, the quality of each base is stored as one text character. To get the number, you take the character's ASCII code (the number computers use for each character) and subtract an offset. In the usual modern encoding, Phred+33, the offset is 33. Some older Illumina data used an offset of 64 instead. Both versions appear in public code, documentation and forum answers, so a model can easily produce the wrong one, or forget the offset altogether. The output still looks like a sensible number; it is just wrong.

What the answer means. How fluent an answer sounds tells you nothing about whether it is correct. Your checking effort should follow how specialised the task is: a standard plot needs a quick look, but anything involving file formats, units, offsets or statistical choices needs a test with a known answer.

The standard way to measure code-writing models began with Chen et al. (2021), “Evaluating large language models trained on code” (arXiv), which introduced Codex and the HumanEval benchmark: 164 hand-written Python problems, each checked by unit tests. In that paper, Codex solved 28.8% of problems with one attempt and 70.2% when 100 attempts were sampled per problem.

Read such scores with care. Newer models score far higher, but benchmark problems are short, self-contained puzzles, and some may have leaked into later training data. A high benchmark score does not tell you how often a tool gets specialised scientific code right, such as file formats or statistical choices.

Key point

You are responsible for every line you run or publish, whoever wrote it. Use AI to go faster at things you can check, not to do things you cannot.

A second example. Asking an assistant to turn your working loop into a neater pandas command is a good use: you can compare the old and new output on the same data and see at once whether they agree. Asking it which statistical test proves your hypothesis is not, because you would have no independent way to know whether its choice is right. In a paper, both would carry your name.

Further reading · Module 3Wilson et al. 2014: testing and review habits that apply to AI-written code too. · All reading for this course

Check your understanding

1. What is a large language model trained to do?

2. Which failure is most dangerous in research code?

3. Which task suits an AI assistant best for a beginner?

Up next

Lesson 2

Asking well: prompts for code

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.