Software Studio
Back to Articles

Building AI Agents with LangChain and Vertex AI

AILangChainVertex AIPythonGCP
Cover illustration for Building AI Agents with LangChain and Vertex AI

The AI agent landscape is evolving rapidly, but the fundamentals are stabilizing. After building several production agents, LangChain paired with Vertex AI has become my go-to stack for anything beyond simple prompt-and-response.

This is what I've learned about making agents that hold up outside a demo.

What an AI agent actually is

An AI agent is more than a chatbot. It's a system that can reason about a task, decide which tools to use, execute actions, and iterate until the task is complete.

In this stack the two halves have clear jobs. LangChain provides the orchestration framework: the loop that passes the model's decisions to tools and feeds the results back. Vertex AI provides the foundation models and the infrastructure they run on.

Choosing a model

The first decision is model selection. For most agent workloads, Gemini Pro through Vertex AI offers the best balance of capability, speed and cost. For tasks requiring stronger reasoning, the top Gemini tier is worth the premium.

The advantage of Vertex AI over direct API access is enterprise features:

  • VPC Service Controls
  • Audit logging
  • Data residency guarantees

If you're building for a company with compliance requirements, those three often decide the platform before any benchmark does.

Tool design is where agents succeed or fail

Each tool should do one thing well, have a clear description that the LLM can understand, and return structured output.

I define tools as Python functions with detailed docstrings. The docstring becomes the tool description the model uses to decide when to invoke it.

from langchain_core.tools import tool


@tool
def get_order_status(order_id: str) -> dict:
    """Look up the current status of a customer order.

    Use this when the user asks where an order is or when it will arrive.
    Returns the status, the carrier and the estimated delivery date.
    """
    return orders.lookup(order_id)

Write the docstring for the model, not for a colleague. Say what the tool does, when to use it, and what comes back. A vague description produces an agent that calls the wrong tool or never calls the right one.

Memory: the underappreciated part

Memory is the most underappreciated aspect of agent design. There are two kinds.

  • Short-term memory is the conversation buffer. It is straightforward.
  • Long-term memory covers user preferences, past interactions and learned facts. It requires a vector store.

I use PostgreSQL with pgvector through Cloud SQL, which keeps the infrastructure simple and avoids adding another managed service. Most applications already have a PostgreSQL database. Storing embeddings next to the rest of your data means one backup strategy, one set of credentials, and ordinary SQL when you need to filter memories by user.

Evaluation: shipping software, not vibes

Evaluation is hard but essential. I build evaluation datasets of input-output pairs and run them through the agent on every significant change.

LangSmith provides tracing and evaluation infrastructure that makes this practical. You can see every step the agent took, which tool it called, and where it went wrong.

Without evaluation, you're shipping vibes, not software. A prompt tweak that fixes one conversation can quietly break five others, and you will not know unless you have a set of cases to run it against.

Error handling needs a different mindset

LLMs are non-deterministic. The same input can produce different outputs. Your agent needs to:

  • Handle tool failures gracefully.
  • Retry with modified strategies.
  • Know when to ask the user for help instead of spinning in circles.

The last point is the one most often missed. Put a ceiling on the number of steps an agent can take. An agent that asks a clarifying question is useful. One that loops through the same failing tool call twenty times is expensive and useless.

The production architecture I've settled on

  1. A NestJS API receives user requests.
  2. It dispatches them to a LangChain agent running in a Cloud Run job, which allows longer execution times.
  3. Results are stored in PostgreSQL.
  4. Progress updates stream back to the client through Server-Sent Events.

Running the agent as a job, separate from the API, matters because agent runs are slow and unpredictable. The API stays responsive, and the user sees progress as it happens. The API side follows the patterns in TypeScript Patterns for NestJS APIs.

Don't over-engineer the agent

One lesson learned the hard way: don't over-engineer the agent. Start with a simple ReAct agent with two or three tools. Add complexity only when you have evidence that the simple approach isn't working.

The most reliable agents I've built are the simplest ones. Every extra tool is another choice the model can get wrong, and every extra layer of orchestration is another place for a failure to hide. Start small, measure with your evaluation set, and let the results tell you what to add.