Navy Secretary W. Graham Claytor broke ranks yesterday with much of the uniformed military by recommending an overhaul of its zealously guarded retirement program.

Claytor, in testimony before president Carter's Commission on Military Compensation, also said the medical plan covering service people "is kind of a disaster bureaucratically, if not otherwise" and should be overhauled as well.

Instead of sticking with the present military retirement program that enables a service person to retire at half pay after 20 years of active duty, Claytor told the commission, he favors a more "cost effective" flexible system.

Claytor's testimony was the strongest recommendation for change yet made to the commission by a Pentagon executive, putting the Navy secretary at odds on the issue with some members of the Joint Chiefs of Staff and distancing him from Army Secretary Clifford L. Alexander Jr.

Carter has said the commission's report, due March 15, will influence his legislative recommendations to Congress on pensions and other military benefits. Congress is not expected to make any decisions on those politically touchy issues until 1979 or 1980.

The basic position of the joint chiefs has been that the 20-year retirement option long has been a prime attraction for joining the service and making it a career. Tinkering with that benefit, uniformed leaders have warned, would alarm career military people, pushing them toward unionization, and would make recruiting soldiers, sailors, airmen and Marines more difficult.

Gen. George S. Brown, chairman of the joint chiefs, moderated that viewpoint somewhat in his appearance before the commission yesterday, declaring that "I think we can move our current retirement policy to one based on longer service" than 20 years.

Brown did not specify how much longer he had in mind. He urged the commission to keep the present retirement system in force for "individuals in uniform today," applying any changes to those that follow them into the service.

Similarly, Adm. James L. Holloway III, chief of naval operations, conceded to the commission yesterday that "strong external pressures" are building up against paying service people retirement after 20 years' service, but urged that any changes be "evolutionary" rather than sudden. He recommended that the 20-year retirement option be kept open under any new plan for sailors who had lengthy sea duty.

Army, Air Force and Marine Corps leaders in earlier testimony to the commission stood firm against changing the retirement system - a view Army Secretary Alexander hewed to yesterday.

"I do not happen to be of that school" which believes change is needed, Alexander said, adding that the cost of military retirement does not impel change.

Differing with Alexander, Navy Secretary Claytor said "the enormous future anticipated costs" of military retirement - $37 billion by the year 2000 - are indeed "a driving force" for change. The present system allowing retirement after 20 years "is certainly popular" but doesn't have to be sacrosanct, Claytor said.

"A significant number" of service people would opt for a more flexible retirement system. Claytor predicted, which would allow them to retire after 10 rather than 20 years of active duty without losing all their benefits.

"The present automatic right to retire voluntarily at 20 years could be extended to 30 years for both officers and enlisted personnel," providing that service people who held arduous jobs - such as long tours of sea duty - could earn points for retirement at 20 years.