# How editors understand code: LSP, Tree-sitter, and their relationship to AI agents

> Code gets syntax highlighting and can jump to function definitions, thanks to two tools that appeared about ten years ago: Tree-sitter handles syntactic structure, and LSP handles semantics. Claude Code added LSP support at the end of 2025. This post first explains the two clearly, as background for the tests that follow.

Published 2026-11-17 · Updated 2026-11-17 · URL https://wizerdbache.com/en/notes/lsp-agents-background/


I recently came across a popular science post about why code gets syntax highlighting automatically and why editors can tell you where a function is defined. The answer is two tools: **LSP (Language Server Protocol)** and **Tree-sitter**. LSP is about ten years old and Tree-sitter about eight, yet both have become taken-for-granted parts of writing code.

The post says Tree-sitter can save tokens and LSP can reduce hallucinations, and it also mentions at the end that Claude Code added LSP at the end of 2025. I use Claude Code every day, but I have never turned this feature on. So I did something: **installed it, designed an experiment, and measured whether it makes any difference.**

This series records the whole process. This is the first post, which explains the two tools.

## Conclusions first

- **Tree-sitter handles “syntactic structure”**: whether this piece of text is a function definition, a string, or a comment.
- **LSP handles “semantics”**: which thing this name refers to, where it is defined, what else uses it, and whether there is a type error here.
- **Claude Code uses LSP**: LSP tools have been available since version 2.0.74 (2025-12-19), and you connect each language’s language server through a plugin. It helps the agent in two ways: the agent can query actively (jump to definition, find references), and after each file edit, errors are reported to the agent automatically.
- **Whether it helps has to be measured**: the next few posts are experiments.

## 1. LSP: each language needs to be done only once

### What problem it solves

Jumping to definitions, finding all references, renaming, and marking errors all require “understanding” the language. In the past, every editor had to build its own version for every language: with 5 editors and 10 languages, up to 50 combinations had to be maintained.

LSP’s approach is to split out the “understanding” part and hand it to a separate program, called a **language server**. Editors and language servers communicate through a shared message format (JSON-RPC). As a result:

- Each language needs only **one** language server, and every LSP-capable editor can use it.
- Each editor needs to support LSP **once** to connect to all languages.

### Where it came from

On June 27, 2016, Microsoft’s VS Code team announced at the DevNation 2016 conference that Red Hat and Codenvy would also adopt the protocol: Red Hat would build a language server for Java, and Codenvy would make Eclipse Che support it. The protocol was published openly on GitHub and became a cross-vendor open standard. When I checked on November 15, 2026, the latest version on the official website was 3.18.

### Questions a language server can answer

Take Pyright, the language server for Python, as an example:

| Question | LSP operation |
|---|---|
| Where is this name defined? | goToDefinition |
| Where else is it used? | findReferences |
| What type is this variable? | hover |
| Who calls this function? | incomingCalls |
| Does this file have any errors? | diagnostics |

What these questions have in common is that **text alone cannot answer them**. For example, in the line `item.load(key)`, you need to know which class’s object `item` is in order to know which `load` it calls. That requires type inference, which is exactly what a language server does.

## 2. Tree-sitter: a syntax tree that updates as you type

### How highlighting worked before

Before Tree-sitter, many editors used regular expressions to color code. In simple cases this works well, but it only matches text patterns and does not know the hierarchical structure of the code.

For example, this line of Python:

```python
message = "def test(): pass"
```

Here, `def` is inside a string, not a real function definition. The editor has to know that it is “inside a string” in order to color it correctly. Once syntax starts nesting, maintaining these rules with regular expressions becomes harder and harder.

### Why not just use the compiler’s parser?

Compilers have complete parsers, but they are designed for “compiling”: they usually parse the whole file once, and may stop when they hit an error. While you are writing code, it is almost always in a “not finished yet” state, and we cannot accept that a missing bracket on line 5 leaves the 300 lines below it all without color.

### How Tree-sitter works

Tree-sitter is a parser generator: it generates a parser for each language. Its official documentation lists four design goals:

- **General-purpose**: can handle almost all programming languages.
- **Fast**: fast enough to re-parse on every keystroke.
- **Error-tolerant**: still produces useful results when the code has syntax errors.
- **No external dependencies**: can be embedded directly into other programs.

It builds a **syntax tree**. When you edit code, it updates only the part that changed instead of re-parsing the whole file; this is called **incremental parsing**.

### Where it came from

