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.

Loading AGENTS.md into a LangChain agent (Python)
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.

How fenbs helps

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 account

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

How to do this with fenbs

  1. Create a free account and add the project your LangChain agent is working on.
  2. 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.
  3. 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.
  4. Load AGENTS.md as the system prompt in your LangChain initialization. Keep the file focused on stable conventions. Let fenbs carry the project state.
  5. 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.

Questions people ask.

Does LangChain automatically read AGENTS.md?

No. LangChain does not scan your repository for any file. You read AGENTS.md with Python's file API and pass its contents explicitly as a SystemMessage at agent initialization. Tools that do auto-read it, like Claude Code, have this built in at the framework level; LangChain does not.

Should AGENTS.md go in the system prompt or as a user message?

In the system prompt, via SystemMessage. System messages are treated as persistent instructions that the model applies throughout the conversation. A human message is treated as input to respond to. Rules placed in a human message are more likely to be interpreted as context than as binding constraints.

What happens if AGENTS.md is too long for the context window?

The model may silently truncate the earlier parts of the context, including your rules. Keep AGENTS.md focused on the critical constraints only, and move background notes and architecture documentation elsewhere. For the rules that matter most, place them near the start of the system prompt where truncation is least likely.

How do I verify my LangChain agent follows the rules in AGENTS.md?

Write targeted tests: ask the agent to perform an action your rules prohibit, and assert it refuses. Ask for a commit message in the format your rules specify, and compare the output. Run these tests when you update the model version or rewrite the rules, since both can change how instructions are interpreted.

Where should current project state live if not in AGENTS.md?

In a tool the agent can read and update at runtime, not in a static file. AGENTS.md is right for stable rules like naming conventions and prohibited commands. For what task is in progress, what was decided yesterday, and where the last session left off, a connected project tool that the agent can query and update is more reliable.

More in Agent harness (for developers)

All Agent harness (for developers) articles

Start with one thing.

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

There is nothing to set up first. Write one line and you’ve started.