Skip to content
Google Cloud · Gemini Enterprise

Real-time context for Gemini agents: streams, BigQuery and MCP

A Gemini agent reasons over whatever context it is handed. If that context is a nightly extract, the agent is confidently wrong about anything that changed today. This post shows how to build the context layer from Google Cloud services: Datastream and Managed Service for Apache Kafka for change capture, Pub/Sub for events, Dataflow for entity context, BigQuery as analytical memory, Bigtable or AlloyDB for keyed lookups, and MCP tools behind Agent Gateway. Every action the agent takes goes back into the stream as an event.

TL;DR

Agents built on Gemini Enterprise need three kinds of context: what was said in this conversation, what the agent has learned about this user over time, and the current state of the business. Agent Platform Sessions and Memory Bank handle the first two. The third is a data engineering problem. Datastream or Managed Service for Apache Kafka captures changes, Pub/Sub carries events, and Dataflow assembles entity context. That context is served from Bigtable or AlloyDB, BigQuery and Vector Search, through MCP tools behind Agent Gateway. Every agent action goes back to Pub/Sub and BigQuery for audit, replay and shadow testing. Below: the architecture, where each tool should read from, a latency budget, five use cases, a walkthrough, cost, a 90-day plan and failure modes.

Gemini Enterprise on Google Cloud · a five-part series
1. Reference architecture · 2. Enterprise knowledge bases · 3. The agentic harness · 4. Real-time context · 5. Agentic L1 resolution

Why Gemini agents need fresh, assembled context

Part 2 covered knowledge: policies, procedures, contracts and runbooks, the documents an agent cites. Those change weekly or monthly. This post covers state: the balance on an account, the status of a claim, the position of a truck, the risk score on a transaction. State changes by the second, and agents that act on it go wrong in three predictable ways when it is handled badly.

  • The context is stale. A customer-service agent working from last night's BigQuery load tells a caller their payment has not arrived, when it cleared forty minutes ago. The model reasoned correctly. Its input was out of date.
  • The context is scattered. One customer's state is spread across billing, CRM, ticketing and orders. Calling five APIs per turn is slow and fragile, and the join logic ends up in a prompt where nobody can test it.
  • Nothing records what the agent did. A credit issued by an agent needs a queryable record of the action, the context it read and the authority it used.

The vendor-neutral version of this architecture, with CDC, Flink and the action log in depth, is in Kafka as the real-time context layer for AI agents. This post is the Google Cloud-native counterpart: if your agents run on ADK and Agent Runtime and your data already lives in BigQuery, which managed services do you use for each layer, and how do they connect to Agent Gateway and Agent Identity?

The principle is the same on any cloud. Don't ask the agent to go and find the facts on every turn. Keep the facts it needs continuously assembled, fresh and governed, so that each one is a single keyed read.

The Google Cloud real-time context architecture

Five stages run left to right, plus a loop that brings the agent's actions back in. What matters is the separation of concerns: capture never shapes data, processing never serves agents, and agents never reach raw tables.

Real-time context for Gemini agents on Google Cloud
Sources Capture Process Store Expose Agents Databasesbilling · orders Kafka estatesexisting topics Apps & docsevents · files DatastreamCDC from DB logs Managed KafkaKafka on GCP Pub/Subapp & agent events Dataflow join · enrich mask · embed entity context Bigtable / AlloyDBkeyed entity context BigQuerycontinuous queries Vector Searchfresh embeddings Agent Gateway MCP tools Agent Identity policy · audit Gemini agents ADK on Agent Runtime agent-actions — Pub/Sub action log stored in BigQuery · audit · replay · evaluation re-enters Dataplex — catalog, lineage and data quality across every hop, from source table to MCP tool Cloud IAM · VPC Service Controls · CMEK / Cloud KMS · Model Armor · Cloud Logging · Cloud Monitoring
Context flows left to right and agents only see it through Agent Gateway. The dashed loop publishes each action as an event, which feeds the same pipeline and the audit store.

Capture: Datastream, Managed Kafka and Pub/Sub

