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.

2 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.