If you make this into an addon, then you can set the AI operation to be run on the note_will_be_added hook since that is triggered when AnkiConnect adds a note. There might be some issues if you quickly add two words and a new operation begins before the previous one has finished but other than that it should be fine.
I’ve done similar stuff to automate Japanese vocabulary note stuff with AI: GitHub - jhhr/anki_japanese_note_ai_ops
I don’t import openai as I’d previously had problems with other addons I had installed also importing openai / pydantic. Some better solutions to avoid this are around but I haven’t tried them yet.
The code used to be quite simple but then I made it possible for the operations to be run in async so that multiple API requests can be awaited at once and there’s been some nasty bugs like Qt crashing because an element is in different thread.