Doctors use the results of physical examinations and X-rays to make this decision. A loose piece of bone or severe malalignment in the knee (as seen on X-rays) point to the need for surgery.
Usually after the kneecap (patella) is dislocated the first time, treatment with a physical therapist is recommended. The therapist will prescribe strengthening exercises and help you retrain your movements to keep the patella in place. If you still have pain, swelling, difficulty climbing stairs or doing sports, surgery may be required.
Ask your doctor if a more conservative approach such as physical therapy could help you.