From API to Report: Building a Currency Analysis Agent with LangGraph and Gemini

Source notebook

Repo path: gemini/orchestration/intro_langgraph_gemini.ipynb · Open on GitHub · intermediate

Builds a LangGraph currency analysis agent using Gemini on Vertex AI and an exchange-rate API.

Summary

This notebook teaches how to orchestrate a multi-stage AI agent with LangGraph and Gemini API in Vertex AI. The workflow retrieves exchange rates through a LangChain tool, loops through tool calls, reviews the results with Gemini, and generates a financial summary report. It also demonstrates memory-backed graph execution and streaming node outputs.

Key code patterns

Initialize Gemini chat model

model = ChatGoogleGenerativeAI(
    model="gemini-3.5-flash",
    project=PROJECT_ID,
    location=LOCATION,
    enterprise=True,
)

Configures Gemini through the LangChain Google GenAI integration with project and location settings.

Define API-backed tool

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

Wraps an external exchange-rate API as a LangChain tool that the agent can call.

Route tool calls

def should_continue(state: AgentState) -> str:
    messages = state["messages"]
    last_message = messages[-1]
    if last_message.tool_calls:
        return "tools"
    return "review"

Uses the model response to decide whether the graph should execute tools or proceed to review.

Compile graph with memory

workflow = StateGraph(AgentState)
workflow.add_node("agent", agent_node)
workflow.add_node("tools", tool_node)
workflow.add_node("review", review_node)
workflow.add_node("report", report_node)
graph = workflow.compile(checkpointer=memory)

Builds a stateful LangGraph workflow with distinct agent, tool, review, and report stages.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI, Frankfurter API
  • SDKs / libraries: langgraph, langchain-google-genai, langchain, requests

When to use this

Use this pattern when you need a Gemini-powered agent to call external APIs, validate results, and generate a structured report.

Gotchas & caveats

  • Requires a Google Cloud project with the Vertex AI API enabled.
  • Colab users must run notebook authentication before using the model.
  • PROJECT_ID falls back to the GOOGLE_CLOUD_PROJECT environment variable.
  • LOCATION defaults to global from GOOGLE_CLOUD_REGION when not set.
  • The exchange-rate data depends on the external Frankfurter API being available.
  • The report prompt avoids currency symbols because they might break output rendering.

Best practices

  • Separate the workflow into distinct nodes for API interaction, data validation, and report generation.
  • Use LangChain tools to expose external data sources to the agent.
  • Use conditional graph edges to route between tool execution and review.
  • Compile the graph with MemorySaver to support state management.
  • Use system and user prompts to define tool usage and the analysis task.