AI Agents for Engineers (Evolution of AI Agents)
Source notebook
Repo path:
workshops/ai-agents/ai_agents_for_engineers.ipynb· Open on GitHub · intermediate
Compares zero-shot, LangChain, and LangGraph essay generation with Gemini and Tavily search.
Summary
This notebook demonstrates three ways to generate essays with Gemini: a single zero-shot call, a LangChain pipeline, and an iterative LangGraph agent. It configures Google GenAI or Vertex AI access, adds Tavily search for recent information, then builds planning, research, writing, critique, and revision workflows.
Diagrams
1-prompt-essay.png — source
2-langchain-essay.png — source
3-langgraph-essay.png — source
Key code patterns
Create Gemini client
from google import genai
client = genai.Client()
MODEL_ID = "gemini-3.5-flash"
response = client.models.generate_content(
model=MODEL_ID,
contents=prompt,
)Shows the simplest zero-shot path for generating an essay with the Google GenAI SDK.
Switch LangChain Gemini backend
if client.vertexai:
model = ChatVertexAI(project=PROJECT_ID, model=MODEL_ID, temperature=0)
else:
model = ChatGoogleGenerativeAI(model=MODEL_ID, temperature=0)Lets the same LangChain workflow run against Vertex AI or the Gemini Developer API.
Add Tavily research to chain
tavily_tool = TavilySearchResults(max_results=5)
def research_fn(topic):
response = tavily_tool.invoke({"query": topic})
return "\n".join([f"- {r['content']}" for r in response])Injects web search results so the essay can cover recent events not known to the model.
Compile LangGraph agent
builder = StateGraph(AgentState)
builder.add_node("planner", plan_node)
builder.add_node("generate", generation_node)
builder.add_node("reflect", reflection_node)
builder.add_node("research_plan", research_plan_node)
builder.add_node("research_critique", research_critique_node)
graph = builder.compile(checkpointer=memory)Defines an iterative stateful agent that plans, researches, drafts, critiques, and revises.
Structured search queries
class Queries(BaseModel):
"""Variants of query to search for."""
queries: list[str]
queries = model.with_structured_output(Queries).invoke(messages)Constrains the model to produce search-query lists for research planning and critique follow-up.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Gemini Developer API, Vertex AI, Tavily
- SDKs / libraries:
google-genai,langchain,langgraph,langchain-google-genai,langchain-google-vertexai,langchain-community,tavily-python,pydantic
When to use this
Use this pattern when a writing task needs Gemini plus external search, planning, and iterative critique for recent or research-backed content.
Gotchas & caveats
- The notebook requires GOOGLE_API_KEY for Gemini Developer API or Google Cloud credentials plus Vertex AI API enabled for Vertex AI.
- TAVILY_API_KEY must be set before running the search-backed LangChain and LangGraph sections.
- The notebook installs packages and then restarts the Jupyter runtime before continuing.
- PROJECT_ID must be set or available from GOOGLE_CLOUD_PROJECT when using Vertex AI.
- LOCATION defaults to us-central1 from GOOGLE_CLOUD_REGION when not set.
- The graph stops revisions only when revision_number exceeds max_revisions.
Best practices
- Use temperature=0 for deterministic LangChain and LangGraph essay workflows.
- Verify whether the GenAI client is using Gemini Developer API, Vertex AI project/location, or Vertex AI express mode.
- Use Tavily search when the prompt asks about recent events that the model may not know.
- Separate planning, research, writing, reflection, and critique research into explicit LangGraph nodes.
- Compile the graph with MemorySaver and run it with a thread_id for state management.
- Limit generated research queries to 3 max in the research prompts.
Related
- Concepts: Agents & ADK · Prompt Engineering · Function Calling & Tools
- Entities: Vertex AI · Google GenAI SDK · LangChain · LangGraph · Gemini
- Area: Workshops Notebooks
- Best practices: Agents & ADK - Best Practices · Prompt Engineering - Best Practices · Function Calling & Tools - Best Practices