In this paper, the context quantization for I-ary sources based on the affinity propagation algorithm is presented. In purpose of finding the optimal number of classes, the increment of the adaptive code length is suggested to be the similarity measure between two conditional probability distributions, by which the similarity matrix is constructed as the input of the affinity propagation algorithm. After the given number of iterations, the optimal quantizer with the optimal number of classes is achieved and the adaptive code length is minimized at the same time. The simulations indicate that the proposed algorithm produces results that are better than the results obtained by the minimum conditional entropy context quantization implemented by K-means with lower computational complexity.