Datastream reads the change log of MySQL, PostgreSQL, Oracle and SQL Server sources and writes row-level changes directly into BigQuery or to Cloud Storage for Dataflow. Google Cloud Managed Service for Apache Kafka fits when Kafka is already your backbone, you run Debezium, or many non-Google consumers share the streams; you get per-key ordering and replay without operating brokers. Pub/Sub carries application events and the agent's own action events. If you have invested in Kafka, keep it; our Kafka POD builds on Managed Service for Apache Kafka as readily as anywhere. Starting fresh with a few relational sources, Datastream and Pub/Sub have fewer moving parts.

Process: Dataflow builds entity context

Dataflow does the work that would otherwise end up in prompts. It joins the streams describing one entity (customer, claim, shipment, work order) into one continuously updated record, computes the rollups an agent would otherwise request, and tokenizes sensitive fields before anything reaches an agent-readable store. When a free-text field changes, the same pipeline re-embeds it, so semantic search stays consistent with the record.

Store: three stores, three question shapes

Bigtable or AlloyDB holds the current entity context for keyed lookups. Bigtable suits high-volume, simple key access; AlloyDB suits tools that need relational filters, small joins, or vectors and rows in one PostgreSQL-compatible database. BigQuery is the analytical memory: full history, cross-entity aggregates, the action log, and continuous queries, which are SQL statements that run continuously over arriving rows and export results to Pub/Sub or Bigtable. Vector Search in Agent Platform holds embeddings for unstructured context, updated as records change rather than rebuilt nightly.

Expose: MCP tools behind Agent Gateway

Agents reach stores only through MCP tools: typed lookups (get_account_context), bounded searches (find_similar_cases) and a few governed analytical tools on a BigQuery MCP server. All of it passes through Agent Gateway, and each agent presents its own Agent Identity; Google's BigQuery MCP template uses the same pattern, so only that agent's identity can use the tool. The tools are listed in Agent Registry so the next team reuses them instead of rewriting them.

Components mapped to Google Cloud services

LayerGoogle Cloud serviceWhat it does for the agent
Database CDCDatastreamRow-level changes from operational databases, in commit order, into BigQuery or Cloud Storage
Streaming backboneManaged Service for Apache KafkaOrdered, replayable topics where Kafka is already the standard or many consumers share streams
EventsPub/SubApplication events, triggers for event-driven agents, the agent action log; BigQuery subscriptions land them as tables
Context assemblyDataflowJoins, enrichment, masking and embedding into one record per entity
Keyed servingBigtable or AlloyDBMillisecond lookups of current entity context by ID
Analytical memoryBigQuery (incl. continuous queries)History, aggregates, real-time signals, the audit store; BigQuery MCP server for governed queries
Semantic contextVector Search (Agent Platform)Filtered top-k over notes, documents and case summaries, refreshed on change
Tool surfaceMCP servers on Cloud Run or GKE, listed in Agent RegistrySmall, typed tools that read context and write command events
EnforcementAgent Gateway, Agent Identity, Model ArmorWho may call which tool, with what inputs; injection and data-leak screening
GovernanceDataplex, Cloud IAM, VPC Service Controls, CMEKCatalog, lineage, quality rules, perimeters and keys for every context table

Session state, Memory Bank and enterprise context: what lives where

A common mistake is putting business state into agent memory, or the reverse. There are three tiers, each with its own owner and lifetime.

TierWhere it livesHoldsLifetime and owner
Session stateAgent Platform SessionsThe turns of this conversation, tool results, scratch state for the current taskOne session; owned by the agent
Long-term memoryAgent Platform Memory BankWhat the agent learned about this user: preferences, recurring issues, stated constraintsAcross sessions, scoped to a user; owned by the agent, governed by memory profiles
Enterprise contextBigtable / AlloyDB, BigQuery, Vector SearchThe business truth: balances, statuses, positions, scores, documentsAs long as the system of record says; owned by data owners, never written by the agent

