Claude Code is a powerful AI development tool, but it comes with a rate limit if you're on the Pro plan. When you’re iterating fast on backend systems—especially with Python frameworks like FastAPI, SQLAlchemy, and Pydantic—you’ll hit that limit sooner than you'd like.
This post dives into concrete tactics that reduce token usage in Claude Code while keeping your dev flow productive and your prompts effective.
🔧 1. Write Prompts Like You Write Specs
Claude Code doesn’t do well with vague. Broad requests trigger excessive clarifications and output size.
❌ Bad
"Create a login feature"
✅ Good
"Implement a login feature in FastAPI using JWT authentication.
Requirements:
– Define request/response models with Pydantic
– Hash passwords using bcrypt
– Validate inputs: email format, password ≥ 8 characters
– Handle errors using HTTPException (401 and 422)"
✅ Break It Down into Steps
Don’t ask for everything at once. Split complex features into sub-steps:
- Define the SQLAlchemy
User
model - Create corresponding Pydantic schemas
- Build JWT token generation/verification utilities
- Implement the login endpoint
- Add authentication decorators
This avoids ambiguous code and bloated responses.
🧠 2. Reduce Context Bloat
Claude Code doesn’t need your whole codebase. Just the part you’re working on.
❌ Bad
“Here’s the entire repo.”
✅ Good
“Here’s the model file and its dependency that needs to be updated.”
Use Repomix for Project Overviews
If you must provide structural context, use repomix
:
npx repomix --style xml --compress --remove-comments --remove-empty-lines
This gives Claude a project snapshot while stripping out:
- Comments
- Blank lines
- Non-critical boilerplate
But don’t feed it huge XML dumps—extract just the relevant directories/files.
🔄 3. Patch, Don’t Rewrite
Avoid full-file regeneration. Instead, ask Claude to modify only what’s necessary.
❌ Bad
"Rewrite
models/user.py
to include a new field"
✅ Good
"Add an
email_verified: bool = False
field to theUser
class inmodels/user.py
.
Also update the Pydantic schema inschemas/user.py
to include this field."
That cuts token usage and preserves your code formatting and style.
📐 4. Use Regular Claude for Pre-Design
Don’t throw architecture problems at Claude Code.
Use Claude (chat mode) for:
- Designing APIs
- Discussing technology trade-offs
- Brainstorming solutions to tricky problems
- Debugging with stack traces
Then give Claude Code well-defined, build-ready specs.
Example Workflow:
1. Chat with regular Claude:
“Design a user registration flow with FastAPI using JWT, SQLAlchemy, and Alembic.”
2. Hand-off to Claude Code:
“Implement the registration endpoint using the following specs:
– SQLAlchemy model
– Pydantic schema
– JWT handling
– Alembic migration
– pytest test cases”
🔁 Feedback Loop Pattern:
When Claude Code fails:
“Claude Code is throwing this SQLAlchemy error:
[Error logs]
I’ve already tried A, B, and C. Suggest the best workaround.”
Then forward the fixed plan to Claude Code as a direct instruction.
🗣 5. Group Related Tasks
Stop making Claude Code context-switch.
❌ Inefficient:
"Add created_at"
"Add updated_at"
"Update schema"
"Write migration"
✅ Efficient:
“Please do the following:
- Add
created_at
,updated_at
tomodels/user.py
- Update the corresponding Pydantic model in
schemas/user.py
- Generate the Alembic migration file”
Same for errors—bundle the message and code together:
❌ Bad:
"Here’s an error"
✅ Good:
“This PydanticValidationError occurred:
ValidationError: email field required
Here’s the relevant code fromschemas/user.py
:class User(BaseModel): name: str
Expected behavior: make the email field optional.”
🧪 6. Python-Specific Optimizations
✅ Use Type Hints
Claude Code uses them to infer better structures.
# Request with type hints
from typing import Optional, List
from pydantic import BaseModel
def get_users(
skip: int = 0,
limit: int = 100,
search: Optional[str] = None
) -> List[UserResponse]:
# Request implementation
...
✅ Declare Dependencies in Advance
[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.104.1"
sqlalchemy = "^2.0.23"
pydantic = "^2.5.0"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
passlib = {extras = ["bcrypt"], version = "^1.7.4"}
Tell Claude what you’re using so it doesn’t guess or pull in outdated packages.
✅ Ask for Tests Early
Instead of:
“Now write the tests”
Say:
“Implement the login endpoint and write corresponding pytest cases:
– Successful login
– Validation error (missing fields)
– Mock external dependencies like JWT”
This saves a second round of token usage.
📁 7. Use a Claude-Facing Knowledge Base
Create a .claude/
directory with shared context Claude can reuse:
project-root/
├── CLAUDE.md # Claude-specific project guide
└── .claude/
├── context.md # Stack, business rules, constraints
├── project-knowledge.md # Technical decisions, patterns
├── common-patterns.md # Reusable prompt snippets
└── debug-log.md # Past issues + solutions
Then reference it in prompts:
“Using the context and coding conventions from
.claude/project-knowledge.md
, implement the user profile API with FastAPI.”
You avoid repeating background details—and avoid repeating tokens.
🧠 TL;DR – the token-saving stack
Technique | Saves Tokens | Improves Speed |
---|---|---|
Specific prompts | ✅✅✅ | ✅✅ |
Decomposed features | ✅✅ | ✅✅✅ |
Minimal file context | ✅✅✅ | ✅ |
Claude chat for design | ✅✅ | ✅✅✅ |
Repomix XML structure | ✅✅ | ✅ |
.claude/ knowledge base |
✅✅✅ | ✅✅ |
Ask for tests early | ✅ | ✅✅ |
For more tips and insights, follow me on Twitter @Siddhant_K_code and stay updated with the latest & detailed tech content like this.
Top comments (0)