"Kubla Khan" is a strange and mysterious poem. In a note published with the poem, Coleridge explains that just before he wrote the poem, he had been in ill health for some time and had been prescribed an "anodyne" that made him very drowsy. He had been reading Purchas's Pilgrimage, and as he drifted off, this is the last sentence he read: "Here the Khan Kubla commanded a palace to be built, and a stately garden thereunto. And thus ten miles of fertile ground were inclosed with a wall.'' He states that he slept and dreamed about three hours
during which time he has the most vivid confidence, that he could not have composed less than from two to three hundred lines; if that indeed can be called composition in which all the images rose up before him as things, with a parallel production of the correspondent expressions, without any sensation or consciousness of effort. On awakening he appeared to himself to have a distinct recollection of the whole, and taking his pen, ink, and paper, instantly and eagerly wrote down the lines that are here preserved.
In other words, he wrote the poem while "asleep" or under the influence of whatever the medicine he had taken was. At that moment he had a visitor, and when he returned to the poem, he could not remember anymore of his dream-vision. He wanted to finish it, but he was unable to.