Here is the rule we apply. If a fact has a system of record, the agent reads it from the context layer every time and never memorizes it. A memory that says “customer has an overdue invoice” is wrong the moment the invoice is paid. A memory that says “customer prefers email and has asked twice about paperless billing” is exactly what Memory Bank is for. Session state should hold references (the entity ID and context_version), not copies, so a long conversation re-reads current state.

Where should the agent read from?

Every tool in the agent's surface should be assigned to exactly one read path. This is the decision diagram we use when designing the MCP tool surface with a client team.

Choosing the read path for each agent tool
What does this tool call need? Current stateof one entity Meaningin notes or documents Aggregatesacross many entities This user’s historypreferences, past asks React to changeno user in the loop yet Serving storeBigtable · AlloyDBkeyed, ms Vector Searchfiltered top-krefreshed on change BigQuery MCPbounded viewscached results Memory Bankper-user memoriesnever business state Pub/Sub triggeror continuous queryagent starts on event Never on the hot path: raw CDC tables, unbounded SQL, or source-system APIs called on every turn
Each tool gets one read path. If a question does not fit any of the five boxes, the tool is probably too broad and needs to be split.

BigQuery through MCP is where cost and latency surprises come from. We expose authorized views and parameterized queries rather than free-form SQL, cap bytes billed on every query, and cache repeated questions. Triggers invert the flow: a continuous query or Dataflow rule publishes a signal to Pub/Sub, and Eventarc or a push subscription starts an agent with the entity ID in hand.

Latency budget and freshness SLOs

There are two clocks. Turn latency is how long the user waits; freshness is how old the context is when read. The serving layer separates them: the hot path reads a finished record that the pipeline keeps current in the background.

One agent turn: the hot path vs. the background freshness path
Hot path · per turn · illustrative budgets User turnquestion Agent Gatewayidentity · policy Keyed lookupBigtable / AlloyDB Vector Searchoptional Geminireasons · answers Action eventto Pub/Sub a few ms ~10 ms tens of ms most of the turn async, ~10 ms context budget: well under 10% of the turn Background path · continuous Source commit Datastream Dataflow Serving upsert keyed contexttarget: seconds Datastream BigQuery Continuous query Pub/Sub signaltarget: seconds–minutes freshness = time the record is readable − source commit time, measured per context type
The numbers are illustrative targets, not benchmarks. Interactive time should go to reasoning, not fact gathering. Freshness is a separate budget, measured on the background path and returned to the agent as as_of.

Setting the turn budget

Start from what the user will tolerate, usually a few seconds to first token, and work backwards. The model takes most of it, so context retrieval must be a small, predictable slice:

  • One keyed read per entity. Move fan-out into Dataflow.
  • Fetch in parallel. Independent tool calls go out together.
  • Return small payloads. A few hundred agent-shaped tokens with an as_of beat a raw row dump. Token count is latency.
  • Keep unbounded BigQuery off the interactive path. Seconds-long queries suit batch agents, not live callers.

Setting freshness SLOs

Define freshness per context type. In a typical rollout, payments target seconds, case status under a minute, and documents and embeddings minutes. Stamp the source commit time in Dataflow, compare it with the time the record is readable, and alert on that Cloud Monitoring metric. On breach, the MCP tool returns stale: true and the agent says “as of 10:42” or hands off. An agent that knows its data is stale is safe; one that doesn't is a liability.

Writing agent actions back: audit, replay and shadow testing

The other half of the problem is what the agent does with context. The pattern has three parts.

  1. Action tools emit commands; they do not execute them. A tool such as issue_goodwill_credit publishes a command to Pub/Sub with an idempotency key (session, entity, intent). A Cloud Run executor re-checks limits, deduplicates, calls the system of record once and publishes the outcome.
  2. Every tool call becomes an action record. The record holds the agent's identity and version, the tool and its inputs, the context_version and as_of it read, the policy decision from Agent Gateway, and the outcome. It is published to an agent-actions topic and written to a BigQuery table through a BigQuery subscription. That table is your audit store, your evaluation dataset and your incident record.
  3. The outcome re-enters as context. The credit posts, Datastream captures it, and the next turn sees it. Nothing needs to remember that the agent acted; the data shows it.
