This example demonstrates how to build an AI research agent using the Flyt workflow framework. The agent can answer questions by searching the web when needed and generating comprehensive answers.
The agent consists of three main nodes:
- DecideActionNode - Analyzes the question and decides whether to search for more information or provide an answer
- SearchWebNode - Performs web searches to gather information
- AnswerQuestionNode - Generates a final answer based on the gathered information
graph TD
A[DecideAction] -->|search| B[SearchWeb]
A -->|answer| C[AnswerQuestion]
B -->|decide| A
style A fill:#cba6f7,stroke:#1e1e2e,stroke-width:2px,color:#000
style B fill:#89b4fa,stroke:#1e1e2e,stroke-width:2px,color:#000
style C fill:#a6e3a1,stroke:#1e1e2e,stroke-width:2px,color:#000
- Go 1.21 or later
- OpenAI API key
- Clone the repository and navigate to the agent example:
cd cookbook/agent- Install dependencies:
go mod tidyThis will install:
github.com/mark3labs/flyt- The workflow framework (uses local version)
Run the agent with your OpenAI API key:
# Using environment variables
export OPENAI_API_KEY=your-api-key
export BRAVE_API_KEY=your-brave-key # Optional for better search results
go run .
# Or using command line flags
go run . -key your-api-key -q "What is the capital of France?"
# With Brave Search for better results
go run . -key your-api-key -brave your-brave-key -q "Latest AI developments"
# If running individual files instead of module:
go run *.go -key your-api-key-q: The question to ask the agent (default: "Who won the Nobel Prize in Physics 2024?")-key: OpenAI API key (can also be set via OPENAI_API_KEY environment variable)-brave: Brave Search API key (optional, can also be set via BRAVE_API_KEY environment variable)
🤔 Processing question: Who won the Nobel Prize in Physics 2024?
🤔 Agent deciding what to do next...
🔍 Agent decided to search for: Nobel Prize Physics 2024 winners
🌐 Searching the web for: Nobel Prize Physics 2024 winners
📚 Found information, analyzing results...
🤔 Agent deciding what to do next...
💡 Agent decided to answer the question
✍️ Crafting final answer...
✅ Answer generated successfully
🎯 Final Answer:
The 2024 Nobel Prize in Physics was awarded to John J. Hopfield and Geoffrey E. Hinton for their foundational discoveries and inventions that enable machine learning with artificial neural networks.
John Hopfield created an associative memory that can store and reconstruct images and other types of patterns in data. Geoffrey Hinton invented a method that can autonomously find properties in data, and so perform tasks such as identifying specific elements in pictures.
Their work has been fundamental to the development of artificial neural networks and machine learning technologies that are widely used today.
- The agent starts at the
DecideActionNode - It analyzes the question and any previous context
- It decides whether to:
- Search for more information (goes to
SearchWebNode) - Answer the question (goes to
AnswerQuestionNode)
- Search for more information (goes to
- If searching, it performs a web search and adds results to the context
- After searching, it returns to
DecideActionNodeto re-evaluate - This loop continues until the agent has enough information to answer
- Finally, it generates a comprehensive answer using all gathered information
The agent uses real web search APIs:
-
DuckDuckGo HTML Search (Free, no key required)
- Uses DuckDuckGo's HTML endpoint directly
- Used by default when no Brave API key is provided
-
Brave Search API
- Provides structured JSON search results
- Get a free API key from https://brave.com/search/api/
- Use via
-braveflag orBRAVE_API_KEYenvironment variable
The agent uses GPT-4.1 by default. To use a different OpenAI model, modify the model parameter in CallLLM function:
reqBody := map[string]any{
"model": "gpt-4.1", // Options: gpt-4.1, gpt-4, gpt-4-turbo, gpt-3.5-turbo
"messages": []map[string]string{
{"role": "user", "content": prompt},
},
"temperature": 0.7,
}To extend the agent with new capabilities:
- Create a new node type implementing the flyt.Node interface
- Add it to the flow in
flow.go - Update the decision logic to route to your new node
The agent includes error handling for:
- Missing API keys
- LLM API failures
- Invalid response formats
- Context cancellation
- The agent uses GPT-4.1
- Each decision and answer generation makes a separate LLM call
- Consider implementing caching for repeated queries