← All articles
AI2026-11-26 · 9 min

How to build an AI customer support chatbot like Vinted's

I collect Pokémon cards, so I spend quite a lot of time on Vinted, buying, selling and keeping track of orders. I often end up in the support chat: you used to talk to a human agent, then came the if/else chatbots, and today almost every platform I use has an AI-powered assistant.

The last time I asked about an order, I wondered how it actually works behind the scenes. So I rebuilt it in a demo environment of my own, with a fake e-commerce site and a virtual assistant built with Node.js and the OpenAI API.

The demo: a fake marketplace and its assistant

▶ Watch from 01:15

The marketplace is called Rigiro and the assistant Giro (in Italian, "giro" is a turn or a round, and "rigiro" a turn again). The AI picked the names, and I have to admit they're cute. The home page has a few sample products and, instead of building login and logout, I added a simple switcher to change user. Each demo user has a different situation to test: Giulia, for example, has an order that was just delivered and one whose shipping is late.

I open the chat and ask: "Where are my Levi's jeans?". Giro checks the orders, finds the vintage Levi's 501, gives me the order number and tells me they've been shipped and are in transit, with tracking updated a day ago, so it's normal they haven't arrived yet.

The Rigiro demo marketplace with the Giro assistant chat open: asked about the Levi's jeans, it replies that the order has shipped and is in transit
Giro checks the user's orders and answers about the shipping status.

Above the reply there's a label I added for debugging: it shows which action the chatbot took, in this case "checked your orders".

Vaguer questions work too. To "Where are my Nikes?" it answers that the white Air Force 1s are waiting to be shipped and that the seller has until Tuesday 17 November. I ask how many days are left and it works out 3 business days: I was recording on the 12th, so it took the weekend into account.

The most interesting case is Sara's: she has an open dispute over a pair of boots and a seller who doesn't reply. Giro checks the orders, opens the disputed one and then opens a ticket for a human agent, who will reply within 24 business hours. That's where the AI realises it should stop and hand over.

Giro chat in Italian: after checking the orders and opening the disputed one, the assistant opens a ticket for a human agent
A dispute with a seller who doesn't reply: Giro opens a ticket for a human agent.

How the Node.js backend works

▶ Watch from 04:50

The AI generated the frontend for me: I didn't ask for a framework like Next.js, so it went old school, with HTML, JavaScript and CSS in the public folder. No big deal, because the interesting part is all in the backend.

It's a perfectly ordinary Node server. When the frontend calls POST /api/chat with the user's message, the server runs handleChat, fetches the user and gets to runAgent, in agent.js, where almost everything happens.

Diagram of a question's journey: logged-in customer, Node.js server, the four things the AI receives, OpenAI GPT, tools and streamed reply
The journey of a question, from the chat message to the reply.

With every question the model receives four things:

  • the instructions: tone, limits and security rules;
  • the Company Brain: the company's rules and FAQs in .md files;
  • the session: today's date and who the logged-in user is;
  • the history of the conversation, stored on the server.

The session is the simplest. It's a short context message with the date of the request (needed for the deadline calculations you saw above) and the user's name, username and ID. The history matters because a chat isn't made of isolated questions: the model needs to know what was said before. To keep it simple I pass the latest exchanges, about ten in my case, with a limit you set in the config file.

