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.
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:
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.
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).