The detection of agricultural pests is a crucial part of smart agriculture. In the realm of small agricultural pest detection within complex environments, this paper presents our novel, tailored pest detection approach. Our methodology is rooted in an optimized adaptation of the YOLOv7 model. The enhanced model we propose guarantees real-time detection capabilities, while simultaneously delivering improved accuracy. The key feature augmentations we incorporate are as follows: We developed a precisely engineered detection layer to tackle the complexities posed by small target detection. Optimization of preliminary box generation was realized through the deployment of a clustering algorithm. Additionally, we improved the non-maximum suppression approach. To manage extraneous information in the convoluted agricultural environment, we fused the ACmix attention mechanism module within the model to concentrate on agricultural pests. Our practical results substantiate the efficacy of our enhanced model, named YOLOv7-AgirPest, which achieves an astounding detection speed of 60.17 FPS in identifying agricultural pests. Additionally, we achieved significant progress with an mAP value reaching 67.23%, marking a 6.1% relative enhancement over the baseline YOLOv7 model. In summary, our refined model not only fulfills real-time detection constraints but also delivers superior agricultural pest detection in complex field conditions.