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

# Google

> Trace Google ADK agents and Gemini API calls

Google provides two SDKs relevant for tracing — choose based on what you're building:

| SDK               | Use case                                            | Integration          |
| ----------------- | --------------------------------------------------- | -------------------- |
| **Google ADK**    | Full agent framework (multi-agent, tools, sessions) | `AgentXADKPlugin`    |
| **Google Gen AI** | Raw Gemini API calls (`generate_content`)           | `patch_genai_client` |

***

## Google ADK

Install:

```bash theme={null}
pip install "agentx-python[google-adk]" google-adk
```

Register `AgentXADKPlugin` in the `plugins` list when constructing the ADK `Runner`. Every subsequent `runner.run()` call is traced automatically.

```python theme={null}
import os
from agentx import AgentX
from agentx.integrations.google_adk import AgentXADKPlugin
from google.adk.agents import Agent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.adk.tools import FunctionTool
from google.genai import types

os.environ["GEMINI_API_KEY"] = "xxxxxxxxxxxxxxxxxxxx"

client = AgentX(
    api_key="agx-xxxxxxxxxxxxxxxx",
    workspace_id="xxxxxxxxxxxx",  # optional
)

def get_policy(topic: str) -> dict:
    """Return the company policy for a given topic."""
    db = {
        "cancel": "Go to Account → Subscription → Cancel.",
        "trial": "14-day free trial, no credit card required.",
        "refund": "Full refund within 30 days.",
    }
    return {"result": db.get(topic.lower(), "No policy found.")}


agent = Agent(
    name="support_agent",
    instruction="You are a helpful support agent. Use get_policy to look up policies.",
    tools=[FunctionTool(func=get_policy)],
)

runner = Runner(
    agent=agent,
    app_name="support-app",
    session_service=InMemorySessionService(),
    plugins=[
        AgentXADKPlugin(
            tracer=client.tracer,
            name="support-agent",
            metadata={"env": "production"},
        )
    ],
)

session = runner.session_service.create_session_sync(
    app_name="support-app", user_id="user-1"
)
for event in runner.run(
    user_id="user-1",
    session_id=session.id,
    new_message=types.Content(
        role="user", parts=[types.Part(text="How do I cancel my subscription?")]
    ),
):
    if event.is_final_response():
        print(event.content.parts[0].text)

client.tracer.flush(timeout=10)
```

### What gets traced

| Field       | Source                                                              |
| ----------- | ------------------------------------------------------------------- |
| `input`     | User message text from `on_user_message_callback`                   |
| `output`    | Last model reply text from `after_model_callback`                   |
| `latencyMs` | Wall-clock time from `before_run_callback` to `after_run_callback`  |
| `model`     | `llm_request.model` from `before_model_callback`                    |
| `toolCalls` | Each `after_tool_callback` — `name`, `input`, `output`, `latencyMs` |

### `AgentXADKPlugin` reference

```python theme={null}
AgentXADKPlugin(
    tracer: Tracer,
    name: str = "google-adk-agent",
    metadata: dict | None = None,
    session_id: str | None = None,
)
```

| Parameter    | Description                                       |
| ------------ | ------------------------------------------------- |
| `tracer`     | `client.tracer` from your `AgentX` instance       |
| `name`       | Fallback label when the agent has no name set     |
| `metadata`   | Static key-value metadata attached to every trace |
| `session_id` | Links traces from the same conversation thread    |

***

## Google Gen AI (raw Gemini API)

Install:

```bash theme={null}
pip install "agentx-python[google-genai]" google-genai
```

Call `patch_genai_client()` once after creating your `genai.Client`. All subsequent `client.models.generate_content()` calls are traced automatically.

```python theme={null}
import os
from agentx import AgentX
from agentx.integrations.google_genai import patch_genai_client
from google import genai
from google.genai import types

client = AgentX(
    api_key="agx-xxxxxxxxxxxxxxxx",
    workspace_id="xxxxxxxxxxxx",  # optional
)

genai_client = genai.Client(api_key="GEMINI_API_KEY")

patch_genai_client(
    genai_client,
    tracer=client.tracer,
    name="gemini-support-agent",
    metadata={"env": "production"},
)

# Regular call
response = genai_client.models.generate_content(
    model="gemini-3.5-flash",
    contents="How do I cancel my subscription?",
)
print(response.text)

# or Streaming call
for chunk in genai_client.models.generate_content_stream(
    model="gemini-3.5-flash",
    contents="What is your refund policy?",
):
    print(chunk.text, end="", flush=True)

client.tracer.flush(timeout=10)
```

### `patch_genai_client()` reference

```python theme={null}
patch_genai_client(
    client: genai.Client,
    tracer: Tracer,
    name: str = "gemini-agent",
    metadata: dict | None = None,
    session_id: str | None = None,
) -> None
```

| Parameter    | Description                                       |
| ------------ | ------------------------------------------------- |
| `client`     | The `genai.Client()` instance to patch            |
| `tracer`     | `client.tracer` from your `AgentX` instance       |
| `name`       | Label shown in the AgentX UI for every trace      |
| `metadata`   | Static key-value metadata attached to every trace |
| `session_id` | Links traces from the same conversation thread    |

Calling `patch_genai_client()` on an already-patched client is a no-op.

<Note>
  Call `tracer.flush()` before your process exits in scripts or one-shot jobs.
  In long-running servers it is not required — traces drain automatically in the
  background.
</Note>
