Intro to Building and Deploying an Agent with Agent Engine in Vertex AI

Source notebook

Repo path: gemini/agent-engine/intro_agent_engine.ipynb · Open on GitHub · intermediate

Builds, tests, deploys, streams, customizes, and deletes a LangChain Gemini agent on Vertex AI Agent Engine.

Summary

This notebook teaches how to build an agent from a Gemini model, a Python tool, and LangChain reasoning using the Vertex AI SDK for Python. It demonstrates local testing with query and stream_query, deployment to Vertex AI Agent Engine with runtime requirements, remote querying, resource-name reuse, REST access options, customization of model and agent settings, and cleanup.

Key code patterns

Initialize Vertex AI

PROJECT_ID = "[your-project-id]"
LOCATION = "us-central1"
STAGING_BUCKET = "gs://[your-staging-bucket]"
 
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION, staging_bucket=STAGING_BUCKET)

Agent Engine deployment requires project, region, and staging bucket configuration.

Define Tool Function

def get_exchange_rate(currency_from="USD", currency_to="EUR", currency_date="latest"):
    import requests
    response = requests.get(
        f"https://api.frankfurter.app/{currency_date}",
        params={"from": currency_from, "to": currency_to},
    )
    return response.json()

A normal Python function is exposed as an agent tool for real-time exchange-rate lookup.

Create LangChain Agent

from vertexai.preview.reasoning_engines import LangchainAgent
 
agent = LangchainAgent(
    model="gemini-3.5-flash",
    tools=[get_exchange_rate],
    agent_executor_kwargs={"return_intermediate_steps": True},
)

LangchainAgent combines the Gemini model, tool, and reasoning layer.

Stream Local Agent Steps

for chunk in agent.stream_query(input="What's the exchange rate from US dollars to Swedish currency today?"):
    for key, label in message_types.items():
        if key in chunk:
            print(label)
            print(chunk[key])

Streaming exposes actions, messages, and output while the agent runs.

Deploy Remote Agent

remote_agent = agent_engines.create(
    agent,
    requirements=[
        "google-cloud-aiplatform[agent_engines,langchain]",
        "cloudpickle==3.0.0",
        "pydantic>=2.10",
        "requests",
    ],
)

Agent Engine packages the local agent with explicit runtime dependencies.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI, Agent Engine, Vertex AI REST API
  • SDKs / libraries: google-cloud-aiplatform, vertexai, LangChain, langchain_google_vertexai, requests, cloudpickle, pydantic

When to use this

Use this pattern when you need to deploy a Gemini function-calling agent with Python tools and LangChain orchestration on Vertex AI.

Gotchas & caveats

  • Install google-cloud-aiplatform with agent_engines and langchain extras before running the notebook.
  • Restart the Jupyter runtime after package installation.
  • Authenticate explicitly when running in Google Colab.
  • Enable the Vertex AI API before initializing the SDK.
  • Provide an existing Google Cloud project, us-central1 location, and Cloud Storage staging bucket.
  • Vertex AI is billable, and deployed Agent Engine instances should be deleted to avoid unexpected charges.
  • Agent Engine works with Gemini model versions that support Function Calling and LangChain agents.
  • Remote deployment requires listing runtime package requirements.

Best practices

  • Test the Python tool directly before wiring it into the agent.
  • Test the agent locally with query before deployment.
  • Use stream_query to observe actions, messages, and output for debugging or real-time updates.
  • Re-define the agent before deployment to avoid stateful information from local testing.
  • Record remote_agent.resource_name so the deployed agent can be reused from another Python environment.
  • Delete the deployed Agent Engine instance after finishing.