Infinity War revealed that children of Thanos had some off screen ability to track specific locations of Infinity Stones. They used this tracker to find Vision’s Mind Stone.
Given that revelation, Thanos would have used the same tracker to know that the Tesseract / Space Stone would be on the Asgard refugee ship with them since apparently this tracker can detect in spite of Loki’s ability to hide it.
At that point Thanos just requires Loki to cough it up from whatever place he’s hidden it. And would know if Loki was telling the truth.