The counties were originally called kōri and as such had ancient origins in Japan. Although the Nihonshoki claims they were established during the Taika reforms, kōri was originally written as 評. It was not until the Taihō Codex that kōri came into use in the form 郡, which denoted commanders (jùn) in China. Under the Taihō Code, the hierarchy of administrative units was: Province (国, kuni), county kōri, and below that the village (里 or 郷, sato).
Etymologically, the spoken words gun and kuni are related or were adopted into Japanese at different times from different language stages of Chinese - Middle Chinese and Old Chinese. Kōri (pronounced kopori in Old Japanese) is also a loanword, here from Baekje Korean.
Provinces and districts were increasingly irrelevant as administrative units from the Middle Ages onwards, but were used as the primary geographical division of the country - largely static and territorially compact, apart from individual adjustments - in contrast to the political feudal/prefecture division throughout until the late 19th/early 20th century, with the districts also being temporarily revived formally in 1878 as administrative units to subdivide the prefectures.