> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini Internal Tools

> Use Google Gemini's built-in internal tools (Google Search, URL Context, Code Execution) with PraisonAI agents.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

agent = Agent(
    name="Gemini Researcher",
    llm="gemini/gemini-2.0-flash",
    instructions="Use Gemini built-in search when facts are needed.",
    tools=[{"googleSearch": {}}],
)
agent.start("What changed in Python 3.13 release highlights?")
```

The user asks a factual question; the agent invokes Gemini Google Search and summarises grounded results.

<Note>
  **Prerequisites**

  * Python 3.10 or higher
  * PraisonAI Agents package installed
  * Gemini API key
  * Gemini 2.0+ model (e.g., `gemini/gemini-2.0-flash`)
</Note>

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    U[Input] --> A[Agent]
    A --> T[Tool]
    T --> O[Output]

    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff

    class A agent
    class U,O tool
    class T tool
```

## Overview

Google Gemini provides three powerful internal tools that are natively supported by the model without requiring external implementations. These tools can be used directly through PraisonAI's tool system.

## Available Internal Tools

<CardGroup cols={2}>
  <Card title="Google Search" icon="magnifying-glass">
    Real-time web search with automatic result grounding

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    tools=[{"googleSearch": {}}]
    ```
  </Card>

  <Card title="URL Context" icon="link">
    Fetch and analyze content from specific URLs

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    tools=[{"urlContext": {}}]
    ```
  </Card>

  <Card title="Code Execution" icon="code">
    Execute Python code snippets within the conversation

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    tools=[{"codeExecution": {}}]
    ```
  </Card>
</CardGroup>

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Install PraisonAI with LLM support:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    pip install "praisonaiagents[llm]"
    ```
  </Step>

  <Step title="With Configuration">
    Set your Gemini API key:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    export GEMINI_API_KEY="your-api-key-here"
    ```
  </Step>

  <Step title="Create Agent with Internal Tools">
    Use internal tools in your agent:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent

    # Agent with Google Search
    search_agent = Agent(
        instructions="Research assistant with web search",
        llm="gemini/gemini-2.0-flash",
        tools=[{"googleSearch": {}}]
    )

    response = search_agent.start("What's the latest news about AI?")
    ```
  </Step>
</Steps>

## Individual Tool Examples

### Google Search Tool

Use Google Search for real-time information retrieval:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

search_agent = Agent(
    instructions="You are a research assistant with web search capabilities",
    llm="gemini/gemini-2.0-flash",
    tools=[{"googleSearch": {}}]
)

response = search_agent.start("What are the latest developments in quantum computing?")
print(response)
```

### URL Context Tool

Analyze content from specific web pages:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

url_agent = Agent(
    instructions="You are a content analyzer that can read and summarize web pages",
    llm="gemini/gemini-2.0-flash",
    tools=[{"urlContext": {}}]
)

response = url_agent.start("Summarize this article: https://example.com/article")
print(response)
```

### Code Execution Tool

Execute Python code for calculations and data analysis:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

code_agent = Agent(
    instructions="You are a data analyst that can execute Python code",
    llm="gemini/gemini-2.0-flash",
    tools=[{"codeExecution": {}}]
)

response = code_agent.start("Calculate the compound interest for $10,000 at 5% annual rate for 10 years")
print(response)
```

## Combined Tools Example

Use multiple internal tools together for complex tasks:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

research_agent = Agent(
    instructions="""You are an advanced research assistant that can:
    1. Search the web for information
    2. Analyze content from specific URLs
    3. Execute Python code for data analysis""",
    llm="gemini/gemini-2.0-flash",
    tools=[
        {"googleSearch": {}},
        {"urlContext": {}},
        {"codeExecution": {}}
    ]
)

# Complex research task
response = research_agent.start("""
Research the current stock price of Apple (AAPL), 
find recent news about the company, 
and calculate its P/E ratio if the EPS is $6.15
""")
print(response)
```

## Multi-Agent System with Internal Tools

Create a multi-agent system where different agents use different internal tools:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, Task, AgentTeam

# Research agent with Google Search
researcher = Agent(
    name="Researcher",
    role="Web Research Specialist",
    goal="Find accurate and up-to-date information",
    instructions="Search for comprehensive information on topics",
    llm="gemini/gemini-2.0-flash",
    tools=[{"googleSearch": {}}]
)

# Analyst agent with Code Execution
analyst = Agent(
    name="Analyst",
    role="Data Analyst",
    goal="Analyze data and perform calculations",
    instructions="Perform data analysis and calculations",
    llm="gemini/gemini-2.0-flash",
    tools=[{"codeExecution": {}}]
)

# Content agent with URL Context
content_analyzer = Agent(
    name="ContentAnalyzer",
    role="Content Analysis Specialist",
    goal="Extract and summarize content from URLs",
    instructions="Analyze and summarize web content",
    llm="gemini/gemini-2.0-flash",
    tools=[{"urlContext": {}}]
)

# Define tasks
research_task = Task(
    description="Search for information about renewable energy trends in 2024",
    expected_output="List of key trends with sources",
    agent=researcher
)

analysis_task = Task(
    description="Calculate the growth rate of solar energy adoption based on the research",
    expected_output="Growth rate calculation with visualization",
    agent=analyst
)

content_task = Task(
    description="Analyze this article: https://example.com/renewable-energy-report",
    expected_output="Summary of key points from the article",
    agent=content_analyzer
)

