N8n AI Agent x Google Sheet Log Filter

Hi everyone, I’m building an AI Agent in n8n that receives chat messages and is connected to a Google Sheet which acts as a running log (timestamp, input, output).

Goal:
When a user sends a query like
“What was discussed with Elon?”
→ …the agent should check the Google Sheet and return the last 10 relevant entries containing that keyword (either in the “Input” or “Output” column), sorted by timestamp.


:wrench: Setup:

  • Trigger: When chat message received
  • Google Sheets: Get row(s) (loads full log)
  • Code Tool: Filters rows by keyword
  • AI Agent: Formats final response


:red_exclamation_mark:Problem:

I’m using the Code Tool to filter entries based on the keyword provided in the message (chatinput), but I always get the full log as output, regardless of what term I search for.

Here’s a simplified version of my Code Tool logic:

js

KopierenBearbeiten

const searchInput = $input.item.json.chatinput?.toLowerCase() || "";
const rows = $input.all();

const matches = rows.filter(row => {
  const input = row.json?.Input?.toString().toLowerCase() || "";
  const output = row.json?.Output?.toString().toLowerCase() || "";

  return input.includes(searchInput) || output.includes(searchInput);
}).slice(-10);

Even when I type Hoffmann, the tool returns all rows, including ones that have nothing to do with that name.


:white_check_mark: What I’ve already tried:

  • Extracting fixed keywords (e.g. [“hoffmann”, “atpuri”]) from the message and filtering by those

  • Using .includes(searchInput) directly

  • Converting everything to lowercase with .toString().toLowerCase()

  • I also tried to do it with google Sheet as a tool but it can only filter for values and not “contains” the word “hoffmann”.


:brain: Question:

  • Is this the right way to filter dynamically based on user input?
  • Should I use a different tool (e.g. Function node or JS Function via HTTP request)?
  • Has anyone built something similar (AI Agent searching chat logs)?

Thanks a lot – happy to share the full workflow JSON if helpful!

Hey @Luca2

when a user asks about discussion with Elon, what would be the keyword? Elon?

What would be helpful is to:

  • understand what is the keyword
  • get the log example to test with

It will be a log for all the chats of the ai agent I had.

Like this:

I can share you the example log I had created.

When I ask the AI Agent:
What was discussed about customer Elon last conversations this week.

It should go into the Google Sheets → scan for Chats where it was mentioned “Elon” and filter for last 7 days, then give them as an Output.

So the keyword is Elon and the date is last 7 days.

This is my attempt to solve this:

Here is an example of our conversation with the agent:

What kind of code is this? where do I add it?

as a tool to the ai agent as code tool? and what is the prompt?

this is the workflow, copy it and just paste to an empty n8n canvas

Thanks for your help!

I got one question:

if the chatlog is for example 2k rows and I will give it all to openai, doesnt it cost a lot of tokens to filter for it?

this is what I build last 30min:

do you think this makes sense to handle it like that?

Good question!
I probably would not send 2k rows to the model, unless this happens rather infrequently or money is not a problem. But… What else I wouldn’t do is keep logs in the Google Sheet. If you need to selectively pull logs to minimize the token cost, consider having your logs in the database, which you can then query/filter for a specific date range before providing the logs to the model.

Thank you!
Why wouldnt you choose google sheets as log entries?

And what kind of database do you mean to store data in?

Another good question.

Why do I think that GSheets is not the best choice for keeping logs… to name a few:

  • it is limited in row capacity before performance REALLY slows down
  • it has no built-in rotation system or archival system
  • very poor scalability for large datasets
  • limited querying and filtering capabilities compared to databases
  • no native indexing for fast searches
  • too easy to accidentally edit or delete data
  • weak concurrency handling, where simultaneous edits can cause conflicts

This is just what comes to mind right away. As for what to use instead,… if you would like to keep the logs in the table, I would probably at least consider airtable, much easier to work with (IMHO). If you really need to have a fast, reliable way to query filter and sort entries - I would go with a mysql/postgres (given your data is tabular).