Firebase + Claude Code (2026)
What It Does
Firebase projects are particularly expensive to develop with Claude Code because of the platform’s distributed configuration model: security rules, Firestore indexes, Cloud Functions, hosting config, and environment variables are spread across multiple files and formats. This guide reduces Claude Code token consumption on Firebase projects by 40-55% through structured context loading and agent-optimized configuration patterns. A typical Firebase project can drop from $180/month to $90/month in Claude Code API costs.
Installation / Setup
Ensure Firebase CLI is available for Claude Code:
# Install Firebase CLI
npm install -g firebase-tools
# Login (required for deploy operations)
firebase login
# Initialize project (if new)
firebase init
Create a Firebase-specific skill to eliminate repeated exploration:
# .claude/skills/firebase-project.md
## Firebase Project: my-app
- Firestore: collections in firestore/ (rules: firestore.rules)
- Functions: functions/src/ (TypeScript, Node 18)
- Hosting: public/ (static) + rewrites to Functions
- Auth: email/password + Google OAuth
- Storage: storage.rules
## Key Files
- firestore.rules -- security rules (MUST test before deploy)
- firestore.indexes.json -- composite indexes
- functions/src/index.ts -- all Cloud Functions exports
- firebase.json -- hosting/functions config
## Deploy Commands
- Full: firebase deploy
- Functions only: firebase deploy --only functions
- Rules only: firebase deploy --only firestore:rules
- Hosting only: firebase deploy --only hosting
Configuration for Cost Optimization
Firestore Security Rules Skill
Security rules are the #1 source of token waste in Firebase + Claude Code workflows. Each debugging cycle costs 10K-30K tokens.
# .claude/skills/firestore-rules.md
## Firestore Rules Patterns
### Read own data
match /users/{userId} {
allow read: if request.auth.uid == userId;
}
### Write with validation
match /posts/{postId} {
allow create: if request.auth != null
&& request.resource.data.title is string
&& request.resource.data.title.size() <= 500;
}
### Admin access
match /{document=**} {
allow read, write: if request.auth.token.admin == true;
}
## Common Errors
- "PERMISSION_DENIED" -> check auth state AND rule path
- "Missing or insufficient permissions" -> usually wrong collection path in rule
- Rules not applying -> deploy: firebase deploy --only firestore:rules
Loading this skill costs ~300 tokens. Without it, Claude reads firestore.rules (500-2,000 tokens), then often reads the client code to understand the data model (5,000-10,000 tokens), then runs test queries (2,000-5,000 tokens per test). Total exploration: 7,500-17,000 tokens vs 300 tokens.
Cloud Functions Optimization
# CLAUDE.md (Firebase section)
## Cloud Functions
- All functions in functions/src/index.ts (re-exported from modules)
- Modules: functions/src/auth/, functions/src/firestore/, functions/src/http/
- Local test: cd functions && npm run serve
- Deploy single: firebase deploy --only functions:functionName
- Logs: firebase functions:log --only functionName
## NEVER
- Deploy all functions when only one changed (wastes deploy time + tokens on output)
- Read node_modules/ in functions/ directory
- Run firebase deploy without --only flag
Usage Examples
Basic Usage
# Good: scoped prompt with Firebase context
claude "Add a Cloud Function that triggers on new user creation
and sends a welcome email. The function goes in functions/src/auth/onUserCreate.ts.
Export it from functions/src/index.ts. Use Resend for email."
# Bad: vague prompt (triggers exploration)
claude "Add a welcome email feature"
# This causes Claude to read 10+ files to understand the project
Advanced: Agent-Friendly Firebase Emulator Output
#!/bin/bash
# firebase-test.sh -- structured output for Claude Code
set -uo pipefail
MAX_OUTPUT=15
output=$(firebase emulators:exec "npm test" 2>&1)
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "STATUS: TEST_FAILED"
echo "EXIT_CODE: $exit_code"
echo "ERRORS:"
echo "$output" | grep -E "(FAILED|Error|AssertionError)" | head -n "$MAX_OUTPUT"
exit $exit_code
fi
echo "STATUS: TEST_PASSED"
passed=$(echo "$output" | grep -c "passing" || true)
echo "TESTS_PASSED: $passed"
This wrapper reduces Firebase emulator output from 2,000-10,000 tokens to under 300 tokens per test run.
Token Usage Measurements
| Firebase Operation | Without Optimization | With Optimization | Savings |
|---|---|---|---|
| Understand project structure | 25,000 tokens | 500 tokens (skill) | 98% |
| Write security rule | 15,000 tokens | 3,000 tokens | 80% |
| Debug permission error | 30,000 tokens | 5,000 tokens | 83% |
| Write Cloud Function | 20,000 tokens | 8,000 tokens | 60% |
| Deploy and verify | 8,000 tokens | 2,000 tokens | 75% |
| Run tests via emulator | 10,000 tokens | 800 tokens | 92% |
Monthly estimate (solo developer, 10 Firebase tasks/day on Sonnet 4.6):
- Before optimization: 10 tasks x 18K avg x 22 days = 3.96M tokens = $11.88 input + $17.82 output = $29.70/month
- After optimization: 10 tasks x 5K avg x 22 days = 1.1M tokens = $3.30 input + $4.95 output = $8.25/month
- Savings: $21.45/month (72%)
Comparison with Alternatives
| Approach | Token Cost | Setup Time | Maintenance |
|---|---|---|---|
| No optimization | High ($30/month) | None | None |
| CLAUDE.md only | Medium ($18/month) | 15 min | Low |
| Skills + wrappers | Low ($8/month) | 45 min | Update on schema changes |
| MCP Firebase tool | Low ($10/month) | 30 min | Tool updates |
Troubleshooting
Claude deploys all functions instead of one: Add to CLAUDE.md: “Always use firebase deploy –only functions:NAME. Never deploy without –only.”
Security rules not taking effect after deploy: Add a verification step to CLAUDE.md: “After deploying rules, verify with: firebase emulators:exec ‘npm test – –testPathPattern=rules’”
Emulator output flooding context: Use the structured test wrapper above. Add firebase-test.sh to the project and instruct CLAUDE.md to use it instead of raw firebase emulators:exec.
Advanced: Firestore Index Management
Firestore composite indexes are a common source of token waste. When a query fails because of a missing index, Claude reads error messages, creates the index, waits for it to build, and retries – costing 10K-20K tokens per incident.
Pre-document indexes in a skill:
# .claude/skills/firestore-indexes.md
## Existing Composite Indexes
| Collection | Fields | Order | Query Purpose |
|-----------|--------|-------|---------------|
| posts | status, created_at | ASC, DESC | Published posts by date |
| posts | user_id, status | ASC, ASC | User's posts by status |
| comments | post_id, created_at | ASC, DESC | Post comments by date |
| users | role, created_at | ASC, DESC | Users by role and signup date |
## Adding New Index
1. Add to firestore.indexes.json
2. Deploy: firebase deploy --only firestore:indexes
3. Wait 2-5 minutes for index to build
4. Update this skill file
## Common Index Error
"The query requires an index" -> check firestore.indexes.json first
The error message includes a direct link to create the index in console
Firebase Security Rules Testing
Security rules testing is the second biggest token consumer after structure discovery. Automate it with a structured test wrapper:
#!/bin/bash
# test-rules.sh -- structured security rules test output
set -uo pipefail
output=$(firebase emulators:exec "npx jest --testPathPattern=rules --json" 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "STATUS: RULES_TESTS_PASSED"
exit 0
fi
echo "STATUS: RULES_TESTS_FAILED"
echo "$output" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
for suite in data.get('testResults', []):
for t in suite.get('testResults', []):
if t['status'] == 'failed':
name = t['fullName'][:80]
msg = (t.get('failureMessages') or [''])[0][:150]
print(f'FAIL: {name}')
print(f' MSG: {msg}')
except:
print('PARSE_ERROR: Could not parse test output')
" 2>/dev/null
exit $exit_code
This reduces security rules test output from 3K-15K tokens to under 500 tokens per run.
Firebase Cost Optimization Checklist
- Create
.claude/skills/firebase-project.mdwith project structure - Create
.claude/skills/firestore-rules.mdwith security rule patterns - Create
.claude/skills/firestore-indexes.mdwith existing indexes - Add
scripts/test-rules.shstructured test wrapper - Add
scripts/firebase-test.shstructured emulator wrapper - Add to CLAUDE.md: “Use firebase deploy –only
, never bare firebase deploy" - Add to .claudeignore:
firebase-debug.log,.firebase/,functions/node_modules/ - Set up per-function deploy commands in CLAUDE.md
Firebase vs Supabase: Token Cost Context
Firebase and Supabase are the two most common BaaS platforms used with Claude Code. Their token cost profiles differ:
| Aspect | Firebase | Supabase |
|---|---|---|
| Security rules format | Declarative (Firestore rules) | SQL (RLS policies) |
| Rules debugging cost | 10K-30K tokens | 20K-50K tokens |
| Schema discovery | Read Firestore rules + indexes | Read Prisma schema + migrations |
| Auth integration | Built-in, simple | Built-in, more complex |
| Edge function debugging | 15K-25K tokens | 10K-20K tokens |
| Total monthly overhead (optimized) | $8-$15/month | $12-$25/month |
Firebase has a slight token cost advantage because its security rules are simpler (declarative vs SQL), but Supabase provides more powerful database features. The skills-based optimization approach works equally well for both platforms.
For teams evaluating which BaaS platform to adopt specifically for Claude Code efficiency, Firebase’s simpler security model and smaller configuration surface provide a slight edge. However, for projects requiring advanced relational queries or complex RLS patterns, Supabase’s PostgreSQL foundation is worth the additional token cost when paired with proper skills files.
Find the right skill → Browse 155+ skills in our Skill Finder.
Related Guides
Estimate tokens → Calculate your usage with our Token Estimator.
Try it: Paste your error into our Error Diagnostic for an instant fix.
- Reducing Claude Code Token Usage for Database Operations – Firestore-specific patterns
- Claude Code Skills Guide – creating Firebase-specific skills
- Cost Optimization Hub – all cost optimization guides