Mentat Tutorial- The AI Coding Assistant

Photo of author
Written By Zach Johnson

AI and tech enthusiast with a background in machine learning.

Mentat is an open source AI assistant built for software developers. It integrates directly within coding projects to help developers code faster and focus on more interesting problems.

Who is Mentat For?

Mentat is useful for any developer that works with source code. It can assist with:

  • Debugging and fixing bugs across multiple source files
  • Quickly ramping up and learning a new codebase
  • Generating initial source code for new projects
  • Automating repetitive code changes and refactors

Developers that use Mentat are more productive, focusing on the interesting aspects of development while letting Mentat handle the mundane.

When to Use Mentat

Mentat shines when you need to make changes across multiple files. Instead of manual edits and copying code back and forth, Mentat handles it for you.

It’s also great when starting work in a new codebase. You can ask Mentat for an outline of the project or to explain components to ramp up faster.

Anytime you need to restructure code or refactor, Mentat can generate and apply those changes for you.

Example with LLama2

Here’s an example using mentat on the more complex LLama2 codebase:

git clone
cd llama
mentat llama2.c

This clones the repo and loads the 485 line llama2.c file.

We can now make requests like:

Give me an outline for the code

To understand the structure. Or:

When we sample a non-likely token, print the most likely token in red brackets  

To modify the sampling code.

Mentat allows quickly iterating on large real-world codebases. Give it a try today to supercharge your development workflow!

Example Calculator Project

Here is an example calculator project:


def add(a, b):
  return a + b

def subtract(a, b):
  return a - b

def multiply(a, b):
  return a * b

def divide(a, b):
  return a / b

from operations import *

def calculate(a, b, operation):
  if operation == 'add':
    return add(a, b)
  elif operation == 'subtract':
    return subtract(a, b)
  elif operation == 'multiply':
    return multiply(a, b)
  elif operation == 'divide':
    return divide(a, b)
    return 'Invalid operation'

Running mentat on these files, we can request:

Add power operation

This will add a power function to and handle the new case in

We can then further refactor:

Use dictionary lookup instead of if/elif blocks

And mentat will make the changes to use a cleaner dictionary lookup approach.

Mentat allows rapid iteration on code by handling the routine refactoring and edits for you. Give it a try on your projects!

AI is evolving. Don't get left behind.

AI insights delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.