All projects

Neuroscience & AI

Fly Brain: a Telegram bot on a real connectome

A bot that runs the full wiring diagram of a fruit fly brain: you tap a button, it simulates the whole brain, code decides what the fly did, and an LLM agent explains why.

Client
Own research project
Format
Practice case
Stack
Python, FastAPI, numba
  • 138,639neurons and 54.5M synapses: the whole brain, no simplified wiring
  • 0.3–2.5 sper whole-brain simulation, 10 runs of 1 s each
  • 86automated tests: kernel accuracy, validation, every scenario
Case cover

The problem

The FlyWire team has mapped the entire brain of a fruit fly: 138,639 neurons and 54.5 million synapses. My goal was to make this science understandable to someone with no biology background, and to make it obvious that the answers come from a brain model, not from a chatbot making things up.

The bot itself speaks Ukrainian, so the screenshots below are in Ukrainian.

The solution

A Telegram bot where this wiring diagram works as a model. The user taps a button, for example “swat the fly with a newspaper” or “give it sugar”, and the bot:

  1. runs a spiking simulation of the whole brain;
  2. shows what the fly did: escaped, started feeding or cleaned its antennae;
  3. sends a video with a schematic fly on the left and the signal running through its brain on the right;
  4. adds a neuron report with a brain map and explains why it happened.

Code decides the verdict, not the LLM. What the fly did is determined by three output neurons validated in the Shiu et al. paper (Nature, 2024). The LLM only explains the result and cannot invent neurons.

“Break the fly”. The same stimulus goes to a normal brain and a modified one. Switch off 2 neurons and the fly stops feeding. Switch off 4 and it fails to take off, so the newspaper lands on it. Cause and effect are visible.

Three modes:

  • “Guess what the fly does” runs with no LLM: you guess first, then the simulation runs and code checks your answer.
  • “Break the fly” shows two flies side by side, before and after.
  • “Experiments”: an LLM agent (OpenAI or Claude) with seven tools answers questions using simulation, path search in the synapse graph, comparison and images.

Speed. In the standard Brian2 simulator a whole-brain run takes minutes, which is too slow for a chat. I rewrote the model in numba with exact integration, and neurons that are guaranteed not to fire are not stepped at all. A test confirms the spikes match the full calculation exactly.

The result

Brain in the simulation 138,639 neurons, 54.5M synapses
Simulation 0.3–2.5 s (10 runs × 1 s of brain time)
Video rendering 3–4 s
Bot reply with video 5–17 s
Automated tests 86

The paper’s results are reproduced: sugar triggers the proboscis with a sigmoid dose-response, bitter suppresses feeding, and touching the antenna triggers antennal grooming.

Honest limitations

This is not an “uploaded mind” or a copy of a fly. It is a wiring diagram plus very simple neurons, with no neuromodulation, learning or body. The fly in the videos is a schematic drawing; the simulation decides what it does and when. The bot says so in every answer.

FlyWire data is licensed under CC BY-NC 4.0, so the project is non-commercial.

How the work went

How work on this project went, from understanding the problem to launch and improvements.

  1. 01

    Discovery

    Studied the Shiu et al. paper (Nature, 2024) and the FlyWire v783 data: which output neurons are validated for behaviour, and what the model can honestly measure and what it cannot (flight, freezing, smell).

  2. 02

    Solution design

    Split the system into two services: a Python simulator with an API and a TypeScript bot. Code decides the verdict from three validated neurons, the LLM only explains it, and the game runs with no LLM at all.

  3. 03

    Build

    Rewrote the neuron model in numba, built the "fly + brain" videos, path search in the synapse graph, an LLM agent with seven tools for OpenAI and Claude, and a button-only menu.

  4. 04

    Testing

    86 automated tests: spikes match the full calculation exactly, the paper's results are reproduced, and every game and "break the fly" scenario is checked across several random runs.

  5. 05

    Launch

    One-command deployment with Docker Compose; the FlyWire data downloads on first start. The bot replies with a video in 5–17 seconds.

  6. 06

    Optimisation

    Cut a whole-brain run from minutes to 0.3–2.5 s and video rendering to 3–4 s. Runaway activity is capped by an event budget, and for smell, where the model is unstable, the agent honestly uses path search instead of simulation.