"Happy Ending", a novelette by Henry Kuttner and C. L. Moore (generally published as by Henry Kuttner but attributed to Kuttner & Moore by the ISFDB), first published in Thrilling Wonder Stories, August 1948, available at the Internet Archive. It was also the answer to this old question. You may have read it in one of these compilations.
This is the way the story ended:
[. . . .]
But Kelvin didn't answer. He had suddenly remembered the other thing the robot had told him about that gadget which established rapport when he pressed the button. He pressed it now—and nothing happened. The gadget had gone dead. Its task was finished, which obviously meant he had at last achieved health, fame and fortune. The robot had warned him, of course. The thing was set to do one specialized job. Once he got what he wanted, it would work no more.
So Kelvin got the million dollars.
And he lived happily ever after. . . .
This is the middle of the story:
[. . . .]
The robot said, "You should not have begun the rapport until I instructed you. Now there will be danger. Wait." His eyes changed color. "Yes . . . there is . . . Tharn, yes. Beware of Tharn."
"I don't want any part of it," Kelvin said quickly. "Here, take this thing back."
"Then you will be unprotected against Tharn. Keep the device. It will, as I promised, ensure your health, fame and fortune, far more effectively than a—a horoscope."
This is the way the story starts:
Quarra Vee sat in the temporal warp with his android Tharn, and made sure everything was under control.
"How do I look?" he asked.
""You'll pass," Tharn said. "Nobody will be suspicious in the era you're going to. It didn't take long to synthesize the equipment."
"Not long. Clothes—they look enough like real wool and linen, I suppose. Wrist watch—that's odd, isn't it? Imagine people who need machinery to tell time!"
"Don't forget the spectacles," Tharn said.
Quarra Vee put them on. "Ugh. But I suppose—"
"It'll be safer. The optical properties in the lenses are a guard you may need against dangerous mental radiations. Don't take them off, or the robot may try some tricks."
"He'd better not," Quarra Vee said. "That so-and-so runaway robot! What's he up to, anyway, I wonder? He always was a malcontent, but at least he knew his place. I'm sorry I ever had him made. No telling what he'll do, loose in a semi-prehistoric world, if we don't catch him and bring him home."