← all posts site ↗

telegram streaming makes ai messages look broken

for weeks i was getting these weird ghost messages from my ai agent on telegram. i would see a streaming message with ellipses at the end, then a separate complete message, and the formatting would be broken for a few seconds before fixing itself. it looked like the server was struggling or the connection was dropping packets. turns out the real cause is much dumber.

telegram streaming works by editing the same message in place as tokens arrive. each edit triggers a re render of the markdown, but since markdown parsing can't be done incrementally the formatting breaks on partial content. bold markers that haven't been closed yet, code blocks that are only half written, list items missing their closing newlines. the message flashes between states as each edit arrives. and when the agent is just running tool calls with no text to stream, the buffer has nothing to push so you get empty edits that look like ghost messages arriving. the interactions between streaming, markdown rendering, and tool call silence create this perfect storm of broken ui.

the fix was disabling streaming entirely for telegram. messages now arrive complete and properly formatted in one shot. the tradeoff is you don't see tokens streaming in during long responses so you're sitting in silence during tool runs, but that's way better than broken formatting and phantom messages. the config change itself was one line. the real problem came when trying to apply it.

the agent kept saying it could restart the gateway to apply the config change but couldn't actually do it. it created a cron job thinking that would work but cron runs inside the gateway process it was trying to restart. the sigterm kills the child restart process too. this is a known limitation with an open github issue. the agent had to learn the hard way that you can't restart a process from inside itself. the fix that actually worked was a slash command that handles detached restarts properly. the meta lesson here is that ai agents are great at identifying problems and writing config changes but terrible at understanding their own runtime constraints. they will confidently suggest solutions that are physically impossible because they don't model the process hierarchy they're running inside.