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

AI Agents with JavaScript

AI Agents with JavaScript

A presentation from Belgrade.js meetup.

------------------------
How AI Agents work and how to build them with some JavaScript
------------------------

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.

Slobodan Stojanović

April 03, 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. The prompt is a set of textual instructions that fit

    LLM's context and other limitations
  3. "Write a 500-word article about the bad influence of Amazon's

    RTO policy on Lambda cold starts" Also valid prompt
  4. Some parts of your instructions might be more important than

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

    all the time Way more weird than this one, trust me
  6. Some other LLMs can answer this question! But I like

    Claude. Can I help it to answer?
  7. I know ChatGPT can search the internet. But that's also

    an agent. It's just built into the ChatGPT product.
  8. 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
  9. Not because of the code complexity of a while loop,

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

    It depends on your use case! But be careful.
  11. 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
  12. 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.
  13. Just be careful not to expose your LLM secret keys

    and limit their usage because, remember, these while loops can be expensive.
  14. 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
  15. LLMs are good at talking to humans, but these replies

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

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

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

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

    you don't really need to write your own while loop!
  20. It's good to understand how these "while loops" work. But,

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

    - AutoGen - Amazon Bedrock Agents - Many other alternatives…
  22. LlamaIndex Supported LLMs: - OpenAI LLms - Anthropic LLms -

    Groq LLMs - Llama2, Llama3, Llama3.1 LLMs - MistralAI LLMs - Fireworks LLMs - D"pS"k LLMs - ReplicateAI LLMs - TogetherAI LLMs - HuggingFace LLms - D"pInfra LLMs - Gemini LLMs
  23. But we have more things we want to add to

    the context (such as Knowledge bases, documents, etc.)
  24. Things to know about RAG - RAG is often explained

    in complicated terms, but it's a simple (and powerful) concept - You can use a vector database for RAG, but it's not necessary - You can store data almost anywhere, in vector DB, PostgreSQL, S3…
  25. 1. Split your Knowledge base into pieces and create vectors

    for each piece 2. Create vectors from user input 3. Do a vector search for the closest knowledge base matches to the user input 4. Add a knowledge base pieces to the prompt with an explanation/instructions 5. PROFIT How to use RAG:
  26. Built-in tools pros: - Less errors (no n"d to force

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

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

    For example, it can look similar to the following diagram
  29. 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
  30. The complete code example is more complicated. I'll publish an

    article with a detailed code example and step-by-step guide soontm
  31. @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