Skip to content
Learn Agentic AI
Learn Agentic AI10 min read0 views

Gemini Grounding with Google Search: Building Agents with Real-Time Information

Learn how to use Gemini's built-in Google Search grounding to build agents that access real-time information, handle citations properly, and deliver accurate, up-to-date responses.

The Problem with Static Knowledge

Every language model has a knowledge cutoff date. Events, prices, regulations, and facts change constantly. When your agent answers "What is the current price of Bitcoin?" or "What are the latest changes to GDPR compliance?" using only its training data, the answer is likely outdated.

Gemini solves this with native Google Search grounding. Instead of building a separate search pipeline, you enable grounding and the model automatically searches Google when it needs current information, then cites its sources.

Enabling Google Search Grounding

Grounding is enabled by passing a tool configuration when creating the model:

flowchart TD
    START["Gemini Grounding with Google Search: Building Age…"] --> A
    A["The Problem with Static Knowledge"]
    A --> B
    B["Enabling Google Search Grounding"]
    B --> C
    C["Accessing Grounding Metadata"]
    C --> D
    D["Building a Research Agent with Citations"]
    D --> E
    E["Dynamic Grounding Threshold"]
    E --> F
    F["Combining Search Grounding with Functio…"]
    F --> G
    G["FAQ"]
    G --> DONE["Key Takeaways"]
    style START fill:#4f46e5,stroke:#4338ca,color:#fff
    style DONE fill:#059669,stroke:#047857,color:#fff
import google.generativeai as genai
import os

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

model = genai.GenerativeModel(
    "gemini-2.0-flash",
    tools="google_search_retrieval",
)

response = model.generate_content(
    "What were the major AI announcements this week?"
)

print(response.text)

When grounding is active, Gemini decides autonomously whether a query needs search results. Factual questions about current events trigger a search, while questions the model can answer from training data may not.

Accessing Grounding Metadata

The response includes detailed metadata about which searches were performed and which sources were used:

response = model.generate_content(
    "What is the current stock price of NVIDIA?"
)

# The generated answer
print(response.text)

# Access grounding metadata
grounding = response.candidates[0].grounding_metadata

# Search queries that were executed
if grounding.search_entry_point:
    print(f"Search rendered: {grounding.search_entry_point.rendered_content}")

# Individual grounding chunks with source URLs
for chunk in grounding.grounding_chunks:
    if chunk.web:
        print(f"Source: {chunk.web.title} - {chunk.web.uri}")

# Grounding supports — which parts of the response are grounded
for support in grounding.grounding_supports:
    print(f"Text: {support.segment.text}")
    for idx in support.grounding_chunk_indices:
        source = grounding.grounding_chunks[idx]
        if source.web:
            print(f"  Backed by: {source.web.uri}")

This metadata lets you build agents that show their sources, a critical requirement for trust and compliance in enterprise applications.

See AI Voice Agents Handle Real Calls

Book a free demo or calculate how much you can save with AI voice automation.

Building a Research Agent with Citations

Here is a complete research agent that formats responses with proper source attribution:

import google.generativeai as genai
import os

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

class ResearchAgent:
    def __init__(self):
        self.model = genai.GenerativeModel(
            "gemini-2.0-flash",
            tools="google_search_retrieval",
            system_instruction=(
                "You are a research assistant. Provide thorough, "
                "factual answers based on current information. "
                "Always note when information might change rapidly."
            ),
        )

    def research(self, query: str) -> dict:
        response = self.model.generate_content(query)

        sources = []
        grounding = response.candidates[0].grounding_metadata
        if grounding and grounding.grounding_chunks:
            for chunk in grounding.grounding_chunks:
                if chunk.web:
                    sources.append({
                        "title": chunk.web.title,
                        "url": chunk.web.uri,
                    })

        return {
            "answer": response.text,
            "sources": sources,
            "grounded": len(sources) > 0,
        }

agent = ResearchAgent()
result = agent.research("What are the latest developments in quantum computing?")

print(result["answer"])
print(f"\nBacked by {len(result['sources'])} sources:")
for src in result["sources"]:
    print(f"  - {src['title']}: {src['url']}")

Dynamic Grounding Threshold

You can control how aggressively Gemini uses search with the dynamic retrieval configuration:

from google.generativeai.types import DynamicRetrievalConfig

model = genai.GenerativeModel(
    "gemini-2.0-flash",
    tools=genai.Tool(
        google_search_retrieval=genai.GoogleSearchRetrieval(
            dynamic_retrieval_config=DynamicRetrievalConfig(
                mode="MODE_DYNAMIC",
                dynamic_threshold=0.3,  # Lower = more search, higher = less
            )
        )
    ),
)

A threshold of 0.3 means the model searches more often, even for queries it could partially answer from training data. A threshold of 0.8 means it only searches when it has very low confidence. For agents handling current events or financial data, a lower threshold is safer.

Combining Search Grounding with Function Calling

Grounding and custom tools can work together. The model chooses between searching the web and calling your functions based on the query:

def get_internal_sales_data(quarter: str, region: str) -> dict:
    """Fetch internal sales data from our database.

    Args:
        quarter: The fiscal quarter, e.g. 'Q1 2026'.
        region: Sales region, e.g. 'North America'.
    """
    return {"revenue": 2_500_000, "deals_closed": 47, "growth": 0.12}

model = genai.GenerativeModel(
    "gemini-2.0-flash",
    tools=[
        "google_search_retrieval",
        get_internal_sales_data,
    ],
)

# This query uses internal tools
response = model.generate_content("What were our Q1 2026 North America sales?")

# This query uses Google Search
response = model.generate_content("What is the current market size of the CRM industry?")

FAQ

Does Google Search grounding cost extra?

Yes. Grounded requests incur additional costs beyond the standard token-based pricing. Each grounded request is billed at a per-request rate that varies by model. Check the current Gemini API pricing page for exact figures.

Can I use grounding with the free tier?

Google Search grounding is available on the free tier with rate limits. The free tier typically allows a limited number of grounded requests per day, which is sufficient for development and testing.

How fresh is the search data?

Gemini uses Google's live search index, so the data is as current as Google Search itself — typically minutes to hours old for major news and events. This is significantly more current than any model's training data cutoff.


#GoogleGemini #GoogleSearch #Grounding #RealTimeAI #Python #AgenticAI #LearnAI #AIEngineering

Share
C

Written by

CallSphere Team

Expert insights on AI voice agents and customer communication automation.

Try CallSphere AI Voice Agents

See how AI voice agents work for your industry. Live demo available -- no signup required.