Agentic Workflow

How to structure a coding session so the agent stays focused, context stays lean, and you get more done per dollar.

Intermediate 12 min read

Anatomy of an Efficient Session

An efficient agentic coding session has three phases:

Session structure
Phase 1 - Setup (zero or near-zero tokens)
  - CLAUDE.md loaded automatically (cached on first message)
  - Session scope defined (one task, one area)
  - .claudeignore excludes noise directories

Phase 2 - Execution (targeted token use)
  - Agent uses Grep + targeted Read (not broad cat)
  - Tool calls return minimal, relevant results
  - You provide trimmed error messages, not full logs
  - No irrelevant topic switches in this session

Phase 3 - Handoff (minimal output)
  - Ask for diff not full rewrite
  - Review change, confirm correctness
  - Start new session for next task

Before You Start

Three things to do before opening Claude Code for a task:

  1. Define the task boundary - write one sentence: "Fix the email validation bug in UserModel::create() that allows empty strings through." This keeps you from scope-creeping inside the session.
  2. Identify the relevant files - know which 1-3 files the task touches. Add them to your initial message instead of asking the agent to explore.
  3. Check your CLAUDE.md - is it up-to-date for this area? Does it point to the right directory/file for this type of task?
Good first message template
# Template for a focused first message:
"Task: [one sentence]
File: [path, lines if known]
Error: [exact error or failing test name]
Constraint: [e.g. must not break existing tests]
Output: [diff / new function / explanation]"

# Example:
"Task: Fix empty-string validation bypass in user email field.
File: app/models/UserModel.php, create() method ~line 45
Error: UserTest::testCreateWithEmptyEmail passes when it should fail
Constraint: do not change the method signature
Output: diff of the changed validation lines only"

During the Session

Rules for staying efficient once a session is running:

  • One topic per session - resist the temptation to ask a quick unrelated question mid-session
  • Trim before pasting - if you need to paste an error, trim it to 10-20 relevant lines
  • Guide the tool calls - if you see the agent about to read a large file, redirect it: "Read only lines 40-80 of that file"
  • Verify each step - confirm the output of each change before the agent moves forward, to avoid cascading bad changes that generate more output tokens correcting them
  • End decisively - once the task is done, stop. Do not add "while we're here..." requests

Hooks for Automated Context

Claude Code supports hooks - shell commands that run automatically at certain events. Use them to inject focused context without manual effort:

.claude/settings.json - Hooks
{
    "hooks": {
        "PreToolUse": [
            {
                "matcher": "Bash",
                "hooks": [
                    {
                        "type": "command",
                        "command": "echo 'REMINDER: pipe long output to | tail -30'"
                    }
                ]
            }
        ],
        "PostToolUse": [
            {
                "matcher": "Edit",
                "hooks": [
                    {
                        "type": "command",
                        "command": "php -l \"$CLAUDE_TOOL_OUTPUT_FILE\" 2>&1 | tail -5"
                    }
                ]
            }
        ]
    }
}

Common hook patterns for token efficiency:

  • PreToolUse on Bash - remind the agent to pipe output
  • PostToolUse on Edit - auto-run PHP lint on edited files (catches errors immediately, preventing expensive debug loops)
  • UserPromptSubmit - auto-prepend git status to every message so the agent always knows what changed

Memory System

The memory system stores facts that persist across sessions and projects. For token efficiency, use it to remember your preferences so you do not have to repeat them:

Memory entries that save tokens
# Tell Claude Code to remember:
"Remember: always show diffs not full file rewrites"
"Remember: use Grep tool not bash grep"
"Remember: pipe all bash output to | tail -30"
"Remember: I prefer 3-column layouts in all PHP lesson files"
"Remember: no em dash in content, always hyphen"

# These get stored in ~/.claude/projects/[path]/memory/
# and are loaded as context at session start
# Cost: ~500-1000 tokens added to system prompt (cheap vs. repeated corrections)

Daily Workflow Pattern

A professional daily workflow that minimizes token usage while maximizing throughput:

Daily AI coding workflow
Morning planning (no agent, zero tokens):
  1. List today's tasks in a text file
  2. Group by feature area (auth, billing, frontend)
  3. Order by dependency (models before controllers)

Per-task session:
  1. Open new Claude Code session
  2. State the task clearly in first message (use template)
  3. Let agent work - guide tool calls if needed
  4. Review and confirm output
  5. Close session

Batch similar tasks:
  - All model changes -> one session (shared context valid)
  - All test writes   -> one session
  - All CSS fixes     -> one session
  (Don't mix concerns - context stays focused)

End-of-day review (one short session):
  "Show me a git diff summary of today's changes.
   List only files changed, not the full diff."
  -> 1-2 messages, ~500 tokens
Batch similar tasks for cache efficiency

If you are writing tests for 5 PHP models, do them in one session. The system prompt (cached) stays warm across all 5 calls. Each model test adds minimal new input tokens. You get 5 tasks done for roughly 1.5x the cost of one task - because caching pays for the common context.

Frequently Asked Questions

Watch for two signals: (1) the agent starts prefacing responses with summaries of what it did earlier (it is compressing context), and (2) responses become slower. In Claude Code you can see the session token count in the status bar. A practical rule: start a new session after 50-80k tokens or when switching to a different feature area.

Claude Code supports nested CLAUDE.md files. A root-level CLAUDE.md applies project-wide. A CLAUDE.md inside /app/models/ applies only when the agent is working in that directory. Use subdirectory CLAUDE.md files to give area-specific context without bloating the global one.

Hooks are a Claude Code feature - they execute shell commands in response to agent events (before tool use, after tool use, on session start). If you use the raw API, you implement equivalent logic in your own orchestration code (e.g., always prepend the git diff to the user message before each call).

CLAUDE.md is for project-specific facts that apply to all sessions (architecture, conventions). The memory system is for session-to-session preferences and cross-project user preferences. The practical difference: CLAUDE.md is committed to git and shared with the team. Memory is personal and survives across projects.