Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AI Agents: How Do They Work and How to Build Th...

AI Agents: How Do They Work and How to Build Them @ Shift 2025

Shift 2025, Zadar, Croatia

Did you know you already have all the skills you need to build an AI agent? You only need some JavaScript knowledge and an API key (optional but highly recommended). AI agents have been a hot topic recently. Everybody talks about amazing, intelligent AI agents that often sound like popular science-fiction movies. Let’s talk about AI agents in plain and simple language and learn how to build them using JavaScript and your favorite LLM.

Avatar for Slobodan Stojanović

Slobodan Stojanović

September 15, 2025
Tweet

More Decks by Slobodan Stojanović

Other Decks in Programming

Transcript

  1. Prompts: - Prompts are instructions - You te! an LLM

    what you want, and it tries to reply based its training and your instructions - more clear instructions = better reply - LLM always answer, but not always based on truth
  2. Some parts of your instructions might be more important than

    other or you might want to make them repeatable
  3. Meet my "friend" Claude. I ask it many weird things

    all the time Way more weird than this one, trust me
  4. This specific question is interesting because Claude could not answer

    it* *It can today, I know! Don’t kill my vibe just yet…
  5. Some other LLMs can answer this question! But I like

    Claude. Can I help it to answer?
  6. I know ChatGPT can search the internet. Claude 4 can,

    too. But these are also agents. These agents are now built into the ChatGPT and Claude products.
  7. An AI Agent is like a "while" loop that keeps

    asking available tools to provide additional information or capability until it has all it needs to complete the task or answer the question
  8. Not because of the code complexity of a while loop,

    but because you invoke an LLM at least once in each iteration!
  9. What does it mean to be "expensive" in this context?

    It depends on your use case! But be careful.
  10. How to be careful: - Define spending limits - Make

    sure you do not iterate indefinitely (i.e., stop after N retries) - Use cheaper models for simple evaluations - Add monitoring and alarms
  11. But you can write your "while loop" anywhere you need

    it. This while loop can be in an app, in a terminal, on a server, in a browser, etc.
  12. Just be careful not to expose your LLM secret keys

    and limit their usage because, remember, these while loops can be expensive.
  13. How to write a while loop? - Define a system

    prompt with clear explanation of a! the tools you want to support (i.e., when and how to invoke them) - Ask an LLM to reply in the strict JSON format - Make sure you parse and validate reply correctly - Handle errors and have a limit on the number of iterations
  14. LLMs are good at talking to humans, but these replies

    are not easy to parse in the code
  15. User: // Some long instructions But always reply with valid

    JSON and nothing else! Here's your JSON: ```json { "some": "JSON", Assistant:
  16. User: // Your instructions Answer with valid JSON and nothing

    else. { " Assistant: System: // Your system prompt
  17. // Your instructions Answer with valid JSON and nothing else.

    { " // Your system prompt some": "valid", "JSON": true } User: Assistant: System:
  18. It's good to understand how these "while loops" work. But,

    you don't really need to write your own while loop!
  19. Popular AI Agents tools & frameworks: - LlamaIndex - LangChain

    - AutoGen - Amazon Bedrock Agents - Many other alternatives…
  20. Built-in tools pros: - Less errors (no n"d to force

    an LLM to return JSON) - No third-party tools - We! defined format
  21. Built-in tools cons: - A bit harder to switch models

    (you n"d to write a sma! adapter/wrapper)
  22. We could build this in many different ways in production.

    For example, it can look similar to the following diagram
  23. In production you need to think about: - Web application

    firewa! (WAF) with rate limiting - Error handling - Rate limits (for your app + LLMs + other services) - Monitoring - Conversation storage (i.e., DynamoDB) - And many other things
  24. The complete code example is more complicated. Check the full

    example here: https://slobodan.me/posts/ai-agents/
  25. @slobodan_ • An AI Agent is like "while" loops with

    tools • You have all the skills you need to build tools • AI agents aren't scary, and they can be useful • Go, build agents, and have fun