The action loop: execute once, record everything, replay safely
change returns as fresh context: Datastream → Dataflow → serving store Agent v3production Agent Gatewaypolicy · identity commandsPub/Sub topic ExecutorCloud Run · dedupe System of recordbilling · CRM re-check authority, execute once every call agent-actionswho · tool · inputs · context BigQuery auditvia BigQuery subscription Evaluation & replayrecorded context + decisions Agent v4 · shadowdecisions diffed, never executed Same context version in, compare decisions out, before promotion
Commands are executed once by an executor that enforces limits. Each call is recorded with the context version it read, which makes replay and shadow comparison possible.

Replay works because the action record references the context version the agent read. To reproduce an incident, read that version from BigQuery history and re-run the agent. To evaluate a new version, run it in shadow over recorded turns, write its proposed actions to a table no executor reads, and compare them with what production did.

Five use cases, and the context each one needs

1. Customer-service agent with live account state

Context is one Bigtable record per customer: balance, recent payments, open orders and tickets, plan and entitlements, assembled by Dataflow from Datastream changes and Pub/Sub order events. Memory Bank holds preferences and past asks. Action tools (bounded goodwill credit, open ticket, schedule callback) are command events with authority limits.

2. Fraud and risk signal agent

This agent is triggered, not asked. A BigQuery continuous query watches transactions arriving through Datastream and flags patterns such as velocity across new payees or a device change followed by a large transfer, then exports each signal to Pub/Sub. The agent starts on the signal with the account ID. It reads account context, retrieves similar past cases from Vector Search, and writes an assessment and recommendation to the analyst's queue. It does not block accounts; the recommendation is logged next to the analyst's decision so you can measure agreement.

3. Supply-chain exception agent

Carrier and warehouse events arrive on Pub/Sub or Managed Kafka; Dataflow keeps one record per shipment with promised date, ETA, destination stock and dependent orders. When the ETA slips past the promise, a signal starts the agent. It finds affected customer orders, checks alternate sources in AlloyDB, and drafts options (expedite, re-allocate, notify) that the planner approves in the Gemini Enterprise app.

4. Insurance claims agent with live policy and claim state

This is the domain where we run production agentic systems, and it is where stale context hurts most. The claim record combines coverage from the policy system, reserves, payments to date, open tasks, documents received and the last contact. A claims-handler agent answers “what is outstanding on this claim?” from that record and cites as_of. It retrieves similar claims and relevant policy wording through Vector Search and the knowledge base. It can send a status letter but cannot change a reserve; that goes to a human with the agent's reasoning attached. The Kafka post walks through this case at stream level.

5. Field-service agent

Technicians ask about the asset in front of them: work orders, open alarms, parts on the van, warranty. Telemetry flows through Pub/Sub and Dataflow into a rolling alarm summary per asset in Bigtable; work orders arrive via Datastream. It can order a part or escalate. On a weak signal, one small pre-assembled record beats a series of API calls.

Walkthrough: one customer-service turn, end to end

Here is the customer-service agent from use case 1, built with ADK, running on Agent Runtime and registered in the Gemini Enterprise app for the support team. The timings are illustrative.

1

A payment posts (10:41:05)

The billing database commits a payment row. Datastream captures the change. The Dataflow job joins it with the customer's open invoices, recomputes the open balance and writes version 318 of customer/C-88213 to Bigtable with as_of = 10:41:07. The same change is replicated to BigQuery for history.

2

A support rep asks (10:44)

“Customer C-88213 says they paid yesterday and got a late-fee notice this morning. Can you check and fix it?” Agent Platform Sessions holds the turn. Memory Bank returns one relevant memory: this customer disputed a late fee in March and prefers email.

3

The agent reads context through the gateway

The agent calls get_account_context and get_recent_notices in parallel. Agent Gateway checks the agent's identity and the rep's entitlement to this customer's region. The tools return: balance zero, payment posted 10:41, late-fee notice generated by the 06:00 batch, before the payment existed.

