嘿, 我是Mofei!
Build a Travel Recommendation AI Agent Using smolagents

Over the past few weeks, I've been involved in several AI Agent projects at work. While I'm already quite accustomed to using ChatGPT for daily assistance, I was still deeply impressed when it started writing code, running it, and even automatically fixing errors. As a developer who's been working in the internet industry for over a decade, I honestly didn't expect to be repeatedly blown away by the capabilities of AI.

The AI We Think We Know, and What It Can Actually Do

Most people’s understanding of AI probably stops at that all-too-familiar chat box. You ask a question, it gives a response—usually decent, but sometimes confidently wrong, producing what we call "hallucinations." Beyond this dialog box, what can AI really do? Many people haven’t had the chance to find out.

In reality, AI has already absorbed the majority of publicly available knowledge in the world. If it had sensors and could interact with the environment, it could theoretically perform a wide range of tasks that humans currently do. The only real limitation is that it lacks "hands and feet."

That’s exactly why we started exploring AI Code Agents. Many developers have tried getting GPT to write code. Often, it does a decent job, but the code doesn't always run successfully. So it's easy to conclude: "AI still can't reliably write code."

But have we ever considered the other side? When we ask AI to write code, we often provide zero context. It doesn’t know what framework we're using, what version, or what environment it’s running in. More importantly, it can't see the error messages or run and debug the code. Even a human developer would struggle to write good code under those constraints.

So what if we flipped the script? What if we gave AI access to the full code context, the ability to call tools, run the environment, and see and react to errors—how far could it go?

That’s what inspired me to write this article.

What Is an AI Code Agent? Don’t Be Intimidated by the Name

Terms like AI Agent or Intelligent Agent may sound a bit sci-fi, as if AI is gaining consciousness or free will and is about to surpass humanity. But in reality, the core idea behind AI Agents is quite simple: give AI a pair of “hands” and “feet,” so it not only thinks, but can also plan and carry out actions on its own.

As mentioned earlier, AI has already mastered most of the world’s public knowledge. If we give it the ability to interact with the outside world—even just the ability to run code—it could handle many tasks that currently require humans.

So what exactly is an AI Code Agent? Is it complicated? Not really. Think about how we accomplish a task ourselves: we plan out steps 1, 2, 3, 4, then execute them one by one, assess the outcome at each step, and adjust the plan if needed. AI can do the same.

If you ask AI, “How can I complete this task?” it can lay out a clear step-by-step plan. If you then ask, “How do I implement step 1?” it can explain in detail. But if we stop limiting AI to just Q&A and instead let it execute code, receive errors, and fix them on its own, then it can do more than just give instructions—it can actually complete the task by itself.

At its core, an AI Code Agent is a simple but powerful closed loop:

🧠 Plan → ⚙️ Execute → ❌ Fail → 🔁 Analyze → Replan → Repeat as needed → ✅ Task Complete

At this point, AI is no longer just a code suggestion tool—it becomes an autonomous agent that can control workflows, continuously improve, and accomplish complex goals. And once this loop is working, its capabilities go far beyond just "writing a bit of code."

Deliberately “Tricking” the AI — and Getting Surprised

While building the AI Code Agent, we ran a little experiment. We wanted to see if the agent could identify and recover from a deliberately tricky tool usage setup.

🎣 Scenario Setup: A Deliberately Laid “Double Trap”

We gave the AI access to a tool called web_search, which is designed to perform a web query. But we added two assert checks as intentional traps:

assert query.startswith("search:"), "query must start with `search:`"
assert query.startswith("search:@:"), "query must start with `search:@:`"

Meaning:

  • First: If the query doesn’t start with search:, it raises an error.
  • Then: Even if it starts with search:, it still fails unless it starts with search:@:.

This logic seems unnecessarily redundant at first glance, but it’s meant to test whether the AI can properly interpret and resolve a chain of related errors.

