<--- Back to all resources
The Context Layer: What AI Agents Need Beyond Raw Data
Raw data isn't enough for AI agents. They need business context: what metrics mean, which tables to trust, how your company defines success. Here's what a context layer looks like and why it matters.
Give an AI agent access to your company’s database and ask it a question about revenue. It will find tables, join them, aggregate numbers, and return an answer. That answer will almost certainly be wrong.
Not because the model is bad. Not because the SQL is broken. The answer is wrong because the agent doesn’t understand what “revenue” means at your company. It doesn’t know which table is the source of truth. It doesn’t know that trial users shouldn’t be counted, that revenue is recognized ratably, or that the orders table includes test data that should be filtered out.
This is the context problem. Even OpenAI discovered this the hard way when building their own internal data agent. Despite having the most capable models available, their agent produced wrong results until they wrapped it in six layers of context: table usage patterns, human annotations, automated code enrichment, institutional knowledge, memory from past interactions, and runtime context. As they put it, “without context, even strong models can produce wrong results.” The model wasn’t the bottleneck. Context was.
Raw data without business context is just numbers in columns. And agents, no matter how sophisticated the underlying model, cannot reliably infer business context from column names and table structures alone.
What a Context Layer Actually Contains
The term “context layer” gets thrown around loosely. Let’s be specific about what it includes and why each component matters for agent accuracy.
1. Semantic Definitions
Every company has its own vocabulary. “Customer” means something different at every organization. At a SaaS company, is a customer someone who signed up, someone who’s paying, or someone who’s been paying for more than 30 days? Are partner-managed accounts customers? What about internal test accounts?
Semantic definitions answer these questions explicitly. They map business terms to precise data definitions:
- Customer: An account in the
accountstable wherestatus = 'active'ANDaccount_type != 'internal'ANDcreated_atis more than 30 days ago - Revenue: Sum of
amountfrominvoicestable wherestatus = 'paid'ANDinvoice_type = 'subscription', excluding refunds processed in the same period - Churn: Accounts that transitioned from
status = 'active'tostatus = 'cancelled'within the reporting period, measured at the end of each calendar month
Without these definitions, agents improvise. They see a customers table and assume every row is a customer. They see an amount column and assume it’s revenue. These assumptions are wrong often enough to make the agent unreliable.
2. Metric Calculation Logic
Definitions tell the agent what a metric means. Calculation logic tells it how to compute the number, including edge cases that aren’t obvious from the definition alone.
Take churn rate. The definition says “accounts that cancelled divided by total accounts.” But the calculation has nuances:
- Is the denominator the count at the start of the period, the end, or the average?
- Do you count downgrades (Enterprise to Starter) as churn?
- If a customer cancels and resubscribes within the same month, do they count as churned?
- Are accounts on annual contracts measured differently from monthly contracts?
Experienced analysts know these rules. They’ve been burned by wrong calculations before and have learned the company’s conventions. Agents need this knowledge spelled out explicitly, as structured logic they can follow for every calculation.
3. Data Source Mappings
This is where things get messy, and where agents fail most often. Every company has multiple tables that could plausibly be the source of truth for any given entity. Customer data might live in:
- The billing system’s
customerstable - The CRM’s
accountstable - The product’s
userstable - A
dim_customerstable in the warehouse that someone built two years ago and may or may not be current
Humans navigate this through institutional knowledge. “Oh, for billing questions use the Stripe data. For feature usage, use the product database. For account ownership, use Salesforce.” An agent without data source mappings will pick whichever table it finds first, and the answer depends on which table that happens to be.
Data source mappings specify, for each business entity and use case, which table is authoritative. They also document known issues: “The dim_customers table has a 24-hour lag because it’s rebuilt nightly” or “The users table includes admin accounts that aren’t real customers.”
4. Business Rules
Business rules are the exceptions and conventions that don’t fit neatly into metric definitions but materially affect calculations. Examples:
- Returns processed within 30 days of purchase are subtracted from the original period’s revenue, not the current period
- Employees get free accounts that should be excluded from all customer counts and revenue figures
- The APAC region reports in USD using the exchange rate on the first business day of each month
- Deals over $100K require two approvals and may appear in the pipeline before they’re confirmed
- Q4 revenue recognition follows different rules due to year-end adjustments
These rules are scattered across email threads, Slack channels, wiki pages, and the memories of specific people. Nobody has ever written them all down in one place. But every wrong answer an agent gives because it didn’t know a business rule erodes trust in the entire system.
5. Freshness Requirements
Different use cases have fundamentally different freshness needs, and agents need to know what’s acceptable:
- Inventory levels: Must be real-time (within seconds). Decisions based on stale inventory lead to overselling or missed restocking
- Revenue for board reporting: Daily is fine. Nobody makes decisions on quarterly revenue that require sub-second accuracy
- Customer support context: Near real-time. When a customer calls about an order placed 10 minutes ago, the agent needs to see it
- Marketing campaign performance: Hourly is usually sufficient for optimization decisions
Freshness requirements tell the agent whether the data it’s looking at is fresh enough for the question being asked. If an agent is answering an inventory question with data that’s six hours old, it should flag that rather than returning a stale number with false confidence.
The Stale Context Problem
Here’s what most discussions about context layers miss: the context itself goes stale.
Traditional approaches treat the context layer as a static artifact. Someone writes the metric definitions, documents the business rules, maps the data sources. Then the document sits in a wiki and slowly drifts from reality.
In practice, context changes constantly:
- A new product launches. The revenue definition needs to include it. The semantic definition of “customer” may need to expand. New tables appear in the database.
- The finance team changes how they calculate ARR. They switch from calendar-month to anniversary-month recognition. Every metric that depends on ARR is now wrong if the context layer doesn’t reflect the change.
- An acquisition closes. Two separate customer tables need to be merged. The data source mappings are now incorrect.
- A schema migration renames
user_idtoaccount_id. Every context layer reference to the old column name breaks. - The business adds a new region. Business rules around currency conversion, tax calculation, and reporting periods need to be updated.
If the context layer is updated quarterly, it can be wrong for up to three months. That’s three months of agents giving incorrect answers, three months of eroded trust, three months where every stakeholder who gets a bad answer files it away as evidence that “AI doesn’t work.”
Streaming the Context Layer
This is where Change Data Capture becomes relevant not just for data freshness but for context freshness.
CDC doesn’t only capture row-level changes. It also captures schema changes. When a column is added, renamed, or dropped, CDC sees it. When a new table is created, CDC can detect it. When table comments or column descriptions change, those metadata events flow through the same pipeline.
A streaming context layer uses CDC to keep itself current:
- Schema evolution events: When the source database changes, the context layer automatically updates its data source mappings. A new column in the
orderstable triggers a review of whether it should be included in revenue calculations. - New table detection: When a new product launches and a new billing table appears, the context layer flags it for integration. An agent querying revenue can be notified that a new revenue source exists that isn’t yet mapped.
- Metadata propagation: Column descriptions, table comments, and relationship constraints defined in the source database flow to the context layer automatically. Database teams that document their schemas well get an automatic context layer.
- Freshness monitoring: CDC provides real-time visibility into data latency. The context layer knows exactly how current each data source is and can communicate that to agents.
The architecture looks like this:
Source Databases (PostgreSQL, MySQL, MongoDB) generate changes. CDC pipelines (Streamkap) capture those changes, including both data changes and schema changes. The Context Layer receives schema events and updates its definitions, mappings, and rules accordingly. An Agent Interface (MCP server, API, tool calling) gives agents structured access to both the context layer and the fresh data.
Building a Context Layer: Practical Steps
You don’t need to build everything at once. Start with the highest-impact components and expand from there.
Step 1: Document your top 20 metrics. Pick the metrics that agents (or humans) ask about most often. For each one, write the definition, calculation logic, source table, and known edge cases. This alone will eliminate a large percentage of agent errors.
Step 2: Map your critical data sources. For each major business entity (customers, orders, products, employees), identify which table is authoritative and which tables are secondary or derived. Document the freshness of each source.
Step 3: Set up real-time data infrastructure. Use CDC to stream changes from your production databases to your analytical stores. This eliminates stale data as a failure mode and provides the foundation for a streaming context layer. Streamkap can have this running in under an hour.
Step 4: Make the context layer queryable. Don’t bury your definitions in a wiki. Put them in a structured format (JSON, YAML, or a database) that agents can query programmatically. Every agent query should first hit the context layer to understand what it’s being asked, then hit the data to compute the answer.
Step 5: Connect CDC to the context layer. As schemas evolve and new tables appear, the context layer should update automatically. Schema change events from your CDC pipeline trigger context layer reviews and updates.
What’s Different About Agent Consumers
It’s worth stepping back and asking: why didn’t we need a context layer before? BI tools and human analysts have worked with raw data for decades.
The answer is that humans are a natural context layer. An analyst who’s been at your company for a year has absorbed all this context implicitly. They know which tables to trust. They know the business rules. They know when data looks wrong because they’ve developed intuition about what “right” looks like.
Agents don’t have this. Every query is their first query. They have no institutional memory, no intuition about what numbers should look like, no ability to call a colleague and ask “which table should I use for this?” They need everything to be explicit and structured.
This is also why agents have a higher freshness requirement than human analysts. A human analyst looking at a dashboard knows it was last updated at 6 AM and mentally adjusts for anything that’s happened since. An agent treats the data as current and makes decisions accordingly. If the data is stale, the agent doesn’t know and doesn’t compensate.
The shift from human consumers to agent consumers is what makes context layers go from “nice to have” to “required infrastructure.” Without one, you have an agent that’s fast, confident, and wrong. With one, you have an agent that actually understands your business.
The Bigger Picture
The context layer is the missing piece between “AI agents that demo well” and “AI agents that work in production.” Every enterprise that’s tried to deploy data agents has discovered this the hard way: the model isn’t the bottleneck, the data understanding is.
Building a context layer is primarily an organizational challenge, not a technical one. The definitions exist, scattered across people’s heads and Slack threads and wiki pages. The work is gathering them, structuring them, and keeping them current.
The technical piece, keeping the context layer fresh via streaming infrastructure, is what makes the organizational investment pay off long-term. A context layer that goes stale is a liability. A context layer that stays current through CDC and schema evolution tracking is a durable competitive advantage for agent accuracy.
Companies that build this infrastructure now will have agents that actually work. Companies that keep waiting for better models will keep getting the wrong answers.
Ready to keep your context layer fresh with real-time data? Streamkap’s CDC pipelines detect schema changes automatically and deliver sub-second data freshness to your agent infrastructure. Start a free trial or explore how Streamkap powers AI agents.