TL;DR: AI coding agents in large repositories can burn 156 million context tokens and peak at 459k tokens on a single ~800-line pull request, according to Sonar’s analysis, because every blind read is re-billed across hundreds of turns. The key implication is that graph-based code navigation, not grep, becomes the control that limits cost and missed call sites.
At a glance
What this is: This is Sonar’s analysis of how AI coding agents lose efficiency in large repositories when they rely on grep and repeated file reads instead of semantic navigation.
Why it matters: It matters because teams building AI-assisted development workflows need to govern how agents retrieve code context, or they will pay a repeated token tax and miss dependencies that affect build integrity and change safety.
By the numbers:
- On one ordinary ~800-line pull request, the agent added up to 156 million context tokens and a context window that peaked at 459k tokens.
- The session included 512 model round-trips and 152.8 million cache-read tokens, according to Sonar’s measured PR data.
- Across 18 comparable single-ticket PRs, the average was ~234 million context tokens per PR.
👉 Read Sonar’s analysis of why AI coding agents pay a navigation tax in large repos
Context
AI coding agents fail in large repositories for a simple reason: the file system is not a semantic map. When the codebase no longer fits in the model context window, the agent falls back to grep, wider reads, and repeated lookups that stay in the conversation and are re-billed on every later turn. That creates both cost inflation and correctness risk, especially when the agent must reason across call sites, type ownership, and cross-language equivalents.
In identity terms, the governance issue is not whether the model can write code, but whether the agent has controlled access to the right context objects at the right time. That is an NHI-style problem because the agent is a software identity acting inside a change workflow, consuming permissions, tool access, and repository context continuously. The starting position described here is increasingly typical in large codebases, not an edge case.
Key questions
Q: How should security teams limit context sprawl when AI coding agents work in large repositories?
A: Use semantic retrieval instead of broad file reads, and measure how much context each task carries forward. The goal is to keep the agent’s working set narrow enough that irrelevant text does not persist across turns, because every extra read becomes a repeated cost and an expanding error surface.
Q: Why do AI coding agents become expensive in long-running sessions?
A: Because each new turn re-sends prior conversation state, so earlier file reads are paid for again and again. A single over-read can become millions of re-billed tokens if it remains in the session for hundreds of turns, which is why navigation efficiency matters as much as model quality.
Q: What breaks when coding agents rely on grep in multi-language codebases?
A: They miss semantic relationships that text search cannot express, such as owner types, return types, cross-language equivalents, and indirect callers. That leads to broader reads, missed dependencies, and extra CI cycles, all of which increase cost and delay safe delivery.
Q: How can teams tell whether an AI coding workflow is using too much context?
A: Watch for high peak token counts, many model round-trips, repeated whole-file reads, and a large gap between the final diff size and the context carried through the session. If those metrics keep rising, the workflow is spending more on navigation than on actual change work.
Technical breakdown
Why grep breaks down for agentic code navigation
Grep is a string-matching tool, not a semantic resolver. It can return every textual match for a function name, but it cannot tell which definition a call binds to, which type owns the method, or whether the same symbol exists in multiple backends. In a large repository, that forces an AI coding agent to compensate by reading broader file slices and carrying those reads forward in context. The result is not just extra compute. It is a persistent context burden that compounds across every later turn in the session.
Practical implication: replace blind text search with semantic lookup where agents need type-aware or cross-file navigation.
How context re-billing turns every read into a lifecycle cost
In a chat-based coding workflow, every new turn re-sends prior conversation content as input. Prompt caching reduces the unit price of repeated tokens, but it does not remove the repetition itself, so a single over-read continues to cost money on every later turn until the session ends or is compacted. That changes the economics of agentic development: the expensive part is often not the final diff, but the navigation path the agent took to get there. A file read that is slightly too wide can become millions of re-billed tokens.
Practical implication: minimise file-level reads early in the session and use retrieval that returns only the exact node or slice needed.
Why a unified dependency graph is the right control plane
A unified dependency graph turns repository navigation from text search into structured queries. Nodes represent code elements such as functions, classes, fields, and parameters, while typed edges capture calls, returns, references, and relationships across languages and documentation. That lets the agent ask for the exact method body, its owner type, its callers, and its return type without loading whole files or guessing across naming differences. The important change is governance as much as efficiency: the agent receives bounded, semantically relevant context instead of uncontrolled repository surface area.
Practical implication: use graph-backed retrieval for code agents that must operate safely across large, multi-language repositories.
NHI Mgmt Group analysis
Semantic navigation is the missing governance layer for AI coding agents. The article shows that the problem is not model reasoning but how the agent finds the right code, documentation, and cross-language relationships. That makes repository navigation an identity and access problem in practice, because the agent must be constrained to the minimum context needed for each task. For practitioners, the control objective is to reduce uncontrolled context exposure, not just improve search quality.
Token waste is a lifecycle failure, not a one-off inefficiency. When repeated file reads stay in the conversation, they become a recurring cost multiplier for the full duration of the session. That is analogous to standing privilege in identity governance: the harm is not the first access event, but the persistence of access over time. For teams deploying coding agents, the important question is how long context survives and how often it is reused.
Cross-language dependency resolution is a real AI governance gap. The article makes clear that grep fails when the same capability is implemented under different names in different languages. That creates blind spots in change workflows, especially when agents mirror logic across systems or backends. The right concept here is context overread debt: once an agent loads too much irrelevant repository state, the cost and error surface both grow. Practitioners should treat context scope as a governed resource, not an incidental by-product.
Agentic development will increasingly depend on semantic control planes. As codebases, documentation, and tickets grow more interconnected, the ability to retrieve the right node with its relationships becomes a security and reliability issue, not just a productivity feature. This aligns with broader NHI governance thinking: software agents need defined boundaries, observable behaviour, and purpose-limited access. Teams that ignore this will keep paying for inefficiency as if it were inevitable.
The real risk is not that agents read code, but that they read too much of it. Large context windows can mask bad navigation habits by making them look successful until cost, latency, and missed dependencies accumulate. The architectural lesson is that retrieval must be exact enough to preserve meaning and narrow enough to avoid turning every lookup into a long-lived liability. Practitioners should measure context footprint per change, not just completion quality.
What this signals
AI coding agents are starting to look like non-human identities with a growing context lifecycle, which means teams need to govern what they can see, when they can see it, and how long that context persists. The practical signal is that repository navigation will become a measurable control surface, not just a developer convenience.
Context overread debt: the longer an agent keeps irrelevant repository state in session, the more cost, latency, and error risk accumulate. That pattern mirrors other identity security problems where standing access creates compounding exposure, so practitioners should treat context scope as something to be minimised and monitored.
For teams already managing secrets and machine identities, the next step is to connect agent workflow design to established identity controls. OWASP NHI Top 10 and the NIST AI Risk Management Framework are both relevant lenses for defining boundaries around tool use, retrieval scope, and change accountability.
For practitioners
- Implement semantic repository navigation for code agents Replace grep-first workflows with graph-backed retrieval that returns the exact symbol, owner type, callers, and related documentation needed for the task. Use this especially in repositories where a single PR can exceed the model window.
- Measure context footprint per change Track round-trips, peak context size, cache-read tokens, and the ratio of useful lines to file lines read. Treat high context churn as a control failure in the agent workflow, not as an unavoidable side effect.
- Constrain long-lived conversation state Set policies for when sessions should be compacted or restarted so that unnecessary file reads do not remain resident for hundreds of turns. This is the simplest way to reduce re-billed context that compounds over time.
- Map cross-language dependencies explicitly Link equivalent functions, contracts, and documentation across languages so the agent can follow meaning instead of matching strings. This reduces blind spots when the same logic is implemented under different names.
Key takeaways
- AI coding agents do not fail mainly because they cannot reason, but because they navigate codebases inefficiently and carry too much context forward.
- A single unnecessary read can become millions of re-billed tokens over a long session, so context scope is now a real cost and governance variable.
- Semantic graph navigation gives agents the exact code relationships they need, which reduces both blind spots and long-lived context waste.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Agentic AI Top 10 and MITRE ATT&CK address the attack and risk surface, while NIST CSF 2.0 and NIST AI RMF set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST CSF 2.0 | PR.AC-4 | The article is about controlling what an agent can access and retain during code navigation. |
| NIST AI RMF | GOVERN | Agent workflow boundaries and accountability map to AI governance and lifecycle oversight. |
| OWASP Agentic AI Top 10 | NHI-01 | Agentic workflows that overreach on context exposure align with identity and tool-use risk. |
| MITRE ATT&CK | TA0007 , Discovery | The article centres on repository discovery and navigation as the operational behaviour being controlled. |
Treat broad file discovery as a monitored activity and replace it with semantic lookup where possible.
Key terms
- Semantic Repository Navigation: A method for finding code and documentation by meaning rather than by string matching. It uses relationships such as calls, returns, references, and ownership so an AI agent can ask for the exact symbol or dependency it needs instead of reading broad file slices.
- Context Overread Debt: The accumulated cost and risk created when an AI agent loads more context than a task actually needs. Because prior turns remain in session and are re-billed, each unnecessary read can keep costing money, latency, and accuracy for the rest of the workflow.
- Unified Dependency Graph: A structured model of a repository where code elements are nodes and their relationships are typed edges. It lets tools query for definitions, callers, return values, and cross-language equivalents without relying on full-text search or whole-file inspection.
- Agentic Navigation Control: A governance pattern that limits how an AI agent retrieves and retains working context during code changes. It focuses on precision, bounded access, and observability so the agent sees only the relationships needed to complete the task safely.
What's in the full article
Sonar's full analysis covers the operational detail this post intentionally leaves for the source:
- The measured trace data behind the 156 million context-token example, including round-trip counts and cache-read breakdowns.
- The implementation logic behind SemSitter’s Unified Dependency Graph and how it keeps the repository model updated.
- The detailed comparison between grep-based navigation and semantic graph queries across code, documentation, and languages.
- The PR-level examples showing where call-site resolution and cross-language linking changed the agent’s behaviour.
Deepen your knowledge
NHI Foundation Level course, the industry's only accredited NHI security programme, covers NHI governance, machine identity security, and identity lifecycle control. It helps practitioners connect software-agent behaviour to the access and accountability models their programmes already manage.
Published by the NHIMG editorial team on September 2, 2026.
NHI Mgmt Group — the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org