Until, when they came upon the valley of the ants, an ant said, “O ants, enter your dwellings that you not be crushed by Solomon and his soldiers while they perceive not.”
The following excerpt is taken from “Towards Understanding the Quran” under the commentary of the above verse note 24:
Rationally also, it is not at all inconceivable that an ant should warn members of its own species of an impending danger and tell them to get into their holes. As for the question as to how the Prophet Solomon (peace be upon him) heard it, the answer is: It is not all difficult to understand the crude speech of an ant for a person whose senses can comprehend and receive a subtle message like the word of revelation.
Quran 27: 19:
So [Solomon] smiled, amused at her speech, and said, “My Lord, enable me to be grateful for Your favor which You have bestowed upon me and upon my parents and to do righteousness of which You approve. And admit me by Your mercy into [the ranks of] Your righteous servants.”
The following excerpt is taken from the same book note 25 and 26:
“Bestow upon me that I may be thankful” means this: “O my Lord! The wonderful powers and abilities that You have given me are such that if I become even a little forgetful and heedless, I might transgress the bounds of service and be puffed up with pride and go astray. Therefore, O my Lord, bestow upon me so that I may remain grateful to You for all Your blessings instead of being ungrateful.”
“And admit me by Your mercy among Your righteous slaves” probably implies this: “I should be included among the righteous in the Hereafter and should enter Paradise along with them.” For a person who does righteous acts will automatically be righteous, but one’s entry into Paradise in the Hereafter cannot come about merely on the strength of one’s good works, but it will depend on Allah’s mercy. According to a Hadith, the Prophet (peace be upon him) once said, “Merely the deeds of any one of you will not enable him to enter Paradise.” It was asked, “In your case too, O Messenger of Allah?” He replied, “Yes, I also shall not enter Paradise only on the strength of my deeds, unless Allah Almighty covers me with His mercy.”
This prayer of the Prophet Solomon (peace be upon him) on this occasion becomes irrelevant if an-namal is taken to mean a tribe of human beings and namlah a member of that tribe. After all, there could be nothing extraordinary in the warning given by a member of a human tribe to the people of his tribe about the approaching troops of a powerful king that it should have induced the king to make such a prayer to Allah. However, a person having such a wonderful power of comprehension that he may hear the speech of an ant from a distance and also understand it, is certainly something extraordinary, which can involve a person in self-conceit and vanity. In such a case only the prayer of the Prophet Solomon (peace be upon him) can be relevant.
Maududi, Abul Ala (2010). Tafhim ul Quran. (note 24, 25 and 26). https://www.englishtafsir.com/Quran/27/index.html