Maximize token efficiency
Optimize token usage to keep your costs down
Understand tokens
Bolt inference uses Anthropic’s Claude 3.7 Sonnet AI model. We purchase tokens from Anthropic, defined by them as:
The smallest individual units of a language model, and can correspond to words, subwords, characters, or even bytes (in the case of Unicode).
When you interact with Bolt, tokens are consumed in three primary ways:
- Chat messages between you and the LLM.
- The LLM writing code.
- The LLM reading the existing code to capture any changes you made.
How Bolt reduces token usage
Bolt’s goal is to use as few tokens as possible to accomplish each task. The team is hard at work continuing to ship product changes that increase token efficiency.
How you can reduce token usage
Following these tips can substantially reduce your token usage.
Use Discussion Mode
In Build Mode, Bolt updates your code at each stage of your conversation, which uses up tokens. To avoid unnecessary code changes, use Discussion Mode when you want to get help, troubleshoot, or discuss plans, but don’t want to implement anything immediately. Discussion Mode uses approximately 90% fewer tokens than Build Mode.
Avoid repeated automated error fix attempts
Repeatedly clicking the Attempt fix button can lead to unnecessary token consumption. After each attempt, review the result and refine your next request if needed. There are programming challenges that the AI cannot solve automatically, so it is a good idea to do some research and intervene manually if it fails to fix automatically.
Add error handling to your project
If you find yourself stuck in an error loop, a useful strategy is to prompt the AI to enhance error handling and implement detailed logging throughout the problematic area. The AI excels at inserting robust error logs, even at a granular level, such as between functions or key steps. These logs provide valuable feedback that the AI can use to better understand the root cause of the issue. The additional logging provides more precise information when the error occurs again. With this detailed feedback, the AI can make more accurate adjustments to fix the issue.
Use the rollback feature
Use the rollback feature to revert your project to a previous state without consuming tokens. This is similar to an undo button that can take you back to any prior state of your project. This can save time and tokens if something goes wrong with your project. Keep in mind that there is no redo function though. Be sure you want to revert before using this feature because it is final: Bolt permanently removes all changes made after the rollback point.
Add functionality incrementally
Make sure the basics of your app are scaffolded before describing the details of more advanced functionality for your site.
Add one piece of new functionality at a time.
Use specific and focused prompts
When prompting the AI, be clear and specific. See Prompting effectively for more information. Tell the model to focus on specific files or functions rather than the entire codebase.
Reduce the size of your project
As your project grows, more tokens are required to keep the AI in sync with your code. Larger projects (and longer chat conversations) demand more resources for the AI to stay aware of the context, so it’s important to be mindful of how project size impacts token usage.
Use knip.dev to remove unused files
Before proceeding, back up your project using both of these methods:
- Click Export > Download to get a local copy of your project.
- Click Export > Open in StackBlitz. With the project open in StackBlitz, click Fork. You’ll now see forked in the project title. Rename the project to something you can easily find if needed.
In the Bolt terminal run the following command:
npx knip --production --fix --allow-remove-files
Check the output in the terminal to see if the command ran successfully. This command removes unnecessary files from the codebase, which reduces the project size.
Use the .bolt/ignore
file
If you open a Bolt project in StackBlitz you can edit a file called .bolt/ignore
. In this file, you can list any files or folders that Bolt should exclude from the AI context window. For example, here is our Vite React starter’s ignore file.
There, you see the following files are ignored:
These files are completely invisible to the AI, which frees up space in the context window. You’ll need to edit the .bolt/ignore
file in StackBlitz and then reopen the project in Bolt for the changes to take effect.
Hiding files from the AI can have unintended consequences, as the AI is no longer aware of your entire project. This approach is powerful, but is only recommended for advanced users who can make informed decisions about what can safely be excluded, and can understand and resolve issues that may arise from this approach.
Split the project
Break a large app into smaller chunks, and glue it all back together outside of Bolt later. For example, separating the backend and frontend into different projects is a common developer pattern. Be careful doing this if you don’t have experience as a developer.
Reset the AI context window
Resetting the AI context window (clearing the conversation history) reduces the amount of information (context) being sent to the AI.
- With your project open in Bolt, click Export > Open in StackBlitz.
- In StackBlitz, click Fork. This creates a fork of your current project.
- Rename the forked project.
- Return to the StackBlitz dashboard by clicking the lighting bolt icon.
- Open your forked project.
- Click Open in bolt.new. You should now see your forked project open in Bolt without the previous chat history.