BackgroundHigh-precision behavior tracking and closed-loop intervention are essential for studying the neural basis of cognition and behavior. Existing commercial systems are costly and lack flexibility for customization, while current open-source tools often have limitations regarding real-time functionality, device synchronization, and ease of use.New methodWe developed RpiBeh, an open-source, cost-effective, and versatile software tailored for rodent neuroethological research. The software features an intuitive user interface and offers extensive customization options. RpiBeh leverages a Raspberry Pi and camera for video streaming, enabling behavior-driven closed-loop control. Additionally, it provides frame-by-frame video timestamp output to achieve precise synchronization with external devices. For real-time tracking and locomotion analysis, RpiBeh utilizes several novel algorithms and integrated newly developed deep-learning method. Specifically, we introduced two algorithms: a Background Subtraction Method (BSM) for real-time position tracking and a Frame Difference (FD) algorithm for freezing behavior detection.ResultsRpiBeh was validated in single animal real-time tracking and locomotion pattern detection, and proved to be flexible and effective for configurating behavior-triggered closed-loop reinforcement experiments including passive place avoidance task and social fear conditioning tasks.Comparison with existing method(s)RpiBeh achieved the same level of tracking and locomotion pattern detection performance by using ANY-maze and DeepLabCut, with advantage in customization and expandability.ConclusionsRpiBeh offers an efficient, affordable, and open-source solution for video tracking and behavioral-driven closed-loop experiments.