This question already has an answer here:
I read this scifi novel/short story around the 2000s in a public library, and I recall the following plot points which could be helpful in its identification:
The space traders (not sure if traders, but certainly a group of people with spacefaring abilities) somehow ended up on a medieval human planet, where the main civilization was a decaying empire much like the medieval Byzantines.
The decaying empire often fought skirmishes with the barbarians, who would handily defeat the heavily armed and armoured empire knights by drawing out their battle lines, retreating, and picking them off when they were tired and overextended.
The space traders managed to revitalise the empire by training a spearman army from the remaining peasants. This army then defeated the barbarians by holding the line against repeated assaults during a siege on the capital.
The story ends with the space traders being "exiled" to the medieval planet for violating a Prime Directive equivalent.
I believe this was a relatively old book when I had read it in the library around that time. The author was IIRC pretty famous in the science fiction genre as well, but I cannot remember any details on that. I'm rather certain about the plot points above, though.