I have done both.
I don't have a formula.
I have started writing a poem, and translated that in to music.
I have improvised on an instrument, and then written lyrics that fit the music that emerged.
I have come up with melodies in my head and then played them on instruments.
I have come up with melodies by trying various shapes on the instrument.
I have come up with melodies purely theoretically, thinking of tones and harmonies and steps.
I have written music chronologically, and other times segments that are far apart in time have emerged after each other.
No matter where I start, or how I do it, however, I know what I am trying to describe. I know what the inspiration is. I often fail at doing it justice.