src/agent.js
export async function* runAgent({ user, conversation, message, signal }) {
  const instructions = buildStaticPrompt();
  const sessionContext = { role: 'developer', content: buildSessionContext(user) };
  const turnItems = [{ role: 'user', content: message }];
  const toolLog = [];
  const usage = { input_tokens: 0, cached_tokens: 0, output_tokens: 0 };
  let textSent = false; // testo già inviato in un giro precedente
 
  for (let round = 0; round <= config.MAX_TOOL_ROUNDS; round++) {
    const input = [sessionContext, ...historyItems(conversation), ...turnItems];
    const events = await streamResponse(requestBody(instructions, input), { signal });

The Company Brain: everything the company knows, in Markdown

▶ Watch from 06:53

The Company Brain, as I called it, is everything the company knows about itself: a company-brain folder with ten Markdown files. Who Rigiro is, how to buy, how to sell, payments and fees, shipping, returns and refunds (essential, because questions in these chats are almost always about them), community rules, account and security, FAQs and escalation.

Editor with the company-brain folder open: ten Markdown files, from 01-chi-siamo.md to 10-escalation.md, and the content of the about-us file
The company-brain folder: one Markdown file per topic.

Escalation is what you saw in the demo: it describes when the chat should open a ticket for a human agent. All these files are loaded by loadBrain and put together in buildStaticPrompt, before every request.

The main file is src/prompts/system.md. It tells the model who it is (Giro, Rigiro's virtual assistant), who it's talking to and, above all, which rules to follow, to avoid hallucinations and malicious users trying to make it do things it shouldn't:

  • rely only on the Company Brain and on the data returned by the tools, without inventing rules, amounts or timelines;
  • if the question is about an order, a refund or the account, always use the tools instead of answering from memory; if the user doesn't give an order number, use the order list;
  • don't cancel orders, issue refunds or change data;
  • never ask for passwords or codes, and only answer questions about the platform;
  • if you're not sure, say so.

There's also a style section: replies in Italian, a friendly tone, short but clear sentences, and always the next step at the end, so the user knows what they can ask next. One rule I like: no "according to the documentation", it talks about "a colleague in customer service". It's a simple way to make it feel more human.

Tool calling: connecting the AI to the database safely

▶ Watch from 11:00

I used OpenAI as the model. The heart of the call is requestBody, where the body sent to the model is built. Two things matter here: prompt_cache_key, a key shared by every request that helps reuse the prompt cache and so waste fewer tokens, and above all tools.

src/agent.js
function requestBody(instructions, input) {
  const body = {
    model: config.OPENAI_MODEL,
    instructions,
    input,
    tools: TOOL_DEFINITIONS,
    tool_choice: 'auto',
    // La cronologia la teniamo noi (conversations.js): OpenAI non salva nulla.
    store: false,
    // Stessa chiave per tutte le richieste: aiuta OpenAI a instradarle
    // verso la stessa cache del prefisso statico.
    prompt_cache_key: 'rigiro-support-v1',
  };

The tools are the list of actions the agent can take. Instead of letting it run wild on a real database, I give it four specific actions and I know it will only do those:

  • lista_ordini: the user's orders, purchases and sales;
  • dettaglio_ordine: the details of a single order;
  • profilo_account: the account details;
  • apri_ticket_operatore: the handover to a human agent.

Each tool has a name, a description that tells the model when to use it, and the parameters it accepts:

src/tools.js
{
  type: 'function',
  name: 'profilo_account',
  description:
    "Restituisce i dati dell'account dell'utente loggato: verifica identità, saldo e prelievi del Portafoglio, avvisi, valutazione.",
  parameters: { type: 'object', properties: {}, required: [], additionalProperties: false },
  strict: false,
},

When the model decides to use a tool, the server runs executeTool. If I ask how many pending orders I have, it figures out it needs lista_ordini, the server reads the orders from the database and hands them back, and the model uses them to reply. Note that the userId comes from the server, not from the model: the AI can't ask for someone else's orders.

src/tools.js
export function executeTool(name, args, userId) {
  switch (name) {
    case 'lista_ordini': {
      const ruolo = args.ruolo || 'tutti';
      let orders = getOrdersForUser(userId);
      if (ruolo === 'acquisti') orders = orders.filter((o) => o.ruolo === 'acquirente');
      if (ruolo === 'vendite') orders = orders.filter((o) => o.ruolo === 'venditore');
      return {
        totale: orders.length,
        ordini: orders.map((o) => ({
          id: o.id,
          ruolo: o.ruolo,
          articolo: `${o.articolo.titolo} (${o.articolo.marca}, ${o.articolo.taglia})`,
          prezzo: o.articolo.prezzo,
          controparte: o.controparte,
          stato: o.statoDescrizione,
          dataAcquisto: o.dataAcquisto,
          // …

If the request is simple, that's the whole round: tool, result, message in the chat. If the case is tricky, escalation kicks in. In the Markdown files, for example, I wrote that orders above €200 need a ticket for a human agent. The same happens when the user explicitly asks to talk to a person, something that has actually happened to me in marketplace chats.

Tool diagram: OpenAI GPT asks the four tools for data, which only read the logged-in user's orders and account; apri_ticket leads to the human agent
The four tools only read the logged-in user's orders and account. Escalation leads to a human agent.

On the database side the AI sees only two things, orders and accounts, always filtered to the logged-in user. It can't cause any other trouble.

Privacy between users and streamed replies

▶ Watch from 15:35

I tried to catch it out. Logged in as Sara, I asked which orders Giulia has. The answer: I can't see other users' orders, but I can help you with yours. The rules and the tools do their job.

Giro chat in Italian: asked about another user's orders, it replies that it can't see them, then lists the logged-in user's orders
Other users' orders stay out of reach.

I also added streaming for the replies: instead of showing a spinner and then the whole message at the end, the message builds up as it arrives. With a quick action like "checked your orders" you barely notice, but when several steps are needed the user sees the reply come in bit by bit instead of sitting there waiting.

Static context or RAG: which should you choose?

▶ Watch from 16:57

To me it's a little project that makes sense: an AI experiment on a fairly realistic case. But let's be clear: for a company like Vinted this solution is too small. When a company has a huge number of documents that change often and maybe come in several languages, loading the whole Company Brain with every request no longer holds up, and the right path is probably RAG.

RAG has its downsides too, though. For a company with few documents that rarely change, static context is simpler, and I think there are real cases where it's actually the better choice. I might try the RAG version in a future video, so we can dig into it together.

If your company's customer support answers the same questions about orders, returns and shipping every day, this is the kind of automation I build to measure: an assistant that reads your data, follows your rules and knows when to hand over to a person.