I am fairly certain you refer to a Dream Larva, from the Epic handbook.
Those are shy, cautious and solitary creatures that attempt to live hidden near small communities, in any dark inhabited spot, to feed on people's nightmares. Their mere presence in an area triggers sudden bad dreams that evolve into disturbed nightmares in whole populations; nightmares from which the larvae feeds.
It's actually a bunch of amalgamated larvae with a psionic connection, who acquired a great amount of intelligence due to the dreams and so many thoughts they consume.
They are an excellent plot device, but be cautious with using their combat stats for most parties. The challenge rating 31 tells a lot about it, but some of his abilities are ridiculous. The mere sight of it, for instance, makes the person have an instant daydream nightmare so horrifying that his mind is distorted and shattered, leading to a swift sudden death. (Save Will 43) (43 bro!)
They will usually prevent conflict, as they are best fed during peace and not combat, but they are merciless and sadistic at some points.
As a DM, your best shot is to have a worm that does not intend to enter conflict, using of his vast abilities (Say, constant telepathy 1Km radius) and dreamscape to plant fears and false convictions to drive pursuers away from him. Especially if the player levels are low, try to make it so that the party's objective is fulfilled by having the worm go away.
You can get more information at the Epic Handbook. I'm at work break right now so i cannot consult it to give you further details.
You are most likely thinking of the Marut, a powerful construct/outsider (depends on the edition) that hunts those who cheat death, and has been in every edition since AD&D.
What they are like for each edition:
1e AD&D - In 1e, They are the most powerful variety of inevitables, and although only a little information is given, it is mentioned how 'severity' plays a large role in their decision to pursue a target (sacrificing a city for immortality will likely draw their attention, while casting Ressurection once will not). They have a minimum of 15 HD, and advance up to 45. Although created with a divine mandate, these Maruts are constructs.
2e - 2e Maruts are very different from their original representation; they are celestial emmisaries who serve no particular purpouse, although their powerful single-target damage leads them to be sent to hunt individuals. They don't even have their signiature lightning and thunder fists in this edition! They do still have 15 hit dice, though.
3.5e - 3.5 Maruts are carbon copies of their 1e counterparts.
4e - Maruts are more similar here to their version from 2e, semi-celestial creatures that may be on any of a variety of tasks, but in the end serve the order of the universe as a whole.
5e - 5e statblocks are less readily available (it is the current edition), but this version seems more similar to the 2nd and 4th edition versions than the 1st and 3rd, being an enforcer of contracts rather than of death.
Pathfinder - Pathfinder seems to take off of the 3.5 interpretation, which isn't surprising. This holds true for Pathfinder 2e as well.
Best Answer
Thanks to the other answers, I figured out the creature. It was an actaeon, a defender of woodland creatures from Mystara campaign setting. I misremembered its breath weapon: instead of acid, its breath weapon polymorphs foes into forest creatures.