---
title: "ast-grep vs Semgrep: Which to Use | Since.dev"
description: "ast-grep vs Semgrep on the same deprecated call: what each finds, what each fix breaks, speed, CI exit codes, licenses and price, run and timed."
canonical_url: https://since.dev/guides/astgrep-vs-semgrep
last_updated: 2026-12-01
---

# ast-grep vs Semgrep: which should you use?

Use ast-grep for fast structural search, rewrites and custom lint rules in your own code. Use Semgrep when you need security rules or matches that follow imports and aliases. Both engines are free. In my run on 2,473 Python files, Semgrep found a call ast-grep missed, and ast-grep was about five times faster.

Updated December 1, 2026

## Which should you use, ast-grep or Semgrep?

Pick by the job, not by the syntax: they write rules almost the same way, and they answer different questions. Here is the pick by situation:

- Pick ast-grep if you're rewriting code: a deprecated call, a renamed option, a migration across hundreds of files. It's the faster tool and its fixes stay close to what you wrote.
- Pick ast-grep if you want the rule as a library inside your own tool, editor plugin or agent. It ships a JavaScript and a Python API; Semgrep doesn't.
- Pick ast-grep for a pre-commit hook or a CI check on every push, where seconds add up. Its scan fails the job by default on an error-level rule.
- Pick Semgrep if you need security checks. Its default community set alone has over 1,000 rules, by its quickstart, and it can follow data from a source to a sink inside a function (taint analysis).
- Pick Semgrep if a match has to see through imports, aliases and constants. In my test it caught `dt.utcnow()` after `from datetime import datetime as dt`; ast-grep did not.
- Pick the paid Semgrep platform if you need cross-file analysis, triage and PR comments across many repositories. That's an application security product, priced per contributor.

My recommendation: for keeping a codebase current, I'd write deprecation rules in ast-grep, because they run in a fraction of a second and the same file fixes, lints and tests. I'd run Semgrep next to it for security, not instead of it. The trade-off is real, though: ast-grep only sees syntax, so every import style you care about needs its own pattern, and a search with ast-grep can come back short without telling you. If missing a call costs more than speed, search with Semgrep and fix with whichever tool fits the import style.

## How do ast-grep and Semgrep compare?

