1. Any number of tags able for setup before battle (in AI code, with custom naming)
2. To hold on each tick N flags for each tag and for each enemy
3. When tag is used on certain enemy (set up, or removed) mark that tag with flag (only for that enemy)
4. Repeat according to code design, or until all the flags are used on one enemy (All at the same tick of the game)
5. Allow to erase all the tags from each enemy once per game tick without use of any flags (it will give you an ability to erase tags from enemy firstly and then setup up to N tags for him).
* Gives not more than MAX amount (MAX is the amount of unique named tags in your AI code, assuming you can name them by Player) per enemy (+1 erase operation) - prevents loops
* Instantly tag everything you need, up to maximum amount of tags per enemy
If it would be possible to erase everything multiple times, well... it even may allow you to calculate something, partially erase memory and continue But I guess it would be enough to wait one tick for that, or tagging loops may be possible. Maybe it would be nice to have 2-3 erase operations to spare.