GitHub engineer Max Brunsfeld presented Tree-sitter at FOSDEM 2018 on February 4, 2018. On October 31 that same year, GitHub announced that Atom 1.32 enabled Tree-sitter by default: highlighting follows the syntax tree, code folding follows syntactic structure rather than indentation, and a shortcut key can expand or shrink the “selected syntax unit” one level at a time.

## 3. How the two divide the work

| | Tree-sitter | LSP (language server) |
|---|---|---|
| Question it answers | What is this text syntactically? | What does this name point to? Are the types correct? |
| Does it need to understand the whole project? | No, looking at a single file is enough | Yes, it tracks imports and types across files |
| Typical uses | Highlighting, folding, selecting by syntax | Jumping to definitions, finding references, renaming, marking errors |
| Speed | Updates on every keystroke | Slower, needs to build a project index |

Many editors use both together: Tree-sitter handles the live structure on screen, and the language server handles features that need to “understand the whole project.”

## 4. How it relates to AI agents

When an AI agent changes code, the thing it does most often is **finding things**: where a function is defined, which places call it, and what changing this spot will affect. Without LSP, the agent mainly relies on text search (such as grep) and then reads the code itself to make a judgment. This carries two risks:

- **Wrong matches**: when there are many functions and methods with the same name, text search cannot tell them apart, so the agent has to read files one by one to judge, and it can easily miss something or misjudge.
- **Token cost**: to make these judgments, many files have to be read into the conversation.

Starting with version 2.0.74 (December 19, 2025), Claude Code provides LSP tools. According to the official documentation, it connects to language servers for each language through “code intelligence plugins,” and there are two ways to use it:

1. **Active queries by the agent**: jumping to definitions, finding references, checking types, and so on. These are all read-only operations.
2. **Automatic diagnostics after edits**: each time the agent edits a file with its editing tools, errors and warnings from the language server are automatically sent into the conversation. According to the official documentation, this lets the agent catch type errors it has introduced, missing imports, or syntax errors without running a compiler.

A few details are worth knowing first:

- The plugin **does not include** the language server itself; you have to install it separately (for example, install Pyright first for Python).
- Cloud sessions do not start these language servers.
- I previously evaluated a tool that builds code relationship graphs with Tree-sitter, and on small projects of the size I work with it gave almost no benefit, so this series only tests LSP.

## 5. Questions this series will answer

1. If you just “turn on the plugin,” does the agent perform better?
2. If the agent is explicitly told to use LSP, how much benefit does it bring?
3. Does this benefit change with the “degree of ambiguity” in the code? In other words, would the difference be larger in projects where text search easily gets things wrong?

The next post covers how I designed the experiment so that “with LSP” and “without LSP” can be compared fairly. The results will appear in later posts; the experiment is still running.

## Sources

- [A Common Protocol for Languages (VS Code team, 2016-06-27)](https://code.visualstudio.com/blogs/2016/06/27/common-language-protocol) (LSP announced at DevNation 2016, with Red Hat and Codenvy participating)
- [Red Hat, Codenvy and Microsoft Collaborate on Language Server Protocol (Red Hat press release)](https://www.redhat.com/en/about/press-releases/red-hat-codenvy-and-microsoft-collaborate-language-server-protocol)
- [Language Server Protocol official website](https://microsoft.github.io/language-server-protocol/) (Messages use JSON-RPC; latest spec when checked on 2026-11-15 was 3.18)
- [Tree-sitter: A new parsing system for programming tools (FOSDEM 2018 schedule page)](https://archive.fosdem.org/2018/schedule/event/code_tree_sitter/) (Max Brunsfeld, 2018-02-04, Source Code Analysis track)
- [Atom understands your code better than ever before (GitHub Blog, 2018-10-31)](https://github.blog/2018-10-31-atoms-new-parsing-system/) (Atom 1.32 enables Tree-sitter by default)
- [Tree-sitter official documentation](https://tree-sitter.github.io/tree-sitter/) (Four design goals: general-purpose, fast, still usable on syntax errors, no external dependencies)
- [Code intelligence plugins (Claude Code official documentation)](https://code.claude.com/docs/en/plugins/code-intelligence) (Plugins do not include the language server itself; diagnostics are reported automatically after each edit; cloud sessions do not start them)
- [Claude Code v2.0.74 release notes (third-party version tracking site)](https://www.havoptic.com/r/claude-code-2.0.74) (Released 2025-12-19, adds LSP tool)
