Deus ex machina is Latin for "God from the machine" and is a literary plot device in which, to resolve a seemingly impossible situation, something seemingly impossible suddenly occurs. The phrase originated in Greek theatre, where actors or statues would be hoisted into the air by machines and enter the stage from the left or right as Gods or deities to resolve a conflict or bring about a happy ending in a play.

While it is rare today to see it used as plainly as gods swinging in from stage left, there are several more modern examples of such a plot device being used. A famous example is that of Harry Potter and the Chamber of Secrets, where a phoenix flies into the chamber, dropping the sorting hat, inside which Harry finds the sword of Gryffindor, which he uses to kill the basilisk. It is later explained that it came due to Harry's loyalty, though this is still widely regarded to be a Deus ex machina.

The reason authors reach for a Deus ex machina is simple: they want the plot to be interesting. Raise your hand if you've ever done this: Your character gets lost in the woods, you read that, and you think - nah, that's not bad enough - so then you make it late fall, or even winter, and they're underdressed. Also, there are howls from wolves nearby, and they just tripped and broke their ankle and can't walk. This makes for interesting writing; you've put a character into a nearly impossible situation. The only problem is, even you, the author, don't know how to get them back out. So instead of rewriting the whole scene, you have a pair of campers hear your lost character crying and take them back to camp safe and sound.

So why do we want to avoid using such solutions in our own novels? The main reason is that if used poorly, it doesn't feel earned. Picture this: your protagonist gets backed into the end of an alley, and there is no escape as the antagonist slowly draws on them, knife in hand. Suddenly, a light explodes overhead, sending shards of glass into the antagonist’s eyes, giving her just enough time to dash past him. Do this, and you can be sure your reader will put your book down (and quite possibly leave an unflattering review). Your reader wants to see your protagonist make a daring escape; they want them to have agency.

How could this be fixed? Well, let's start with our first example, one of the easiest ways to resolve a Deus ex machina is to foreshadow the arrival. In the Harry Potter example, explaining the fierce loyalty of the Phoenix, having Harry meaningfully interact with it (beyond seeing it within Dumbledore's office), such as by saving it, could have made its appearance more sensible. The withdrawal of the sword as well is not well set up prior to this scene, making it feel like something shoehorned in to fix the scene. Casually mentioning the artifact, perhaps saying it was missing and alluding to the properties of the hat, may have worked here.

Going back to our protagonist, imagine if earlier in the book you had made reference to them having taken years of karate lessons. This doesn't have to be a fleshed-out back story; even a casual mention of a photo on the wall and a two-sentence explanation would have worked:

"Wait, you took karate lessons?" Sussie asked, looking at a framed photo on her wall
"Oh, that? Yeah, my parents made me go. Told me it would build 'discipline'," Abby motioned air quotes with her fingers. "Anyway, I made it a black belt before finally getting out last year."

These two simple lines are all we need to set up the scene, so when Abby handily disarms the attacker and makes a break for it, not only will this keep your reader glued to the page, it will feel earned. Just don't forget to remove the photo scene if later you change your mind about the alley, otherwise you will have an unresolved Chekhov's Gun.

There are rare cases where a Deus ex machina works, such as in a comedic work or a parody piece where you are poking fun at the idea. For example, you are writing a skit mocking the three little pigs where the wolf wins, and perhaps when he makes it to the third house, you might have:

"Oh no," said the wolf, "This house is much too strong for me to blow down, if only I had some kind of tool to get in," his tone sarcastic.

The three pigs looked at each other, suspicion growing on their face, "Why did he say it like that?"

Meanwhile, outside, a passing work truck drove by, hitting a well-placed speed bump and knocking loose a sledgehammer, which fell neatly at the wolf's feet.

"Right on schedule," he said, rubbing his hands together.

The primary difference here is that your readers are supposed to groan; that's literally the whole point of the scene. Other famous examples come from cartoons such as Bugs Bunny, where Wile E. Coyote can never seem to catch the road runner through the equipment (or even the environment) working against him in completely baffling ways.

In the end, the advice is always the same: be intentional with your plot devices. If you write your character into a corner, go back and give them a way back out. If your camper is going to get lost in the woods, give them a back story as a scout to give them a plausible way forward, and a reason for having a survival pack on them (and perhaps leave the convenient campers out entirely). The best solutions to a hard situation in your plot are the ones that feel earned, even if they are surprising.

If you want to be sure you are avoiding this literary trap, read your book back through this simple question, "Did this solution feel earned?", even if they don't say so explicitly, this is what your reader will ask themselves. Ask your beta readers this same question; being so close to your novel, it is very easy to forget you didn't add that backstory about the photo, for example.

The best twists are the ones that feel inevitable in hindsight. Plant the solution before you need it, and not only will your reader accept it, but they'll feel like they should have seen it coming. And remember the difference between a dreamer and an author is the tap of the keys, or the stroke of the pen.