AbstractImported agricultural pests can cause substantial damage to agriculture, food security, and ecosystems. In the United States, the Agricultural Quarantine Inspection Monitoring (AQIM) program conducts random sampling to estimate the probabilities that cargo and passengers arriving at ports of entry carry pests. Assessing these risks accurately is critical to enable effective policies and operational procedures. This study introduces a pathway‐level analysis with an objective function aligned with AQIM's goal, offering a new perspective compared to the current container‐by‐container approach, which relies on heuristics to set inspection rates. We formulate an optimization model that minimizes the mean squared error of the probability estimates that AQIM obtains. The central decision‐making tradeoff that the model explores is whether it is preferable to sample more arriving containers (and fewer boxes per container) or more boxes per container (and fewer containers), given limited resources. We first derive an analytical solution for the optimal sampling strategy by leveraging several approximations. Then, we apply our model to a numerical case study of maritime cargo sampling at the Port of Long Beach. Across a wide range of parameter settings, the optimal strategy samples more containers (but fewer boxes per container) than the current AQIM protocol. The difference between the two strategies and the accuracy improvement with the optimal approach are larger if the pest statuses of boxes in the same container are more strongly correlated. We recommend that AQIM record box‐level (beyond only container‐level) inspection data, which could be used to estimate this correlation and other model parameters.