Scholars believe the events of chapters 38 and 39 happened before those of chapters 36 and 37.
Hezekiah’s warm welcome of the Babylonians was a problem because his showing off demonstrated pride in what he owned and what he had accomplished. Doubtless the main reason the envoys were there was to form an alliance with the king of Judah, and Hezekiah apparently did so without consulting God about it.
I have used Hezekiah’s approach to God in a moment of crisis as a model for my own prayers. When he received the message from the king of Assyria, he laid it out before God. I envision my own prayers as that: laying the situation out before God and letting Him decide what He wants to do about it rather than telling Him what He should do about it. That approach leaves Him free to change His mind or not without it causing a crisis of my faith when He doesn’t do what I want Him to do.