# Create and run the multi-agent system
agents = AgentTeam(
    agents=[researcher, analyst, content_analyzer],
    tasks=[research_task, analysis_task, content_task],
    process="sequential"
)

agents.start()
```

## Mixing Internal and External Tools

Combine Gemini's internal tools with custom external tools:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

def custom_calculator(expression: str) -> str:
    """Custom calculator function"""
    import ast
    import operator
    
    ops = {
        ast.Add: operator.add,
        ast.Sub: operator.sub,
        ast.Mult: operator.mul,
        ast.Div: operator.truediv,
        ast.Pow: operator.pow,
        ast.USub: operator.neg,
    }
    
    def eval_expr(node):
        if isinstance(node, ast.Constant):
            return node.value
        elif isinstance(node, ast.BinOp):
            return ops[type(node.op)](eval_expr(node.left), eval_expr(node.right))
        elif isinstance(node, ast.UnaryOp):
            return ops[type(node.op)](eval_expr(node.operand))
        else:
            raise TypeError(f"Unsupported operation: {type(node)}")
    
    try:
        return str(eval_expr(ast.parse(expression, mode='eval').body))
    except Exception as e:
        return f"Error: {e}"

# Agent with both internal and external tools
hybrid_agent = Agent(
    instructions="You are a versatile assistant with multiple capabilities",
    llm="gemini/gemini-2.0-flash",
    tools=[
        {"googleSearch": {}},      # Internal tool
        {"codeExecution": {}},     # Internal tool
        custom_calculator          # External tool
    ]
)

response = hybrid_agent.start("Search for the current Bitcoin price and calculate 15% of $10,000")
print(response)
```

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Feature as Gemini Internal Tools

    User->>Agent: Request
    Agent->>Feature: Process request
    Feature-->>Agent: Result
    Agent-->>User: Response
```

### Tool Definition Syntax

Gemini internal tools use a special dictionary syntax:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Internal tool format
tools=[{"toolName": {}}]

# Multiple internal tools
tools=[
    {"googleSearch": {}},
    {"urlContext": {}},
    {"codeExecution": {}}
]
```

### Integration Flow

1. **Tool Definition**: Define tools using the special internal tool syntax
2. **Pass-Through**: PraisonAI passes these tools directly to LiteLLM
3. **Execution**: LiteLLM sends them to Gemini as internal tool configurations
4. **Results**: Gemini executes the tools natively and returns integrated responses

## Benefits of Internal Tools

<AccordionGroup>
  <Accordion title="Native Integration">
    * No external API calls required
    * Seamless integration with Gemini's capabilities
    * Optimized for performance
  </Accordion>

  <Accordion title="Automatic Grounding">
    * Search results are automatically integrated into responses
    * Context-aware information retrieval
    * Source attribution built-in
  </Accordion>

  <Accordion title="Security">
    * Code execution is sandboxed within Gemini's environment
    * No local code execution risks
    * Controlled access to resources
  </Accordion>

  <Accordion title="No Rate Limits">
    * No separate API rate limits
    * Included in Gemini API quota
    * Simplified billing
  </Accordion>
</AccordionGroup>

## Best Practices

<AccordionGroup>
  <Accordion title="Model Selection">
    Use Gemini 2.0+ models for internal tools:

    * `gemini/gemini-2.0-flash` (recommended)
    * `gemini/gemini-2.0-flash-thinking-exp`
    * Other Gemini 2.0+ models
  </Accordion>

  <Accordion title="Error Handling">
    Always handle potential errors:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    try:
        response = agent.start("Your query")
    except Exception as e:
        print(f"Error: {e}")
    ```
  </Accordion>

  <Accordion title="Debugging">
    Enable reflection for better debugging:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    agent = Agent(
        # ... other config
        reflection=True
    )
    ```
  </Accordion>
</AccordionGroup>

## Troubleshooting

<Tabs>
  <Tab title="API Key Issues">
    **Problem**: API key not recognized

    **Solution**: Ensure the environment variable is set correctly:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    echo $GEMINI_API_KEY  # Should show your key
    ```
  </Tab>

  <Tab title="Model Support">
    **Problem**: Tool not available error

    **Solution**: Verify you're using a Gemini 2.0+ model:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    llm="gemini/gemini-2.0-flash"  # Correct
    # NOT: llm="gemini/gemini-1.5-pro"  # May not support all tools
    ```
  </Tab>

  <Tab title="Regional Restrictions">
    **Problem**: Some tools may have regional restrictions

    **Solution**: Check Gemini API documentation for availability in your region
  </Tab>
</Tabs>

## References

* [Gemini API: Google Search Grounding](https://ai.google.dev/gemini-api/docs/google-search)
* [Gemini API: URL Context](https://ai.google.dev/gemini-api/docs/url-context)
* [Gemini API: Code Execution](https://ai.google.dev/gemini-api/docs/code-execution)
* [LiteLLM Gemini Provider Documentation](https://docs.litellm.ai/docs/providers/gemini)

## Related Documentation

<CardGroup cols={2}>
  <Card title="Google Gemini Models" icon="google" href="/docs/models/google">
    Learn about Gemini model configuration
  </Card>

  <Card title="External Search Tools" icon="magnifying-glass" href="/docs/tools/external/google-search">
    Explore external search tool alternatives
  </Card>

  <Card title="Custom Tools" icon="wrench" href="/docs/tools/custom">
    Create your own custom tools
  </Card>
</CardGroup>