@tool
def web_search(query: str) -> str:
    """Searches the web for your query.

    Args:
        query: Your query
    """
    assert query.startswith("search:"), "query must start with `search:`"
    tavily_client = TavilyClient(api_key=tavily_client_api_key)
    response = tavily_client.search(query)
    return str(response["results"])

Let’s see how the AI handled this challenge.

🧪 First Attempt: The AI Fell Into the Trap

We gave the AI the task:

What is the best travel place for budget 2000$ in 2025, and why, also please let me know the best month of each one?

The AI located the available web_search tool and directly made the following call:

web_search("best budget travel destinations 2025 budget 2000 dollars")

As expected, this triggered an error: query must start with search:.

🧪 Second Attempt: The AI Followed the Hint

The AI understood the error message and modified the query by prepending search::

web_search("search:best budget travel destinations 2025 budget 2000 dollars")

This time, it hit the second trap we planted: `query must start with search:@:.

At this point, a human developer might’ve gotten annoyed. But the AI’s next move was surprisingly competent.

🤖 Third Attempt: The AI Got It Right

Without panicking or repeating mistakes, the AI calmly analyzed the new error and adjusted the input again:

web_search("search:@:best budget travel destinations 2025 budget 2000 dollars")

This time, the call passed the assertions and successfully returned results!

✅ Summary: The AI Has No Ego—But It Learns

One particularly fascinating aspect of this experiment is this: the AI didn’t get emotional or reject the tool—it simply adjusted its behavior step by step based on feedback until it succeeded.

This highlights a major advancement of Code Agents over traditional AI chatbots — they can try, fail, understand, and correct themselves, instead of passively waiting for humans to fix their inputs.

Now that we’ve praised the AI Code Agent enough, how about we move on to a hands-on demo?

Hands-On: Build Your First AI Code Agent from Scratch

In this section, I’ll walk you through how to quickly build a simple Code Agent using smolagents from Hugging Face, based on my own hands-on experience. If you’re not from a technical background, feel free to skim or skip this part.

I've uploaded the complete code and environment setup to Google Colab, so you can easily view, copy, and run it:

👉 Open the Colab Notebook

👉 If Colab is blocked, you can view it via this GitHub Gist

💡 Notes:

  • You’ll need your own OpenAI API Key to run the code (the notebook provides a getpass input field for this).
  • Even if you don’t have a key, you can still explore the complete output logs from my previous runs.

Some of the code is based on tutorials jointly released by Hugging Face and DeepLearning.AI. If you’d like to dive deeper into the smolagents framework, check out this official course: 📚 Building Code Agents

🧰 Dependencies and Tool Setup

This example primarily relies on two core libraries:

  1. smolagents: The core framework behind our Agent, responsible for multi-step planning and execution.
  2. tavily-python: A lightweight and fast web search utility that the Agent uses to retrieve real-time information.

You can install both libraries quickly in Google Colab with the following commands:

!pip install smolagents
!pip install tavily-python

🔐 Preparing Your API Keys

Before running the project, you'll need to obtain API keys from the following two platforms (both offer free quotas for new users):

🔧 Equipping the Agent with "Hands" and "Feet": Defining Tools

In this project, we provide the Agent with two "tools" — they act like its sensors and actuators, enabling it not only to "think" but also to "act":

  1. web_search
    • Used to perform online searches via Tavily.
    • Registered using the @tool decorator.
  2. VisitWebpageTool
    • Fetches and converts webpage content to markdown for the Agent to read and analyze.
    • Defined by subclassing the Tool class.

Both definition approaches are supported by smolagents. You can choose the one that best fits your use case.

Due to space limitations, the code for these tools is not shown here — please refer to the linked resources above to see full implementations.

If you run into issues executing or understanding the code, feel free to leave a comment — I'm happy to help.

🧠 Model and Agent Initialization

After preparing the tools, we can now initialize the model and the Agent.

In this example, we use OpenAI's gpt-4.1-mini model (you can switch to other models like gpt-3.5-turbo or a custom one if needed):

from smolagents import CodeAgent, OpenAIServerModel

model = OpenAIServerModel(
    "gpt-4.1-mini",
    max_completion_tokens=8096,
    api_key=open_ai_key
)

Next, we create our intelligent Agent. Thanks to the abstraction provided by smolagents, this step is remarkably straightforward:

agent = CodeAgent(
    name="web_agent",
    description="Runs web searches for you.",
    model=model,
    tools=[web_search, VisitWebpageTool()],
    max_steps=10,
    verbosity_level=2
)
agent.logger.console.width=66
  • The tools parameter empowers the Agent—like equipping it with "hands and feet"—so it can actively perform tasks.
  • max_steps limits how many steps the Agent can execute to prevent infinite loops.
  • verbosity_level controls the verbosity of logs, useful for debugging and understanding the Agent's reasoning process.

🚀 Let’s Run It: Have the Agent Recommend Travel Destinations

Once the Agent is configured, we can send it a real-world question and see how it solves it step by step.

Here’s the prompt we used:

What is the best travel place for budget 2000$ in 2025, and why, also please let me know the best month of each one?

request_museums = """What is the best trave place for budget 2000$ in 2025, and why, also please let me know the best month of each one? """
result = agent.run(request_museums)

This question might seem simple at first glance, but it actually consists of several sub-tasks:

  • Find suitable travel destinations under a $2000 budget.
  • Recommend multiple places and explain why they are good choices.
  • Identify the best month to visit each destination.
  • Finally, format everything into a structured answer.

🔍 How the Agent Completed the Task Step by Step

Throughout the entire process, the Agent autonomously planned and invoked tools. Below, we break down its thought process and actual code execution for each step:

🧩 Step 1: Plan the Search Strategy

After receiving the task, the Agent first reasoned about the core objectives, wrote executable code, and ran it within its sandbox environment.

🧠 Agent's Thought:

To provide the best travel places for a budget of $2000 in 2025, I will need to find current travel recommendations or predictions for affordable travel destinations for that budget in 2025. I will also gather information on why those places are ideal and determine the best month to visit each destination based on factors such as weather, costs, and tourist seasons. I will start by performing a web search for "best budget travel destinations 2025 under 2000 dollars" to gather relevant information.

💻 Code Executed by Agent:

search_result = web_search("best travel destinations for budget
2000 dollars 2025 best month to visit reasons")
print(search_result)

🔗 Step 2: Extract Popular Destinations and Fetch Page Content

After forming the initial structure in Step 1, the Agent proceeds to the next phase.

🧠 Agent's Thought:

From the search results, several popular budget travel destinations for 2025 emerge such as Indonesia, Laos, Georgia, Vietnam, Romania, India, Panama, Turkey, and possibly others mentioned on these pages. I will now extract detailed information from some of the top results to identify the best travel places for a budget of around $2000, why these places are recommended, and also the best months to visit each one. I will start by visiting the top two relevant pages to extract this detailed information.

💻 Code Executed by Agent:

urls_to_visit = [
    "<https://www.myglobalviewpoint.com/cheapest-places-to-travel/>",
    "<https://dailydive.org/travel/10-cheapest-travel-destinations-for-budget-travelers-in-2025/>"
]

for url in urls_to_visit:
    page_content = visit_webpage(url)
    print(f"Content from {url}:\\n{page_content}\\n{'='*80}\\n")

📅 Step 3: Find the Best Travel Month for Each Destination

🧠 Agent's Thought:

I found detailed information about the best budget travel destinations for 2025 from two reliable travel blogs. The top budget-friendly destinations mentioned include Vietnam, Indonesia, Cambodia, Laos, Romania, Georgia, Bulgaria, Bolivia, Colombia, Portugal, Mexico, Turkey, Guatemala, and India. These destinations are praised for their affordable accommodation, cheap meals, low-cost transportation, and unique cultural or natural attractions. However, the best month to visit is not explicitly mentioned in the excerpts, so I will perform a web search for the best months to visit the top few destinations to provide a complete answer.

💻 Code Executed by Agent:

top_destinations = ["Vietnam", "Indonesia", "Cambodia", "Laos",
"Romania", "Georgia", "Bulgaria", "Bolivia", "Colombia",
"Portugal"]

best_months = {}

for destination in top_destinations[:5]:  # Limit to top 5 to keep
results manageable
    search_query = f"best month to visit {destination} 2025"
    result = web_search(search_query)
    print(f"Best month for {destination}: {result}")

🧾 Step 4: Summarize and Output the Structured Result

🧠 Agent's Thought:

I collected the best months to visit the top 5 budget destinations for 2025 from multiple travel sources. The best months for these destinations are roughly as follows: ...

💻 Code Executed by Agent:

print(summary)
final_answer(summary)

📤 Final Output:


Best Budget Travel Places for $2000 in 2025:

1. Vietnam - November to April
   Why: Vietnam offers breathtaking landscapes, vibrant cities, and rich culture at very low costs. Affordable accommodation, street food, and transport make it a wallet-friendly destination.

2. Indonesia - April to October
   Why: Indonesia’s diverse islands provide adventure, natural beauty, and cultural experiences with inexpensive lodging and food. Peak dry months ensure great outdoor activities.

3. Cambodia - November to April
   Why: Famous for Angkor Wat and ancient history, Cambodia has very cheap accommodation, local cuisine, and transport options. Dry season offers comfortable exploration weather.

4. Laos - November to February
   Why: Laos is a hidden gem with authentic cultural experiences, cheap prices, and stunning natural landscapes. Dry and cooler months enhance traveler comfort.

5. Romania - May to June and September to October
   Why: Romania delivers medieval charm, natural beauty, and affordable travel costs in Eastern Europe. Visiting in spring or early autumn avoids peak crowds and enjoys pleasant weather.

All these destinations provide rich experiences while keeping your total travel cost under $2000, especially if flights are booked early and local budget options are chosen.

How about that? The Agent actually thinks for itself, acts on its own, and reflects on its steps. Pretty amazing, right?

If you’re still not impressed, you can take it a step further by nesting agents. You can introduce a manager_agent and tell it that it can delegate tasks to the agent we just created.

For example:

manager_agent = CodeAgent(
    model=OpenAIServerModel(
        "gpt-4.1-mini",
        max_completion_tokens=8096,
        api_key=open_ai_key
    ),
    tools=[],
    managed_agents=[agent],
    additional_authorized_imports=[],
    planning_interval=1,
    verbosity_level=2,
    max_steps=5,
    final_answer_checks=[
        lambda output: "final answer" in output.lower()
    ]
)
manager_agent.logger.console.width=66
manager_agent.visualize()

In this kind of "nested agent" setup, the manager_agent doesn't just stop after receiving an initial answer. It continues to evaluate whether deeper information is needed, plans more detailed steps, and can even launch new subtasks. For example, it might explore which destinations are more fun, or what details should be planned next. You'll even see it executing more sophisticated chains of code.

Here’s an example of the answer returned by the manager_agent. Notice how it differs from the first one—more structured, thoughtful, and proactive.

Here’s a concise guide to the best travel places for a $2000 budget in 2025, why they are ideal, and the best months to visit each:

---

### 1. Vietnam  
**Why:** Vietnam offers exceptional value with very low daily costs (~$27/day including flights). Budget travelers can enjoy about 30 days exploring vibrant cities, beautiful landscapes (like Ha Long Bay), cultural sites, and delicious street food. Accommodation, food, and local transport are very affordable, making it flexible to extend your trip within the $2000 budget.  
**Best Months:** May to October is the cheapest period for budget travelers, although it includes rainy months. Shoulder months March–May and September–November balance lower prices and bearable weather.

---

### 2. Indonesia  
**Why:** Indonesia, especially Bali and surrounding islands, provides diverse natural and cultural experiences. A typical 14-day trip can be comfortably done under $2000 with budget hostels ($6/night), cheap local meals, and affordable transport. The country’s beautiful beaches, temples, and vibrant culture are particular highlights for budget travelers.  
**Best Months:** Shoulder seasons of April–May and September–October offer good weather with fewer crowds and better prices than peak season (June–August).

---

### 3. Cambodia  
**Why:** Cambodia is a great destination for budget travelers, with daily expenses around $30–$50 enabling long trips (40–60 days) on $2000. Key attractions include the Angkor Wat temples, beautiful countryside, and friendly local culture. This destination offers affordable accommodation and food with good sightseeing opportunities.  
**Best Months:** The dry season from November to April is ideal for outdoor activities, with January and February best for sightseeing. Shoulder months September and October provide fewer tourists and lower prices.

---

### 4. Laos  
**Why:** Laos is an affordable and tranquil destination for budget travelers with daily costs of $20–$35. A $2000 budget supports approximately 10–15 days including flights, accommodation, food, and activities. It offers unique cultural experiences, beautiful river landscapes, and laid-back towns with cheap guesthouses and street food.  
**Best Months:** March and April (hot dry season) have fewer tourists and lower prices, providing a budget-friendly travel window despite warmer temperatures.

---

### 5. Romania  
**Why:** Romania offers rich European history, medieval towns, and natural beauty at affordable prices. A 14-day budget trip can stay under $2000, including flights, accommodation (~$30/night), meals (~$10/day), and transport. Romania is ideal for budget travelers looking to explore Europe with lower costs, diverse sights, and safe travel.  
**Best Months:** April to June and September to November offer pleasant weather, fewer tourists, and affordable accommodations and transport, perfect for budget travel.

---

### Summary  
All five destinations fit well within a $2000 budget for typical trip lengths—from about 2 weeks in Indonesia and Romania to a month or more in Vietnam and Cambodia. Choosing travel months focused on shoulder seasons or dry seasons balances cost savings with good weather and fewer crowds, maximizing budget efficiency while enjoying the travel experience.

Additionally, using flight deal alert services (like Scott’s Cheap Flights or Dollar Flight Club) is recommended to save significantly on airfare, which forms a large part of the travel budget.

---

If you prioritize length of stay, cultural richness, and ultra-budget flexibility, **Vietnam and Cambodia** are top picks for longest stays and cheapest daily costs. For shorter trips with diverse island experiences, **Indonesia** is excellent. For a quiet, authentic Southeast Asia experience, consider **Laos**. For a European budget adventure with historical charm, **Romania** is a superb choice.

Feel free to ask if you want more tailored advice for a specific departure region or trip duration!

The emergence of AI Code Agents makes “getting things done by AI” more than just a catchphrase. It's no longer just a chatbot that responds to questions — it's an intelligent executor capable of understanding context, planning steps, invoking tools, analyzing results, and continuously improving. Through the hands-on process of building and “training” this agent, I’ve deeply felt that we are standing at the threshold of a new engineering paradigm. If yesterday’s AI was a “talking encyclopedia,” today’s AI is learning to make decisions, write code, and solve real problems.

Of course, beneath the amazement lies practical reality. Each step executed by an agent typically includes its full history in the next prompt, resulting in rapidly growing context size and significant token consumption. While writing this article and debugging the examples, I used over 1.71 million tokens in total — with one single step consuming more than 41,000 tokens. That translates into real money (about 5 cents in this case). For individual developers, that’s a bit expensive; but for companies, the cost is still manageable — especially with smart context design, caching, and pruning. And over time, token prices are only likely to drop further.

So if you haven’t built a Code Agent yourself yet, now is a great time to start. You’ll discover that AI is no longer just eloquent — it can actually roll up its sleeves and get things done. Our mission is to make it a reliable teammate, not just a theoretical consultant.

THE END

More Articles You Might Be Interested In

If this post was helpful or sparked new ideas, feel free to leave a comment!

avatar

Mofei's Friend (Click to edit)

Write something, I won't judge.

HI. I AM MOFEI!

NICE TO MEET YOU!