In Thor: Ragnarok, while looking for the Eternal Flame in Odin's vault, Loki comes upon the Tesseract and steals it off screen. In Avengers: Infinity War,
Loki pretends to offer the Tesseract, which contains an Infinity Stone, to Thanos in an effort to distract him and give the Hulk enough time to attack him. The attack fails, and Loki pretends to lend his services to Thanos, only to betray him again, leading to his death.
Given these events, why would Loki steal the Tesseract if he wasn't planning on giving to Thanos?