Temporary workaround for error due to exceeding input token count (unofficial)

Good news for those who are having trouble with the input token count exceeding the limit!
I’ve found a temporary solution.
However, please note that this is not an official solution.

The error discussed in this article

[GoogleGenerativeAI Error]: Error fetching from https://monospace-pa.googleapis.com/v1/models/gemini-2.5-pro-preview-03-25:streamGenerateContent?alt=sse: [400 Bad Request] The input token count (1053137) exceeds the maximum number of tokens allowed (1048576).

Related cases

Explanation

In this section, I’ll share my understanding of how App Prototyper Agent works based on the tests I’ve done.
Please note that this is only my understanding and is not official information.
The conversation history with App Prototyper Agent seems to be saved in the following location in the workspace.

/home/user/.idx/ai folder

It contains the following two JSON files, both of which store conversation history, but they seem to have different formats and purposes.

capra-thread.json

Referenced for display in Prototyper and used to allow app creators to check the conversation history.
The contents mainly include the messages interacted, the names of the files modified, and information about restore points.
I verified this by actually deleting the contents of the files.
Even after deleting the contents of the files and refreshing the page, the history remained.
However, after resetting the workspace, I confirmed that the conversation history on Prototyper was deleted.

Furthermore, when I restored the deleted content and reset the workspace, the conversation history reappeared in Prototyper.

(Reference) Workspace reset: Assign a new VM while maintaining the state of the files on the home disk.

capra-context-state.json

This is the context itself that is added to the input of the App prototyper agent prompt.
In addition to this file, code files in the workspace are also used as context for prompt input.
However, folders and files listed in .gitignore are not used as context for prompt input.

This mainly includes the contents of the initially created App Blueprint and the messages interacted.

Information about files modified by the App Prototyper agent is recorded concisely as follows:

(Omitted from agent history: changes suggested and applied to the following files: /src/ai/flows/generate-recipe-image-flow.ts, /src/ai/dev.ts)

This file is directly referenced only when a new prompt is sent.
When you click [Try Again] to resend the prompt, the capra-context-state.json file that was referenced when the prompt was first sent is referenced.

As it currently stands, all content in this file (i.e., all conversation history) seems to be added to the prompt each time.
This means that as the conversation history grows, the number of input tokens will continue to increase, eventually causing an input token limit error.
In other words, by reducing the contents of this file, you can reduce the number of input tokens and resolve the input token limit exceeded error.
I was able to resolve the issue for the time being by following the steps described at the bottom of this page, but I strongly recommend that you read the following precautions before proceeding.

Precautions

The current behavior of the App Prototyper agent is as follows:

  • Past conversations with the app creator can only be used as a context by referencing capra-context-state.json.
  • capra-thread.json is only for the app creator to view conversation history on Prototyper.

This means that the content of conversations deleted using the following steps will no longer be considered by the App Prototyper agent in the future.
Therefore, please be careful not to delete any conversation history that you consider important for future conversations with the App Prototyper agent.
I recommend deleting the following two types of conversations:

Old messages

General chat AI deletes old conversation history in order when the maximum number of input tokens is exceeded.

user messages that start with “I see this error” and the model message immediately following them

These types of messages can become numerous and are often not important in context.

This is recorded when the following events occur:

The following two messages are recorded for each error correction:

  • user message: A message requesting the app prototyper agent to fix the error
  • Immediately following model message: A message reporting the cause of the error and how the app prototyper agent fixed it

With the above points in mind, carefully select and delete any messages that you believe are not necessary for future conversations with the agent.

How to reduce past conversation history contained in input tokens

Caution

  • I strongly recommend that you read the above points before proceeding.
  • These steps are not official instructions, so please proceed at your own risk.