4

The agent reasons and proposes

Gemini concludes the customer paid within their plan's grace period and proposes reversing the fee, which is under the tool's limit; it states the amount and asks the rep to confirm.

5

The action is a command, not an API call

On confirmation, reverse_fee publishes a command with idempotency key sess-41c:C-88213:reverse_fee:INV-5520. The Cloud Run executor checks the limit again, calls billing once, and publishes the outcome. If the rep clicks twice, the second command is discarded as a duplicate.

6

Everything is recorded and the context catches up

The agent-actions record (identity, version, reads at context version 318, gateway decision, command, outcome) lands in BigQuery. Seconds later the reversal flows back and the context moves to version 319; if the rep asks “is it done?”, the agent reads it rather than remembering it.

Security, identity and governance

A context layer concentrates sensitive data, so the controls must be concentrated there too. Part 3 covers the harness; these are specific to context:

  • One Agent Identity per agent, scoped to its tools. The customer-service agent may call get_account_context and reverse_fee. It has no grant on the fraud agent's tools or on BigQuery datasets outside its views. Agent Gateway enforces this on every call, and Cloud Logging records it.
  • User entitlements travel with the call. Where an agent acts on a user's behalf, the tool checks that user's entitlement (region, line of business, customer segment) as well as the agent's. Agent identity sets the ceiling. The user's rights narrow it.
  • Masking happens in Dataflow, before storage. Card numbers, bank details and IDs are tokenized before writing; BigQuery policy tags protect history, and agents read authorized views only.
  • Perimeters and keys. The context stores, Pub/Sub topics and BigQuery datasets sit inside a VPC Service Controls perimeter with CMEK. Model Armor screens tool inputs and outputs for prompt injection and sensitive-data leakage, which matters when a context record contains free text such as a customer's own notes.
  • Dataplex for lineage and ownership. Each context table and topic is cataloged in Dataplex with an owner, data-quality rules and lineage from source table through Dataflow to the serving store. When an auditor asks where the balance figure the agent quoted came from, lineage answers at the dataset level and the action log answers at the record level.

Evaluation and operations

Run the context layer as a data product with SLOs. We track four signals per context type in Cloud Monitoring:

  • Freshness: p50 and p99 of readable-time minus commit-time, alerted on breach.
  • Read latency: p99 tool latency at the gateway, split by store.
  • Completeness: share of context records with every required field populated. A silently null field is the usual symptom of an upstream schema change.
  • Stale-read rate: share of agent turns that received stale: true, which ties data health to user experience.

Agent Platform tracing links a turn to its tool calls; the action log links it to the context version used. Build the evaluation set from recorded turns, test new versions with multi-turn autoraters and Agent Simulation, and promote only after shadow decisions match or beat production. InsightLense, our tracing and cost tool, sits alongside the platform's own observability where teams want cost per turn broken down by tool.

What it costs, and where the money goes

Prices depend on volume, region and commitments, but the cost structure is predictable.

ComponentCost driverHow to keep it in check
DatastreamVolume of change data processedCapture only the tables and columns agents and analytics need
DataflowAlways-on streaming workersConsolidate entity jobs; right-size and use autoscaling; avoid one job per tool
Pub/Sub / Managed KafkaThroughput and retentionRetention sized to replay needs; long history belongs in BigQuery
Bigtable / AlloyDBProvisioned capacityStore current state only; history lives in BigQuery
BigQueryStorage, query bytes, slots for continuous queriesContinuous queries run on a slot reservation, so size it deliberately; cap bytes billed on MCP queries; cache
Vector SearchServing capacity and embedding callsRe-embed only when content changes, not on every row update
Gemini tokensContext size times turnsAgent-shaped context records; avoid dumping raw rows into prompts

The streaming layer is a shared fixed cost: later agents in a domain reuse the same context and add little. And a well-shaped record usually reduces model spend, because 400 tokens of assembled context is cheaper than five raw API responses the model must join.

A 90-day rollout plan

30

Days 1–30: one entity, one agent, read-only

