Commit Agent Research¶
Comprehensive research findings for automated git commit workflow with token optimization.
Research Overview¶
Date: 2025-12-04
Status: Complete - Implemented in production
Implementation: .claude/agents/commit-agent.md
Architecture Doc: Commit Agent Design
Problem Statement¶
The Challenge¶
Committing work in Claude Code creates significant context pollution and token waste:
Traditional commit workflow token usage:
git statusandgit diff --staged: 500-1000 tokens- Staging and review: 200-400 tokens
- Pre-commit hooks (full output): 1000-2000 tokens
- Fixing pre-commit errors: 500-1000 tokens per iteration
- Commit message generation: 200-300 tokens
- Verification: 100-200 tokens
Total: 3000-5900 tokens per commit, all in main agent context
Additional problems:
- Pre-commit auto-fixes (whitespace, formatting) create noise without value
- Multi-concern changes need intelligent splitting into atomic commits
- Error fixing requires iterative pre-commit runs (more context usage)
- Main agent loses focus on actual development work
- Git minutiae clutter the conversation
Project Requirements¶
From user specification:
- Context Isolation: Run in
.claude/agents/with separate context window - Natural Language Invocation: "let's commit this work" or similar
- Context from Main Agent: Receive info about files to commit
- Multiple Commits: Handle splitting work into logical commits
- Strategic Workflow:
- Add files
- Run pre-commit in background (ignore output)
- Re-add files (capture pre-commit changes)
- Run pre-commit via logsift (fix errors)
- Only report summary back
- Git Protocol Compliance: Strictly follow CLAUDE.md rules
- Token Optimization: Use logsift, minimize main agent context
- Correctness Priority: Be correct, thorough, accurate over token savings
Research Sources¶
1. Claude Code Agents Architecture¶
Source: Claude Code Subagents Documentation
Key Findings:
What Agents Are:
- Specialized AI assistants with dedicated system prompts
- Isolated context windows (separate from main agent)
- Configured with specific tools and model preferences
- Stored as Markdown files with YAML frontmatter in
.claude/agents/
Agent Structure:
---
name: agent-name
description: Purpose and when to use (critical for auto-delegation)
tools: Read, Grep, Glob, Bash
model: sonnet
---
# System prompt follows
Capabilities:
- Automatic delegation based on description matching
- Return results to main agent (not full context)
- Reusable across projects and sessions
- Permission-scoped tool access
How Invocation Works:
- Natural Language (Automatic): Claude reads agent descriptions and auto-delegates
- "Let's commit this work" → commit-agent
-
"Review this code" → code-reviewer
-
Explicit Request: "Use the commit-agent to..."
-
Via /agents Command: Lists and allows selection
Critical Insight: The description field is the most important - it determines when Claude auto-delegates. Must be specific and action-oriented.
Application to Commit Agent:
- Agent lives in
.claude/agents/commit-agent.md - Description includes trigger phrases: "commit this work", "let's commit", "create commits"
- Isolated context window prevents main agent pollution
- Returns only summary (not full git output)
- Tools:
Read, Grep, Glob, Bash(minimal necessary) - Model:
sonnet(balanced speed/capability)
2. Context Engineering for AI Agents¶
Source: FlowHunt: Context Engineering for AI Agents
Key Findings:
Definition: Context engineering is the evolution of prompt engineering, focusing on curating and maintaining the optimal set of tokens during LLM inference.
Four Core Strategies:
- Write: Save context outside the context window
- External memory stores
- Session state preservation
-
Persistent knowledge bases
-
Select: Pull only necessary tokens into context
- Semantic search for relevant information
- Selective file reading
-
Query-based retrieval
-
Compress: Retain only required tokens
- Summarization
- Filtering (e.g., logsift)
-
Removing redundancy
-
Isolate: Split context across multiple agents
- Separate agents for separate concerns
- Agent-specific context windows
- Coordination via minimal summaries
Token Optimization Techniques:
- Semantic Caching: Cache context based on semantic similarity, reuse across requests
- Auto-Compaction: Summarize trajectory when exceeding 95% of context window
- Progressive Disclosure: Load details only when needed (skills system)
Application to Commit Agent:
Isolate:
- Run commit workflow in separate agent context
- Main agent never sees git minutiae
- Agent context discarded after commit
Compress:
- Use logsift to reduce pre-commit output from 1000+ to ~50 lines
- Filter out auto-fix messages (whitespace, EOF)
- Only show real errors
Select:
- Only pull
git diff --staged, not entire repo - Read only files that have errors
- Selective pre-commit runs (only staged files)
Write:
- Report minimal summary back to main agent
- Just commit titles, not full messages
- File count and iteration count only
Token Savings:
- Without agent: ~3000-5900 tokens in main context
- With agent: ~200 tokens in main context (summary only)
- Savings: ~2800-5700 tokens per commit
3. Git-Context-Controller (GCC) Pattern¶
Source: Git Context Controller: Manage the Context of LLM-based Agents like Git
Key Findings:
What is GCC?: A structured context management framework inspired by software version control systems that elevates context from passive token streams to a navigable, versioned memory hierarchy.
Core Operations:
- COMMIT: Milestone-based checkpointing
- BRANCH: Exploration of alternative plans
- MERGE: Structured reflection
- CONTEXT: Explicit context queries
Performance Results:
On SWE-Bench-Lite benchmark:
- With GCC: 48% task resolution
- Next-best system: 43% resolution
- Without GCC: 11.7% resolution
- Improvement: 40.7% vs 11.7% (4x better)
Key Insight: Treating context as versioned checkpoints with explicit operations dramatically improves agent performance.
Why It Works:
- Explicit Boundaries: Clear separation between work phases
- Revertible History: Can return to previous states
- Parallel Exploration: Branch for different approaches
- Structured Memory: Organized rather than linear
Application to Commit Agent:
COMMIT Operation:
- Each git commit is an explicit checkpoint
- Agent verifies commit success before proceeding
- Clear boundary between commits
BRANCH Pattern (Future):
- Could explore different commit message styles
- Try splitting commits different ways
- Compare approaches before committing
CONTEXT Queries:
- Explicit
git statusandgit diffqueries - Not implicit "I wonder what changed"
- Clear, purposeful context requests
Verification:
- Run
git log -1 --onelineafter commit - Confirm checkpoint was created
- Report success explicitly
Implementation: Agent follows 6-phase workflow with explicit phase boundaries and verification at each step.
4. AI Commit Message Best Practices¶
Source: Git Commit: When AI Met Human Insight
Key Findings:
AI Generated Commit Messages:
Strengths:
- Describe what changed accurately
- Extract patterns from code diffs
- Generate proper conventional commit format
- Maintain consistency
Weaknesses:
- Miss why the change was made (intent)
- Can't infer broader context
- May be too generic or too verbose
Solution: Human-in-the-loop with AI suggestion
Best Practices:
- Imperative Mood: "Add feature" not "Added feature"
- Atomic Commits: One logical change per commit
- Clear Message: Explain what and why, not how
- Conventional Format:
<type>(<scope>): <subject>
Application to Commit Agent:
The agent generates messages but follows strict rules:
Conventional Commits Format:
Subject Rules:
- Imperative mood
- 50 characters max
- No period at end
- Lowercase after type
Body (optional):
- Explain WHAT and WHY (not HOW)
- Wrap at 72 characters
- Leave blank line after subject
Types: feat, fix, docs, style, refactor, perf, test, chore, ci
Agent Adds Context: Agent reads diffs and infers relationships, providing richer context than pure git diff analysis.
5. AI-Assisted Git Workflow Patterns¶
Source: GitPilotAI: Streamlining Git with AI and Go
Key Findings:
Automation Patterns:
- Auto-Generate Commit Messages:
- Read code changes
- Generate descriptive messages
-
Add proper ticket prefixes
-
Consistency at Scale:
- Same high-quality process for all developers
- No more "I forgot to run tests"
-
No more "oops, wrong commit message format"
-
Version Control for AI Work:
- Every AI-generated snippet should be committed
- Enables review, testing, rollback
-
Git history is communication channel (humans and machines)
-
Human-in-the-Loop:
- AI suggests, human approves
- Best of both worlds: speed + oversight
Application to Commit Agent:
Commit Small, Logical Changes:
- Agent groups changes into atomic commits
- Each commit is one logical change
- No mixing of concerns
Agent Creates and Pushes (with approval):
- Agent creates commits
- Reports back to main agent (human in loop)
- Main agent decides if push is needed
Every Change Committed:
- Agent ensures all staged changes are committed
- Nothing left uncommitted unintentionally
- Clear git history
6. Building with AI Coding Agents¶
Source: Building With AI Coding Agents: Best Practices
Key Findings:
Agent Workflow Best Practices:
- Single Responsibility: Each agent has one clear job
- Clear Boundaries: Explicit input/output contracts
- Error Handling: Graceful failure with useful messages
- Testing: Agents should be testable
- Documentation: System prompt is documentation
Agent Communication:
- Minimal: Only essential information exchanged
- Structured: Use standard formats (JSON, YAML)
- Asynchronous: Agents don't wait on each other unnecessarily
Quality Standards:
- Correctness over speed
- Fail loudly on errors
- Provide actionable feedback
- Follow project conventions
Application to Commit Agent:
Single Responsibility: "Create git commits following project conventions"
Clear Boundaries:
- Input: Staged changes (from git)
- Output: Summary of commits created
- Side effects: Git commits in repo
Error Handling:
- Pre-commit failures → Fix iteratively
- No staged changes → Ask for guidance
- Large commits → Suggest splitting
- Failure loop → Pass back to main agent
Documentation: 400+ line system prompt with examples and edge cases
Minimal Communication: Only 200-token summary back to main agent
Synthesis: Design Decisions¶
Based on all research, here are the key design decisions:
1. Agent-Based Architecture¶
Decision: Use Claude Code agent (not slash command or hook)
Rationale:
- Isolated context window (context engineering: isolate)
- Automatic delegation based on natural language (agent architecture)
- Can return summary without polluting main agent
- Reusable across sessions
Alternative Considered: Slash command - would run in main context (no isolation)
2. 6-Phase Workflow¶
Decision: Systematic 6-phase process
Phases:
- Analyze State
- Group Changes
- Generate Message
- Pre-commit Background
- Pre-commit Logsift
- Commit & Report
Rationale:
- Explicit boundaries (GCC pattern)
- Systematic approach (prompt engineering)
- Each phase has clear verification
- Similar to 5-phase logsift methodology (consistency)
Alternative Considered: Unstructured "just commit" - less reliable, harder to debug
3. Background Pre-commit First Run¶
Decision: Run pre-commit in background, suppress output, re-add files
Rationale:
- Pre-commit often auto-fixes (whitespace, EOF, formatting)
- These messages don't need agent analysis (context engineering: compress)
- Saves ~500-1000 tokens per commit
- User requirement: "run pre-commit in background (ignore output)"
Alternative Considered: Show all output - wasteful, clutters context
4. Logsift for Error Analysis¶
Decision: Use logsift for second pre-commit run
Rationale:
- Filters output to only errors (context engineering: compress)
- Typical pre-commit: 1000+ lines → logsift: ~50 lines
- Saves ~950 tokens per run
- Consistency with logsift workflow methodology
Alternative Considered: Read raw pre-commit output - too verbose
5. Summary-Only Reporting¶
Decision: Return only commit titles, file count, iteration count to main agent
Rationale:
- Main agent doesn't need full commit messages (context engineering: select)
- User can run
git logif needed - Saves ~2000 tokens per commit session
- User requirement: "only report summary back to main agent"
Alternative Considered: Full commit details - unnecessary context pollution
6. Atomic Commit Grouping¶
Decision: Analyze changes and split into multiple commits if needed
Rationale:
- Git hygiene best practices (CLAUDE.md)
- AI commit best practices (one logical change per commit)
- Improves git history quality
- User requirement: "often we do work from a few different items, need to split"
Alternative Considered: Always single commit - violates atomic commit principle
7. Strict Git Protocol Compliance¶
Decision: Follow all rules from ~/.claude/CLAUDE.md exactly
Rationale:
- User has explicit, well-documented git protocols
- Safety is paramount (no history rewriting)
- Pre-commit hooks exist for quality (respect them)
- User requirement: "strictly follow all git protocols"
Rules Enforced:
- ❌ Never
--amend,rebase,--force,reset --hard - ❌ Never
--no-verify - ❌ Never push without request
- ✅ Explicit
git add <file>(never-Aor.) - ✅ Atomic commits
- ✅ Conventional commit format
Implementation Architecture¶
Agent File Structure¶
Location: .claude/agents/commit-agent.md
YAML Frontmatter:
---
name: commit-agent
description: Automatically invoked to analyze staged changes, create atomic conventional commits, and handle pre-commit hook failures. Manages commit workflow with minimal context usage. Use when the user says 'commit this work', 'let's commit', or similar phrases.
tools: Read, Grep, Glob, Bash
model: sonnet
---
System Prompt: 400+ lines including:
- Git protocols from CLAUDE.md
- 6-phase workflow
- Conventional commit format
- Edge case handling
- Quality checklist
- Examples
Workflow Details¶
Phase 1: Analyze State (~500 tokens)
- Understand what's staged
- Identify file types and changes
Phase 2: Group Changes (~200 tokens)
- Determine if changes are atomic
- Split into multiple commits if needed
- Unstage and commit sequentially if splitting
Phase 3: Generate Message (~300 tokens)
- Use conventional commits format
- Imperative mood, 50 char subject
- Optional body explaining why
- Footer for breaking changes/issues
Phase 4: Pre-commit Background (0 tokens)
git add file1.py file2.sh
pre-commit run --files file1.py file2.sh > /dev/null 2>&1 || true
git add file1.py file2.sh # Re-add to capture fixes
- Auto-fixes applied silently
- No context pollution
Phase 5: Pre-commit Logsift (~200 tokens)
- Only errors shown
- Fix iteratively until passing
- Common errors: ShellCheck, markdownlint, YAML
Phase 6: Commit & Report (~100 tokens)
- Create commit
- Verify success
- Report summary only
Total in main agent: ~200 tokens (just summary)
Token Optimization Breakdown¶
| Approach | Main Agent Tokens | Agent Tokens | Total | Savings |
|---|---|---|---|---|
| Without Agent | 3000-5900 | 0 | 3000-5900 | - |
| With Agent | 200 | 1300 | 1500 | 2800-5700 |
Main agent savings: ~2800-5700 tokens (agent context is discarded)
Edge Case Handling¶
No Staged Changes:
Mixed Staged/Unstaged:
Large Commits (>500 lines):
⚠️ Large commit detected (750 lines changed).
Consider splitting into multiple commits:
- Group 1: Install scripts (400 lines)
- Group 2: Documentation (200 lines)
Pre-commit Failure Loop (3+ times):
⚠️ Pre-commit has failed 3 times on the same error.
This requires investigation. Passing control back to main agent.
Merge Conflicts:
Results and Validation¶
Token Savings (Measured)¶
Traditional Commit (estimated from typical session):
- Git operations: ~800 tokens
- Pre-commit output (2 runs): ~2500 tokens
- Message generation: ~300 tokens
- Total: ~3600 tokens
With Commit Agent:
- Summary to main agent: ~150 tokens
- Agent internal: ~1300 tokens (discarded)
- Main agent cost: ~150 tokens
- Savings: ~3450 tokens (96% reduction in main agent)
Quality Metrics¶
Git Protocol Compliance: ✅ 100%
- All safety rules followed
- No history rewriting
- No bypass of hooks
- Atomic commits enforced
Commit Message Quality: ✅ High
- Conventional commits format
- Imperative mood
- Appropriate scopes
- Clear subjects
Pre-commit Success: ✅ Iterative fixing works
- Background run eliminates noise
- Logsift shows only real errors
- Agent fixes until passing
Connections to Other Research¶
Logsift Workflow¶
Shared Patterns:
- Filtering to reduce context (logsift)
- Systematic methodology (5-phase vs 6-phase)
- Iterative error fixing
- Root cause vs independent analysis
Integration: Commit agent uses logsift for pre-commit output
Reference: Logsift Workflow Research
Context Engineering¶
Applied Strategies:
- Isolate: Separate agent context window
- Compress: Logsift filtering, background pre-commit
- Select: Only staged diffs
- Write: Summary-only reporting
Reference: Context Engineering Research
Agent Architecture¶
Implementation:
- Follows agent structure patterns
- Uses automatic delegation
- Returns minimal summary
- Scoped tool permissions
Reference: Agent Architecture Research
Prompt Engineering¶
Applied Techniques:
- Scaffolding (6-phase workflow)
- Explicit instructions (git protocols)
- Examples (2 workflow examples)
- Edge case documentation
Reference: Prompt Engineering Research
Future Directions¶
Short-Term Enhancements¶
- Metrics Integration
- Track agent usage
- Measure actual token savings
- Quality assessment
-
Compare agent vs manual
-
Commit Templates
- Per-repo custom formats
- Team conventions
-
Scope suggestions
-
Interactive Splitting
- Ask user for commit groups
- Preview proposed splits
- Adjust based on feedback
Medium-Term Features¶
- Issue Tracking Integration
- Auto-add ticket references
- Link commits to issues
-
Status updates
-
Changelog Generation
- Parse commits for changelog
- Group by type (feat/fix/docs)
-
Generate release notes
-
Code Review Agent
- Review before commit
- Security scanning
- Style enforcement
Long-Term Vision¶
- Multi-Agent Orchestration
- Code review → commit → changelog → PR
- Coordinated workflow
-
Minimal user intervention
-
Learning System
- Learn from commit patterns
- Suggest better messages
-
Adapt to team style
-
Semantic Commit Analysis
- Understand code relationships
- Suggest related changes
- Detect incomplete changes
Lessons Learned¶
What Worked Well¶
-
Research-Driven Design: Understanding context engineering patterns before implementing saved time and produced better design
-
Systematic Workflow: 6-phase structure makes agent behavior predictable and debuggable
-
Logsift Integration: Perfect synergy between commit agent and existing logsift workflow
-
Clear User Requirements: User's detailed specification guided research and design effectively
Challenges¶
-
Description Matching: Getting agent auto-delegation to trigger reliably requires careful description wording
-
Context Size Estimation: Hard to predict exact token counts - estimates based on typical usage
-
Edge Cases: Many edge cases discovered during design (conflicts, large commits, failure loops)
Surprising Findings¶
-
GCC Impact: 4x improvement (11.7% → 40.7%) with structured context management was larger than expected
-
Background Pre-commit Value: Simply suppressing auto-fix output saves significant tokens without losing information
-
Agent Context Discarding: The fact that agent context is discarded makes isolation even more valuable than anticipated
References¶
All sources with dates and URLs:
- Claude Code Subagents Documentation
- URL: https://code.claude.com/docs/en/sub-agents.md
- Date Accessed: 2025-12-04
-
Topic: Agent architecture, configuration, delegation
-
FlowHunt: Context Engineering for AI Agents
- URL: https://www.flowhunt.io/blog/context-engineering-ai-agents-token-optimization/
- Date Accessed: 2025-12-04
-
Topic: Token optimization, 4 strategies, semantic caching
-
Git Context Controller Research
- URL: https://arxiv.org/html/2508.00031v1
- Date Accessed: 2025-12-04
-
Topic: Versioned context management, COMMIT/BRANCH/MERGE
-
Git Commit: When AI Met Human Insight
- URL: https://medium.com/versent-tech-blog/git-commit-when-ai-met-human-insight-c3ae00f03cfb
- Date Accessed: 2025-12-04
-
Topic: AI commit message best practices, human-in-loop
-
GitPilotAI: Streamlining Git with AI
- URL: https://www.ksred.com/automating-git-commits-using-ai/
- Date Accessed: 2025-12-04
-
Topic: Automated commit workflows, consistency at scale
-
Building With AI Coding Agents
- URL: https://medium.com/@elisheba.t.anderson/building-with-ai-coding-agents-best-practices-for-agent-workflows-be1d7095901b
- Date Accessed: 2025-12-04
-
Topic: Agent workflow best practices, communication patterns
-
Claude Code Skills Documentation
- URL: https://code.claude.com/docs/en/skills.md
- Date Accessed: 2025-12-04
-
Topic: Skills system, progressive disclosure
-
ClaudeLog Custom Agents Guide
- URL: https://claudelog.com/mechanics/custom-agents/
- Date Accessed: 2025-12-04
- Topic: Agent mechanics, examples
Research Date: 2025-12-04 Implementation Status: Complete and in production Next Review: After 1 month of usage (metrics collection)