Procedure

  1. Sign in to Firebase Studio.

  2. From [My workspaces], click the workspace where the problem is occurring to open it.

  3. Click [Switch to Code] to open the code view.

    Switch to Code

  4. Press [Ctrl+`] to open the terminal (skip if the terminal is already open).

  5. Enter the following command in the terminal and press the [Enter] key to execute it, creating backups of the current capra-context-state.json and capra-thread.json files in the context_backup folder:

    timestamp=$(date +%Y%m%d%H%M%S); mkdir -p "/home/user/studio/context_backup/" && cp "/home/user/.idx/ai/capra-context-state.json" "/home/user/studio/context_backup/capra-context-state-${timestamp}.json" && cp "/home/user/.idx/ai/capra-thread.json" "/home/user/studio/context_backup/capra-thread-${timestamp}.json"
    

  6. Confirm that the backup has been created in the context_backup folder.

  7. Open .gitignore and add the following to the bottom:

    # prototyper
    /context_backup
    

    Note: This prevents the context_backup folder and the backup files within it from being used as input for the App prototyper agent context.

  8. Enter the following command in the terminal and press the [Enter] key to open the context file:

    code /home/user/.idx/ai/capra-context-state.json
    

  9. Once capra-context-state.json is open, right-click on the file and select [Format Document].

  10. Verify that the previously single-line capra-context-state.json has been formatted into a readable format.

  11. Locate the line that reads app-prototyping-agent: edit-app-agent: [

  12. Confirm that the conversation history is listed in the range enclosed by [].

  13. Carefully select messages in the app-prototyping-agent: edit-app-agent: [<here>] that you do not need to refer to in future conversations with the agent and delete them as follows:

    Causion: Do not delete the entire JSON or any items other than app-prototyping-agent: edit-app-agent: [<here>]. If you do this, the App prototyping agent might mistakenly recognize existing workspace as new workspace and attempt to recreate App Blueprint.

  14. Once the deletion is complete, press the [Ctrl+S] keys to ensure that the changes to capra-context-state.json are saved.

  15. Click [Switch to Prototyper] to open the Prototyper view.

    Switch to Prototyper

  16. Send a prompt to the App Prototyper agent and verify that no input token count error occurs.
    Instead of clicking [Try Again], be sure to resend the prompt from the input box.

    Note: As explained in the Explanation > /home/user/.idx/ai folder > capra-context-state.json section, [Try Again] action uses the context data from capra-context-state.json which is before you delete the history, so you should see the same input token limit exceeded error again.

  17. If an error still occurs at this point, press the [Ctrl+R] key or the [Refresh] icon in your web browser to refresh the page or reset the workspace (your code files will not be deleted) and try again.

  18. If the issue persists, further reduce the history from capra-context-state.json.
    If the number of input tokens mentioned in the error message after doing this step has decreased compared to before, it is highly likely that the large number of entries in the history is the cause, so keep reducing much more entries in the history.

If the issue persists even after following all the above steps, it is likely caused by another issue, so I recommend contacting official support.

If you want to include deleted conversation history in the context again

Retrieve the relevant history from the backup created in step 5 and return it to /home/user/.idx/ai/capra-context-state.json.

However, this may cause the number of input tokens to exceed the limit again, so if the error occurs again, adjust the size of the history.

Let’s wait for the dev team to implement a permanent solution.

@kirupa, if there are any corrections or additions to the above content, please follow up from you or other official team members.
Unless improvements on the product side are made to adjust the conversation history entered in the prompt, almost all users will eventually encounter this issue.
Therefore, we would like you to address this issue with a high priority.


If this information helps solve your issue, I would be very happy to hear from you :slightly_smiling_face:

8 Likes

I didn’t have the app-prototyping-agent: edit-app-agent line. I have this line: app-prototyping-agent: proposal-agent and deleting conversation history from this array doesn’t do anything to solve this error.

@Tyler_Fischer, thank you very much for your report.
Are you sure that the error you are encountering is an error indicating that the maximum number of input tokens has been exceeded?

If you are encountering an issue where the agent is recreating a Blueprint in your existing workspace, please check here as well:

@Tom Thanks for your reply. I’ve been working on this project for about a week now so i’ve been giving it prompts left and right and I finally hit the limit. I have a well built program.
I’m at the token limit and can’t erase any context. I have my first prompt i ever sent to it showing up in my chat
[GoogleGenerativeAI Error]: Error fetching from https://monospace-pa.googleapis.com/v1/models/gemini-2.5-pro-preview-03-25:streamGenerateContent?alt=sse: [400 Bad Request] The input token count (1511840) exceeds the maximum number of tokens allowed (1048576).
I’ve also obliterated all AI Chat Context, but notice mine says proposal agent, not edit agent.

@Tyler_Fischer, thank you for your report.
If you are still seeing the over limit error on the number of input tokens in the screenshot you submitted, please try implementing the steps I added to the above post just 2 hours ago.
One important update is the addition of step 7 (editing .gitignore).

1 Like

@Tom The update with the .gitignore change worked. I’m back in action. Thanks a ton!

2 Likes

Thanks @Tom for this detailed writeup and proposed solutions. We are also looking into how to reduce the frequency of these errors.

1 Like

It solved my problem!!! Thanks man!!

1 Like

I looked in my project file and these files you mention do not exist. All I see in .idx is /dev.nix. Is this solution already obsolete?

@todobino, thank you for your comment.
The .idx folder you can see in the code view is located at /home/user/studio/.idx.
This is different from the /home/user/.idx folder I mentioned in the above post.
That’s why I’m instructing you to perform the steps from the Terminal.
If you encounter an error indicating that the maximum number of input tokens has been exceeded, please try the above method exactly as described.

Thank you, this worked. I had about 7k lines of user/model. I deleted half of them, and it worked. Cheers

1 Like

@Tom great guide! Although I’ve never get the token error I was looking for a way to clear context because I realized it starts to mix things up, and brings back old code when editing things that aren’t even related.

Have you tested what do we need to keep in the files in order to start fresh but avoiding it trying to prototype again ? could we delete capra-thread.json entirely without messing the context ?

I want to start 100% fresh, but without him wanting to prototype.

I got the same, after uploading an image from the screen saver that it has, so now its all getting back error messages, so far my app is working . but I cant do any other updates, , how can I fix this,

where do I check to delete, wich files? I have large rules , other, I have 3k line on page

@Tom Have you ran into the issue where I have the context-backup in my .gitignore:

prototyper

/context_backup

My capra-context-state.json file is 83 lines (I deleted a lot of history because i don’t need it)

And i’m still getting the error in the prototyper, I was able to continue deleting history and continuing my app but it appears now it’s not reading my changes to my file. yes i’ve saved, reset vm, refreshed. All of it.

thanks, after fiew reloads, works, I delete the messages as you indicate , save the file, and wooo its back

I deleted the first 3k lines; there were too many to check which ones to keep. It worked. I’m sure I deleted a few things that might be needed in the context. I have a backup just in case I need it in the future. Now, I’m a few days deep in more coding, so re-adding that might not be possible.

One more thing, when i only deleted about 1k lines, i still got the error.

I hope this helps.