AGENTS.md With LangChain: Loading Rules and Managing Context
LangChain does not read AGENTS.md automatically. Here is how to load your repository rules deliberately into a LangChain agent and verify it follows them.
Updated 5 min read
LangChain does not read AGENTS.md on its own. Unlike Claude Code, which natively loads the file from your repository, a LangChain agent starts from whatever system prompt you give it. Loading your repository rules is a deliberate step you take at agent initialization, and verifying those rules are followed is a step you build into your tests.
This matters because AGENTS.md usually contains instructions that change what the agent should and should not do: which commands to run, which files to avoid, how to format commits. If the file is not loaded, the agent has no reason to follow any of it. Tools like fenbs hold the project context and decisions that change over time, keeping them separate from the static rules in the file.
What AGENTS.md is and why LangChain needs help reading it
AGENTS.md is a plain text file in a repository root that describes rules, constraints, and conventions for an AI agent working in that codebase. It is a convention, not a standard: each tool decides whether and how to load it. Claude Code reads it natively; GitHub Copilot does not recognize it; LangChain has no built-in mechanism for it.
The AGENTS.md examples guide covers what to put in the file. The does Claude Code read AGENTS.md guide covers how Claude Code handles it. This page focuses on LangChain specifically: loading the file, passing it as instructions, and checking that the agent actually respects the scope.
Load the file into the system prompt at initialization
The most direct approach is to read AGENTS.md at startup and include its contents in the system message. LangChain's SystemMessage class accepts any string, so the file content goes in directly.
from pathlib import Path
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
agents_md = Path("AGENTS.md").read_text(encoding="utf-8")
llm = ChatOpenAI(model="gpt-4o")
# Pass rules as the first message in every conversation
system = SystemMessage(content=agents_md)If your AGENTS.md is long, consider whether the full file belongs in the system prompt or whether a shorter summary of the most critical rules serves better. A context window (the total amount of text the model can process at once) has a limit. When the rules plus the task plus the code to review exceed that limit, the model may silently drop the earlier parts, including the rules themselves.
Keep the rules section of AGENTS.md lean for LangChain use: the commands the agent may run, the files it must not touch, the format for commits, and any hard constraints. Move background context, architecture notes, and onboarding material to a separate file that you include only when the agent is doing architecture work.
Verify instruction scope after loading
Loading the file does not guarantee the agent follows every instruction. Models sometimes skip rules that conflict with a user prompt, prioritize later instructions over earlier ones, or misread formatting that was meant to imply hierarchy. Verification is a separate step.
Write a small set of tests that check whether the agent respects your key constraints. Ask it to do something the rules prohibit, and check that it refuses. Ask it to format a commit message, and check that it matches your specified format. These tests catch regressions when you update the model or rewrite the rules.
- Test prohibited actions: ask the agent to modify a file explicitly excluded in AGENTS.md and assert it refuses or asks for confirmation.
- Test required formats: ask for a commit message and compare it against the format your rules specify.
- Test command allowlists: if AGENTS.md lists which commands are safe to run, ask the agent to suggest a command outside that list and check its response.
- Test rule precedence: include a user prompt that contradicts a system rule and confirm the system rule wins.
Handle context window limits for larger AGENTS.md files
If your AGENTS.md has grown large, a few strategies help keep the important rules visible to the model. One is to split the file: put hard constraints in a short RULES.md that always loads, and put contextual notes in the full AGENTS.md that loads only for relevant tasks. Another is to summarize the file at the start of each session with a preprocessing step, keeping only the sections relevant to the current task.
LangChain's RecursiveCharacterTextSplitter can help if you need to retrieve sections of a large file. However, retrieval introduces its own risk: a rule buried in a section the retriever does not surface may never reach the model. For critical constraints, explicit inclusion is safer than retrieval.
The AGENTS.md vs README vs Skills guide covers which information belongs in each file. Keeping AGENTS.md focused on actionable rules and moving background reading elsewhere helps with both loading and verification.
Keep dynamic project context separate from static rules
AGENTS.md holds stable rules: naming conventions, branch policies, prohibited commands. It is not the right place for information that changes session to session: which task is in progress, what was decided in yesterday's code review, where the last session left off. That dynamic context goes stale quickly if it lives in a file.
Mixing stable rules with current project state in one file means every session either gets outdated context or requires someone to update the file before each run. Keeping them separate lets the rules stay accurate for months while the project context updates automatically through a connected tool.
Keep evolving project context out of your static rules file
Connect fenbs to your LangChain agent via MCP from Integrations. At the start of a session, the agent reads the current project rules, Where we left off, and the Decisions and rules your team has recorded. AGENTS.md handles the stable conventions; fenbs handles the state that changes between sessions.
Create a free accountFree for one person · No credit card · Works with Claude, ChatGPT, Cursor and more
How to do this with fenbs
- Create a free account and add the project your LangChain agent is working on.
- Write the key decisions your team has made as Decisions and rules in fenbs: model selection rationale, approved tool lists, any constraints beyond what AGENTS.md covers.
- Connect your agent to fenbs via MCP from Integrations. At the start of each session, ask it to read the project context: Rules first, then Where we left off, then current tasks.
- Load AGENTS.md as the system prompt in your LangChain initialization. Keep the file focused on stable conventions. Let fenbs carry the project state.
- After each session, ask the agent to save progress back to fenbs: which tasks it touched, what it completed, and what remains for the next session.
Common mistakes when loading AGENTS.md in LangChain
Loading the file as a human message instead of a system message is the most common mistake. Models treat system messages as persistent instructions and human messages as inputs to respond to. Rules in a human message may be treated as suggestions rather than constraints.
Another mistake is loading the file once and reusing the same SystemMessage object across sessions that need different context. If the rules file updates, the agent continues using the cached version. Read the file fresh at agent initialization for each session.
Not testing is the third common mistake. Loading AGENTS.md is necessary but not sufficient. The hermes-agent approach covers how to structure agents that use instruction files reliably, including how to write verification steps.