Pick the entity with the clearest value. Stand up Datastream for its sources, a Dataflow job that builds the masked record with as_of, and a Bigtable or AlloyDB table. Expose two read tools through an MCP server behind Agent Gateway with a dedicated Agent Identity. Instrument freshness from day one. Ship the agent to a pilot group in the Gemini Enterprise app with no action tools.

60

Days 31–60: actions, audit and analytics

Add the command pattern: one or two bounded action tools, a Cloud Run executor with idempotency and limits, the agent-actions topic and its BigQuery table. Add Vector Search for the entity's unstructured fields and one governed BigQuery MCP tool. Register everything in Agent Registry and catalog the datasets in Dataplex. Build the evaluation set from recorded pilot turns.

90

Days 61–90: triggers, shadow and the second agent

Add a continuous query or Dataflow rule that starts an agent on an event. Run the next agent version in shadow against recorded traffic before promotion. Onboard a second agent on the same entity context and measure how much of the build it reused. That number makes the case for the platform.

Failure modes to plan for

1. Agents querying raw replicated tables. Replicas carry every column, including the ones you masked elsewhere. Agents read authorized views or the serving store, through MCP.

2. Free-form SQL as a tool. It works in a demo, then scans terabytes mid-call. Use parameterized, bounded queries with byte caps.

3. Business state in Memory Bank. Memories of balances and statuses go stale silently and contradict the context layer. Memories are for the user and the relationship. State comes from the system of record.

4. Freshness nobody measures. A job lags after a schema change and the agent keeps answering confidently. Measure, alert, pass stale.

5. Action tools calling APIs directly. Without a command event there is no idempotency, no second authority check and no consistent record. Retries become duplicate refunds.

6. A shared service account for all agents. You lose the ability to say which agent did what, and a compromise of one becomes a compromise of all. One Agent Identity per agent, scoped at the gateway.

Readiness checklist

AreaReady when
CaptureEach system of record the agent depends on is captured by Datastream, Kafka or events, not a nightly extract
Entity modelAgent-facing entities are defined with context_version, as_of and an owner
MaskingSensitive fields are tokenized in Dataflow before any agent-reachable store
ServingKeyed reads meet the turn budget at peak load in Bigtable or AlloyDB
FreshnessAn SLO per context type is measured end to end, alerted, and surfaced to the agent
Memory tiersSessions, Memory Bank and enterprise context each have documented contents and no overlap
Tool surfaceMCP tools are typed, bounded, registered in Agent Registry; no free-form SQL
IdentityOne Agent Identity per agent, gateway policies per tool, user entitlements checked
ActionsCommand events with idempotency keys; executor re-checks authority
AuditAction log in BigQuery with context versions, retained to your compliance window
GovernanceContext datasets cataloged in Dataplex with lineage and quality rules; VPC-SC and CMEK in place
EvaluationRecorded-turn test set, shadow mode and replay available before promotion

The bottom line

Gemini Enterprise gives you a strong agent harness: Agent Runtime, Sessions, Memory Bank, Agent Identity, Agent Gateway and Agent Registry. It does not know your account balances. That context comes from your data platform, and on Google Cloud the services are already in place: Datastream or Managed Kafka to capture change, Pub/Sub to carry events, Dataflow to assemble entities, Bigtable or AlloyDB to serve them, BigQuery to remember and to signal, and Vector Search for meaning. Put a small set of MCP tools in front of them behind Agent Gateway, write every agent action back as an event, and you have agents that answer from current facts and leave a record anyone can audit.

In seven years of building data and AI platforms on Google Cloud, including a global retailer's enterprise GenAI platform and Vertex AI platforms for a large technology company and a large utility, the recurring lesson is that the first agent pays for the context layer and every later agent benefits from it. Part 5 puts these pieces to work on agentic L1 resolution.

Give your Gemini agents current facts

We build the context layer on Google Cloud, from Datastream and Dataflow to Bigtable, BigQuery and MCP tools behind Agent Gateway, starting with one entity and one agent inside your project.

Data & AI on Google Cloud
Back to blog