Helicobacter pylori causes clinical disease primarily in those individuals infected with a strain that carries the cytotoxin associated gene pathogenicity island (cagPAI). The cagPAI encodes a type IV secretion system (T4SS) that injects the CagA oncoprotein into epithelial cells and is required for induction of the pro-inflammatory cytokine, interleukin-8 (IL-8). CagY is an essential component of the H. pylori T4SS that has an unusual sequence structure, in which an extraordinary number of direct DNA repeats is predicted to cause rearrangements that invariably yield in-frame insertions or deletions. Here we demonstrate in murine and non-human primate models that immune-driven host selection of rearrangements in CagY is sufficient to cause gain or loss of function in the H. pylori T4SS. We propose that CagY functions as a sort of molecular switch or perhaps a rheostat that alters the function of the T4SS and "tunes" the host inflammatory response so as to maximize persistent infection.