How to choose the number of codewords in bag of words model
It is a trade-off between recall and precision. If you have only one codeword in your codebook then everything will map to it and you will have high recall. If you have 20,000 codewords in your codebook then you will have high precision and low recall. You can use a validation set to try different codebook sizes and see which one works best for your application. 

If you have a smaller size codebook I don't think that will help you get a sparse feature. It will just mean that more features map onto the same small set of codewords, increasing the recall.