First thing to remember: the human brain is consuming a lot of energy when it's forced to solve a problem. Even though you're sitting in a chair motionless, what's going on in your head isn't trivial.
Your boss may or may not be sympathetic, but it's a good idea to gather up the research needed to support this assertion. The point being is that you're sprinting, and after making the 100 meter dash or whatever, then you're trying to run the marathon, having used up all your 'consumables'. What you need to do at the point where you're losing interest is get up, take a walk, take a nap, do something else - get away from the problem, and let your brain purge the waste products and build up fresh nutrients.
This article Does Thinking Really Hard Burn More Calories? says it ain't true. This article Evaluation of a “mental effort” hypothesis for correlations between cortical metabolism and intelligence suggests it is, however it is in a scientific journal and costs $35. Details of this are still uncertain and controversial.
I've been coding since the mid 1970s, and I've had this problem when I started and I have it now. My strategies for dealing with are, first, stop when one is dis-engaged, and second, shift to something physical (like walking around or making coffee or whatever). Since employers don't like the appearance of slacking off, I work independently as a contractor, and bill a lot of dollars per hour, but relatively few hours. If the people you're working for allow for this, you should be OK.