Ai chat model run twice and incurring cost twice

Hi I want to ask about ai chat model running twice because of tool usage. I have an ai agent that needs to look into database and if there’s a recorded data with the same item it will remove it from the output, e.g. the ai agent will receive bunch of data and it will search the database if there isn’t duplicate data all the data will be outputted but if there’s a duplicate data it will outputting only the unduplicated data.

When I ran the ai agent I notice that the ai chat model run twice, before the tool usage and after the tool usage. When I looked at the log I saw the first call ai agent will say something like “I will look at the database for duplicate data” and the second call outputting the output that will be use for the second node.

The problem is when I looked for the second ai chat model call I notice it has the same input prompt from the first call and when I looked at the usage log I notice that it indeed run with twice cost. Is this the expected behavior of the ai agent or not?

Hi
Since you’re using the AI Agent node, it’s normal for the model to run twice — once to decide to use the tool, and again after the tool returns data. This is how the agent is designed (ReAct logic). Both calls are counted separately in usage.

Thanks for your clarification, I thought this behavior is and abnormal because how the the ai chat model run more than once. A problem arise when I use http request node to do query search and make the ai chat model ran 9 times that costing 9x of a single task. Since this is the expected behaviour I think I need to find a solution so that the ai chat model doesn’t ran multiple times.

Yes, it’s expected. The AI Agent calls the chat model twice: once to decide what tools to use (planning), and again after tool results are returned (final reasoning). Each call consumes tokens separately, so you are billed for both.

You can do alternative way for search node like SerpAPI or Tavily