Solved: [GoogleGenerativeAI Error]: The input token count (xxxxx) exceeds the maximum number of tokens allowed (1048575). Unofficial Solution

:magnifying_glass_tilted_left: Problem

I kept running into a frustrating Gemini error in Firebase Studio:
[GoogleGenerativeAI Error]: The input token count (xxxxx) exceeds the maximum number of tokens allowed (1048575).

Even after resetting context, things still didn’t work. Gemini would stall, miss files, or just throw that same token error. It turns out this isn’t a bug — it’s a hidden bloat problem.


:brain: What’s Really Going On?

Gemini has a hard token limit of 1 million tokens — based on logs and testing.

But your project might silently exceed that because Gemini:

  • Reads all files, not just src/
  • Indexes .next/, dist/, .git/, internal/, and even package-lock.json
  • Includes large markdown docs, binary files, or compiled JS maps
  • Doesn’t warn you in the Studio UI

When I checked, my project had ~1.8M tokens total, and Gemini silently failed to load full context.


:hammer_and_wrench: The Fix — Clean Up with

.studioignore

+ a Script

Firebase Studio does support a .studioignore file — similar to .gitignore. It’s undocumented but works.

Here’s what solved it for me:

:white_check_mark: Step 1 — Create a

.studioignore

file

Paste this into your project root:

node_modules/
.next/
dist/
internal/
context_backup/
firebase-debug.log
*.node
*.wasm
*.log
*.map
.git/
package-lock.json

# Optional:
docs/

This tells Gemini to ignore those directories and heavy files.


:white_check_mark: Step 2 — Run This Script to Check Token Bloat

Here’s a small Bash script that prints your token estimate, file size offenders, and whether .studioignore is working:

:light_bulb: Save this as: scripts/health-check-context.sh

  1. Make it executable. From your terminal run:
    chmod +x scripts/health-check-context.sh

:package: Then run it from the terminal: bash ./scripts/health-check-context.sh

#!/bin/bash

echo "đź§  Gemini Context Health Check"
echo "-----------------------------"
echo ""

echo "📦 Largest files (by size):"
du -sh .[!.]* * 2>/dev/null | sort -hr | head -n 10
echo ""

echo "⚠️  Files over 100KB (potential token bloat):"
find . -type f -size +100k | grep -vE "node_modules|.git|.next|dist|context_backup|internal" | sort
echo ""

echo "📏 Estimating total word count (excluding node_modules)..."
wc_output=$(find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.json" -o -name "*.md" \) \
  -not -path "*/node_modules/*" \
  -not -path "*/.git/*" \
  -not -path "*/.next/*" \
  -not -path "*/dist/*" \
  -not -path "*/internal/*" \
  -not -path "*/context_backup/*" \
  -exec cat {} + | wc -w)
token_estimate=$(( $wc_output * 4 ))
echo "→ Total words: $wc_output (~$token_estimate tokens)"
if [ "$token_estimate" -gt 1000000 ]; then
  echo "❌ OVER TOKEN LIMIT — Gemini will fail to load context."
else
  echo "âś… Under token limit."
fi
echo ""

echo "🔍 Checking .studioignore..."
if [ -f .studioignore ]; then
  echo "âś… .studioignore found."
  for path in node_modules .next dist internal context_backup firebase-debug.log .git package-lock.json; do
    if grep -q "$path" .studioignore; then
      echo "âś… $path is ignored"
    else
      echo "⚠️  $path is NOT ignored — consider adding it"
    fi
  done
else
  echo "❌ .studioignore not found — create one!"
fi

echo ""
echo "🎯 Context Health Check Complete"

:clipboard: What This Script Tells You

  • Which folders are eating space
  • Which files over 100KB might blow up your token count
  • If your .studioignore is working
  • A rough estimate of total tokens Gemini will see

:white_check_mark: How I Verified It Worked

After fixing my .studioignore, I asked Gemini in Studio:

“List the files you have context access to right now.”

It correctly excluded .next/, .git/, dist/, etc.

Previously, these were all being included — silently pushing me over the limit.


**:paperclip: Related Post:

How to reset context

This fix works after you’ve reset context properly. The original reset post helped me — this one builds on that with a more robust cleanup strategy.


Most people using Firebase Studio don’t realize how easy it is to overload Gemini.

Until Studio shows token feedback visually, you need to self-check.

This script + a good .studioignore is the cleanest fix I’ve found so far.

Hope this saves someone else hours of debugging.

3 Likes

Amazing addition! I definitively gonna use it.

Never occurred to me that gemini would be including, for example, some (or all) of the saved context files, although these don’t go to the capra-thread file, or else they would blow immediately.

So you’re saying the prototyper may be including these files, even if they don’t show on the capra-thread file ?

Yes prototyper is definitely including these files based on my experience. Not just these, it also includes any logs or huge files that you have in your project folder unless you specify what to ignore. The .studioignore might be a good addition. Since it is not officially documented, it might only be applicable to a few users until they rollout to everyone.

Thank you for this post and script.

Is this still working for you? I tried .studioignore and it does not work for me.

There is also no populated icon .PNG or .SVG like there are for other filetypes.

I’m testing .aiexclude and .gcloudignore now which both have icons and seem to be recognized.

I’ve noticed that the context files also changed names recently, from capra* to prototype*

The official docs say you should use .aiexclude to exclude files from Gemini’s indexing, so I assume that’s what you should be using.

1 Like

Here’s a good .aiexclude starting point, if anyone is interested.

P.S.
I know some people are using context_backup for the ai context backups, but for me ai-context makes more sense, since I come from coding environments where context is a common occurrence and I needed this to be more explicit.

# Dependency and Build Artifacts
# Exclude third-party libraries, compiled code, and build-generated files.
/node_modules/
/.pnp/
.pnp.*
.yarn/
!/.yarn/patches/
!/.yarn/plugins/
!/.yarn/releases/
!/.yarn/versions/
/dist/
/build/
/.next/
/out/
*.tsbuildinfo
*.node
*.wasm
package-lock.json

# Development and Debugging Artifacts
# Exclude temporary, log, and debugging files.
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
firebase-debug.log
firestore-debug.log
*.map

# Environment and Sensitive Files
# Crucial: Exclude API keys, secrets, and environment configurations.
.env*
*.pem

# IDE and System Files
# Exclude IDE-specific and OS-generated files.
.DS_Store

# Version Control System Files
# Exclude Git's internal directory.
.git/

# AI-Specific Exclusions
# Exclude AI-generated content or backups.
.genkit/
ai-context/

# Cloud Platform Specific
# Uncomment if you want to exclude Vercel-related files.
#.vercel/

# Internal Project Structure (Review carefully!)
# Consider excluding 'internal/' if it contains sensitive or unreleased code.
# Remove this line if AI should see its contents.
internal/
1 Like

These steps helped me to identify the files that were blocking my prototyper…now, I deleted the files, and everything is working again! thanks a lot!!!

Super helpful. Thanks Ben.

Wish I would have tried harder to find those docs myself!

1 Like