Both parse code into a syntax tree and match patterns that look like code, and they split on how much meaning they add on top. Every cell below comes from each tool's docs, pricing page or registry page, or from my runs on this page, on Dec 1, 2026. The main sources are ast-grep's [comparison page](https://ast-grep.github.io/advanced/tool-comparison.html) and Semgrep's [Community Edition versus platform page](https://docs.semgrep.dev/semgrep-pro-vs-oss).

ast-grep 0.51.3 and Semgrep 1.187.0 on the same criteria, from their docs, pricing and registries, and my runs, Dec 1, 2026

| Criterion                      | ast-grep                        | Semgrep                                                             |
| ------------------------------ | ------------------------------- | ------------------------------------------------------------------- |
| Price and license              | Free, MIT                       | CLI free, LGPL-2.1; platform from $0 to $30 per contributor a month |
| Latest version                 | 0.51.3, Nov 16, 2026            | 1.187.0, Nov 24, 2026                                               |
| Downloads last week            | 2,902,194 on npm                | 5,176,503 on PyPI                                                   |
| Languages                      | 28 built in                     | 30+ in the Community Edition                                        |
| What a match sees              | Syntax only                     | Syntax plus imports, aliases, constants and taint in one function   |
| Cross-file analysis            | No                              | Paid platform only                                                  |
| Ready-made rules               | A small community catalog       | Over 1,000 in the default set, under their own license              |
| Exit code on an error finding  | scan exits 1                    | 0, unless you pass \--error                                         |
| Use as a library               | Yes, JavaScript and Python APIs | No, CLI and platform                                                |
| 2,473 files, one rule (my run) | 1 finding, 0.29 to 0.31 s       | 2 findings, 1.42 to 1.75 s                                          |

The download counts come from different registries (npm for ast-grep, [PyPI](https://pypi.org/project/semgrep/) for Semgrep), so read them as a sense of size, not a ranking.

## What is each one best for?

### ast-grep

- Best for: rewriting and linting your own code by pattern, quickly, in many languages.
- Price: free and open source under the MIT license ([npm](https://www.npmjs.com/package/@ast-grep/cli), Dec 1, 2026). There's no paid tier.
- Pros: a pattern reads like the code it matches; one YAML file fixes, lints and tests a rule; scan exits 1 on an error-level match, so CI fails without extra flags; it ran about five times faster than Semgrep in my test; it can run as a library.
- Cons: its own [comparison page](https://ast-grep.github.io/advanced/tool-comparison.html) says it has no type information, control flow, data flow or taint analysis, and no constant propagation; it doesn't resolve imports, so an aliased call slips through; its catalog of ready-made rules is small next to Semgrep's registry.
- Runs on: your machine or CI, as a native binary from npm, pip, Cargo, Homebrew or MacPorts ([quick start](https://ast-grep.github.io/guide/quick-start.html)).

### Semgrep

- Best for: security scanning, and code search that has to follow imports and simple data flow.
- Price: the Community Edition CLI is free under LGPL-2.1. The platform's Free edition covers up to 10 repositories and 10 contributors; Teams costs $30 per contributor a month for Code, $30 for Supply Chain and $15 for Secrets ([Semgrep pricing](https://semgrep.dev/pricing), Dec 1, 2026).
- Pros: matches see through imports, aliases and constants; over 1,000 community rules in the default set, for security and frameworks; single-function taint analysis in the free CLI; findings in JSON and SARIF for other tools.
- Cons: slower to start and to scan; it exits 0 on findings unless you add `--error`; its fix is plain text, which can break code written with a different import style (shown below); registry rules are under the [Semgrep Rules License](https://semgrep.dev/legal/rules-license), which allows internal business use only; cross-file analysis is paid.
- Runs on: your machine or CI, installed with pipx, uv, pip (Python 3.10 or later) or Homebrew, which Semgrep says it keeps up on a best-effort basis ([Semgrep quickstart](https://docs.semgrep.dev/getting-started/quickstart-ce)).

## What happens when you run the same rule in both?

Semgrep finds more, and ast-grep runs faster. I tested one real deprecation: [datetime.utcnow()](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow), deprecated since Python 3.12. Python 3.12.15 prints this when you call it:

```
$ python3.12 -W always -c \
  "import datetime; datetime.datetime.utcnow()"
<string>:1: DeprecationWarning:
datetime.datetime.utcnow() is deprecated and
scheduled for removal in a future version. Use
timezone-aware objects to represent datetimes in
UTC: datetime.datetime.now(datetime.UTC).
```

Python printed that as one line; I wrapped it. The test file has the call twice, once through an alias, plus the same text in a comment and a string, which a text search would also hit:

```
import datetime
from datetime import datetime as dt

a = datetime.datetime.utcnow()
b = dt.utcnow()
# datetime.datetime.utcnow() in a comment
c = "datetime.datetime.utcnow()"
```

The two rules say the same thing: match the call, report it, and offer the replacement Python's own warning names.

### ast-grep: rule.yml

```
id: utcnow
language: python
severity: error
message: utcnow() is deprecated in 3.12
rule:
  pattern: datetime.datetime.utcnow()
fix: datetime.datetime.now(datetime.UTC)
```

### Semgrep: semgrep.yml

```
rules:
  - id: utcnow
    languages: [python]
    severity: ERROR
    message: utcnow() is deprecated in 3.12
    pattern: datetime.datetime.utcnow()
    fix: datetime.datetime.now(datetime.UTC)
```

```
$ npx ast-grep scan -r rule.yml \
    --report-style short src
src/app.py:4:5: error[utcnow]: utcnow() is
  deprecated in 3.12
Error: 1 error(s) found in code.
$ echo $?
1
```

```
$ semgrep scan --config semgrep.yml \
    --metrics=off -q --emacs src
src/app.py:4:5:error(utcnow):a =
  datetime.datetime.utcnow():utcnow() is
  deprecated in 3.12
src/app.py:5:5:error(utcnow):b =
  dt.utcnow():utcnow() is deprecated in 3.12
$ echo $?
0
```

I wrapped the long lines and left out ast-grep's help line. Both skipped the comment and the string. Semgrep also matched line 5, because it knows dt is datetime.datetime; ast-grep only matches the spelling in the pattern. And note the exit codes: Semgrep found two ERROR findings and still exited 0. With `--error` it exited 1. The one-liners, `ast-grep run -p` and `semgrep -e`, gave the same split: one match and two.

## Does the difference show up in real code?

Yes. I ran both rules over the site-packages folder of the fresh Semgrep install: 2,473 Python files and 787,138 lines of other people's code. ast-grep found one call, Semgrep found two:

- Both: boltons/ecoutils.py line 258, which calls `datetime.datetime.utcnow()` after `import datetime`.
- Semgrep only: boltons/timeutils.py line 240, which calls `datetime.utcnow()` after importing the class with `from datetime import ...`. That's boltons 21.0.0.

That second form is common in Python, so an ast-grep rule for this deprecation needs a second pattern for it. You can add one under `any`, but you have to know to. That's the honest cost of a syntax-only tool: when it misses, it prints nothing about it. Count the hits with a plain `git grep -n utcnow` once, and compare before you trust a structural search on a new codebase.

## Which one fixes code more safely?

Neither checks that its fix runs, and Semgrep's wider match makes that matter more. I applied both fixes to copies of the test file, with `ast-grep scan -U` and `semgrep --autofix`. ast-grep changed line 4 ("Applied 1 changes"). Semgrep changed lines 4 and 5, and line 5 came out right only because the file also had `import datetime`.

Then I gave Semgrep the boltons import style on its own:

```
$ cat c/job.py
from datetime import datetime

x = datetime.utcnow()
$ semgrep scan --config semgrep.yml \
    --metrics=off --autofix -q c
$ cat c/job.py
from datetime import datetime

x = datetime.datetime.now(datetime.UTC)
$ python3.12 c/job.py
AttributeError: type object 'datetime.datetime'
has no attribute 'datetime'
```

I left out Semgrep's findings box and the traceback's first lines, and wrapped the error. Semgrep matched the call by meaning but wrote the fix as plain text, and in this file datetime is the class, not the module. ast-grep never matched that line, so it never broke it. The fix, whichever tool you use: keep a fix only on a pattern that matches one import style, run your tests after any automatic rewrite, and read the diff.

## Is ast-grep faster than Semgrep?

Yes, about five times on my machine. I timed each rule over the same 2,473 files, three runs each, on an Apple M6 with 12 cores:

- ast-grep 0.51.3: 0.30, 0.29 and 0.31 seconds of wall time.
- Semgrep 1.187.0: 1.75, 1.42 and 1.43 seconds.
- On the one-file test, ast-grep took 0.10 seconds through npx and Semgrep about 0.9 seconds, most of it start-up.

Bing's answer for this query quotes a bigger gap, about 43 ms against 7.5 seconds on a simple search, citing zread.ai. That's a different workload from mine, so I'd plan on the five times I measured on Python. For a nightly scan, 1.4 seconds is nothing. For a hook that runs on every save or commit, ast-grep's speed is the reason to pick it. For the same kind of test against jscodeshift, see [jscodeshift vs ast-grep](https://since.dev/guides/jscodeshift-vs-astgrep.md).

## Are ast-grep and Semgrep open source?

ast-grep is, under MIT, with no paid edition. Semgrep's engine is open source under LGPL-2.1, but its registry rules are not: the [Semgrep Rules License v1.0](https://semgrep.dev/legal/rules-license) lets you use them for your own internal business purposes and says you may not distribute them or offer them as a service. Your own rules are yours, under any license you like.

There's also [Opengrep](https://github.com/opengrep/opengrep), a fork of the Semgrep engine, also LGPL-2.1, started on GitHub in December 2024. I didn't run Opengrep for this page. If you're building a product that ships rules to other people, read the rules license first; for scanning your own repositories, it doesn't get in the way.

## How do you run each one in CI?

Run the CLI on the repository with your rules folder, and make sure a finding fails the job. With the rules above:

```
# ast-grep: exits 1 on any severity: error match
npx ast-grep scan -r rule.yml .

# Semgrep: add --error, or findings exit 0
semgrep scan --config semgrep.yml \
  --metrics=off --error .
```

Both have a test runner for rules. ast-grep's [ast-grep test](https://ast-grep.github.io/guide/test-rule.html) takes valid and invalid code samples in YAML. Semgrep's [test mode](https://docs.semgrep.dev/writing-rules/testing-rules) (`--test`) reads a sample file next to the rule with `# ruleid:` and `# ok:` comments above the lines that should and shouldn't match. A rule with a test is a rule that keeps working after someone edits it. If you want to know which deprecations an upgrade brings before you write any rule, start with [how to check dependencies for breaking changes](https://since.dev/guides/dependency-breaking-changes.md).

## Where Since.dev fits

Since.dev doesn't replace either tool, and it isn't a security scanner. It answers the question before the rule: which deprecations, breaking releases, model retirements and runtime end-of-life dates touch your code, and where. [Scan a public repository](https://since.dev/scan.md) (free, no sign-in) for the files and lines, with the replacement. Package pages such as [semgrep](https://since.dev/packages/pypi/semgrep) and [@ast-grep/cli](https://since.dev/packages/npm/@ast-grep/cli) list what changed between versions, and your coding agent can ask the same through the [MCP server](https://since.dev/docs.md#mcp).

For changes it can fix, it opens a fix pull request that was built and checked first; it doesn't open routine version bumps. For more rewrite tools, see the [best codemod tools](https://since.dev/guides/best-codemod-tools.md). It reports or opens pull requests for people to review, and nothing merges or deploys.

## Questions

### Is ast-grep better than Semgrep?

For rewriting and linting your own code by pattern, usually yes: it was about five times faster in a Dec 1, 2026 test, and its rules fix, lint and test from one file. For security scanning, or a search that must follow imports and aliases, Semgrep is better. Many teams run both.

### What is the difference between ast-grep and grep?

grep matches text, so a search for utcnow also finds comments, strings and names that only contain the word. ast-grep parses the code and matches its structure, so a pattern finds only real calls, however they're spaced or split across lines. It can also rewrite what it matches.

### Is Semgrep free?

The Semgrep Community Edition CLI is free and open source under LGPL-2.1. The hosted platform has a Free edition for up to 10 repositories and 10 contributors; Teams is a paid plan priced per contributor per month, by Semgrep's pricing page on Dec 1, 2026. Registry rules come under the Semgrep Rules License, which allows internal business use only.

### Why does Semgrep exit 0 when it finds something?

By default, semgrep scan exits 0 even on ERROR findings. Add `--error` and it exits 1 when there's a finding, which is what a CI job needs. ast-grep's scan exits 1 on its own when a rule with severity error matches.

### Can ast-grep find security vulnerabilities?

Only the ones you can write as a syntax pattern, such as a call to a dangerous function. Its own docs say it has no data flow or taint analysis, so it can't tell whether user input reaches that call. For that, use Semgrep or another security scanner.

### What is Opengrep?

Opengrep is a fork of the Semgrep engine, under the same LGPL-2.1 license, whose GitHub repository was created in December 2024. It describes itself as a static code analysis engine to find security issues in code. I haven't run it, so check its own docs for how close it stays to Semgrep's rule format.

## Related guides

### Comparisons

- [jscodeshift vs ast-grep: which should you use?](https://since.dev/guides/jscodeshift-vs-astgrep.md)jscodeshift vs ast-grep on the same fix: TypeScript, formatting, speed, CI checks and price, with both run on a real Node.js deprecation and timed.
- [Best codemod tools to fix deprecated API calls](https://since.dev/guides/best-codemod-tools.md)The best codemod tools for replacing deprecated APIs, by language and job, with prices, pros and cons, and each one run on a real deprecated call.

### How-to

- [How to check your dependencies for breaking changes before you upgrade](https://since.dev/guides/dependency-breaking-changes.md)How to check dependencies for breaking changes before upgrading: the commands, a real failing upgrade, CI that fails on deprecations, and where notices appear.
- [How to find the breaking changes between two versions of a package](https://since.dev/guides/breaking-changes-between-versions.md)How to find breaking changes between two versions of a package: list the releases in between, read each changelog, diff the code. Commands for npm, PyPI, Go.

## Put your code on continuous maintenance.

Scan a public repository now, or give your agent one line.

[Scan a repository](https://since.dev/scan.md). Any public GitHub repository. No sign-in.

### Tell your agent what changed since it was trained.

`claude mcp add --transport http since https://api.since.dev/mcp`

[Get an API key](https://app.since.dev/login?mode=signup)

Codex, Cursor and any MCP client use https://api.since.dev/mcp.

---

Every page on this site as Markdown: https://since.dev/sitemap.md. For agents: https://since.dev/llms.txt.
