Tutorial: .agent folder is making claude code 10x better...
What You'll Learn
- Viewers will learn how to optimize Claude Code's performance using context engineering techniques.
- Viewers will understand how to set up and leverage a comprehensive documentation system for their codebase.
- Viewers will gain strategies for reducing token consumption and improving agent accuracy.
- Viewers will be able to apply sub-agent and documentation strategies in their own projects.
- Viewers will be able to manage larger and more complex codebases with Claude Code more effectively.
In this tutorial, we will explore advanced strategies for optimizing Claude Code's performance through context engineering. This article details how to leverage the .agent folder structure to significantly enhance your AI agent's capabilities. We will cover methods to reduce token consumption, improve agent accuracy, and effectively manage larger codebases. You will learn to implement a comprehensive documentation system and apply sub-agent strategies to streamline your development process.
Step 1: Context Engineering Optimization
Context Engineering Optimization
Context engineering optimizes the information in a conversation history thread to guide an agent's next actions. Claude Code has a 200,000 token context limit.
- Run
/contextsin Claude Code to view a breakdown of token consumption.
To optimize context:
- Identify and remove any unnecessary MCPS (Multi-Context-Prompts) from the agent if they are consuming tokens.
- Ensure that all information in the conversation history is relevant and reduces noise.
- Utilize sub-agents to reduce context by delegating research to an isolated conversation thread. This returns only a summary of the research to the main conversation thread.
- Before implementing a large feature, add a small text prompt to use a task or sub-agent for research first.
- Use the compact command proactively to clean up the conversation thread after an agent completes a set of isolated tasks.
- Set up a documentation system for your codebase.
- Create a
.agentfolder to contain relevant information.- Task Folder: Store PRDs (Product Requirements Documents). When
plan modeis used to generate an implementation plan, save it to the task folder. - System Folder: Store project structure, database schema, APIs, and critical or complex code parts.
- SOPs (Standard Operating Procedures): Log standard processes or mistakes the agent makes. After the agent performs a task, ask it to generate an SOP for that specific process.
- Readme File: This acts as an index for all documentation files, guiding the agent on which file to read and when.
- Task Folder: Store PRDs (Product Requirements Documents). When
- Use the
update doccommand:- It includes instructions for the agent on how to initialize the documentation structure and update documentation.
- Run this command after implementing features or correcting agent mistakes.
- Example setup for a new project:
- Create a
claude.mdfile. - Add a section for
docsexplaining the desired documentation structure. - Include rules that the agent folder docs are always updated after implementing features and that the
readmeis read first before planning any implementation to get the full context. - Create a
doc claude folder commands update doc.MDfile. This is a simple prompt that tells the agent about the doc structure, what to do when initializing docs, what to do when asked to update docs, and specific rules for creating new doc files. - Run
/update doc initialize. This will set up the.agentfolder structure and create aproject architecturedocument as the first doc. Areadmefile will also be created under the.agentfolder, listing all existing docs.
- Create a
- Example usage:
- Copy a doc and give the prompt: "How we build a text to image app using model above".
- Use
plan mode. - After the plan is finished, prompt to "save implementation plan in a
do agent/taskfolder and start implementation". - If the model fails, use the
update doccommand, e.g., "generate SOP integrating replicate model". This will create an SOP explaining the integration process, directory structure, and related documentation. It will also update thereadme. - To add a new capability, clear the conversation and give a prompt like: "help me add text to video capability using model above plan implementation and read
do agentdoc first for context". Useplan mode. - Save the implementation plan to the
/taskfolder and start implementing.
- To apply to an existing codebase, ask Claude Code to generate system architecture documentation, including text project architecture, database schema, API endpoints, and more.
- Create a
Step 2: Documentation System for Cloud Code
Documentation System for Cloud Code
The purpose of creating a documentation system is to generate summarized snapshots of your codebase. This allows the agent to read summarized documentation instead of performing deep research across the entire codebase. This approach reduces noise in the context window and ensures that relevant information is fed into the context, especially as your codebase grows larger.
The common structure for a documentation system is composed of the following folders and files:
.agent Folder Structure
This folder contains all relevant and useful information.
-
taskFolder:- This is where Product Requirement Documents (PRDs) are stored.
- To create a PRD, turn on plan mode before implementing any feature and ask the agent to generate an implementation plan.
- After the implementation plan is generated, store it in the
taskfolder. This allows you to link to these implementation documents as a reference for similar future implementations.
-
systemFolder:- This folder contains information such as project structure, database schema, APIs, or other critical and complex parts of your codebase.
- These items can span across different individual PRDs and are useful for the agent to gain an overall understanding. This is also the part that can grow significantly.
-
SOPs(Standard Operating Procedures) Folder:- This is where standard processes for performing certain tasks or mistakes made by the agent are logged.
- After an agent completes a task, ask it to generate an SOP for that specific process.
- SOPs can be generic, such as "adding a new database table," or more granular, such as "how to integrate a new replicate model."
-
READMEFile:- As your codebase becomes more complicated, there will be many documentation files.
- The
READMEfile acts as an index of all documentation files, indicating when to read which documentation. This helps the agent quickly get an overview of all relevant documentation and read injection history.
Setting Up Your Documentation System
Perform the following steps to set up your documentation system in a new project:
-
Create
cloud.mdfile:- Add a section specifically explaining the documentation structure you want the agent to follow.
- Include rules such as "always update agent folder docs after implementing certain features" and "before planning any implementation, always read the
READMEfirst to get the full context."
-
Create
doc_cloud_folder_commands_update_doc.MD:- This file contains a simple prompt that specifically tells the agent about the documentation structure.
- It also defines what to do when initializing the documentation and what to do when asked to update the documentation, along with specific rules for creating new documentation files.
Updating Documentation
To continuously update your documentation, follow these steps:
- Initialize the documentation structure:
- Run the command:
/update doc initialize - This command will scan your current project and set up the
.agentfolder structure. - Upon completion, you will see the
.agentfolder created, withproject_architectureas the first document. - A
READMEfile will also be present under the.agentfolder, listing all current documents.
- Run the command:
Example Workflow
Consider the following example:
-
Building a text-to-image app:
- Provide a prompt like: "How do we build a text-to-image app using the model above?"
- Use plan mode.
- After the plan is complete, prompt to save the implementation plan in the
.agent/taskfolder and then begin implementation.
-
Generating an SOP for model integration:
- If a model fails to integrate, use the
update doccommand to generate an SOP. - For example:
generate SOP integrating replicate model. - This will create a
replicated_model_integration_SOPdocument. - This document will explain the step-by-step integration process, the directory structure to follow, and a section for related documentation.
- The
READMEwill also be updated to include this new SOP.
- If a model fails to integrate, use the
-
Adding new capabilities with existing context:
- Clear the conversation if needed.
- Provide a prompt, for example, for adding text-to-video capability: "help me add text-to-video capability using model above plan implementation and read
.agentdoc first for context." - Use plan mode.
- The agent will read through different files to get the full picture.
- Save the implementation plan to the
/taskfolder and start implementation.
This approach allows for consistent performance and confidence when implementing new features, by leveraging a well-structured and continuously updated documentation system. You can apply the same process to your existing codebase by asking Cloud Code to generate system architecture documentation, including project architecture, database schema, and API endpoints.
Key Concepts
- Context Engineering
- Token Consumption
- Sub-agents
/compactCommand.agentfolder structure (task, system, SOPs, readme)- PRDs (Product Requirement Documents)
- SOPs (Standard Operating Procedures)
update docCommand- AI Agents Unleashed Report
Conclusion
This tutorial guided you through optimizing Claude Code by implementing context engineering techniques and a robust documentation system. We covered strategies for token consumption reduction, improving agent accuracy, and leveraging sub-agents within the .agent folder structure. By applying these methods, you can manage complex codebases more effectively and enhance your project's overall performance.