In verse 4 of Chapter 4 Paul is talking about what a man does to earn a wage. Those aren't works of the law. Those are physical things people do to earn a wage. Then in verse 5 he again uses that same analogy of things people do. Yes he talks about "works of the law" in other passages but in Chapter 4 he is not. If there was something we could do to attain righteousness the righteousness would be due for work done.
Paul told the Ephesians "not by works, so that no one can boast". Do you really think that men would only boast about following the law or do you think they would also boast about all the good deeds they did. Didn't Jesus say he would turn away all those who bragged they did all those things saying "I never knew you"? They weren't boasting about following any law.
Paul introduces Abraham into the discussion to illustrate the point he’s been arguing. And what is his point? Back up and try to follow him. His point is that righteousness is accounted on the basis of faith and not works of law.
We’ve already seen that this “law” includes more than the Law of Moses. We saw that when he proved that Gentiles, who weren’t under the Law of Moses, are guilty nevertheless.
Surely you don’t think Abraham was guiltless because he wasn’t under the Law of Moses...Do you?? Abraham was guilty. If he was to be acquitted, it would have to be on basis other than his works, because his works could only condemn him. But as the Scripture says, he was justified by his faith, not works. So it’s of grace, not debt.
No, Paul doesn’t change gears for a couple of verses and start talking about earning a living. He’s made his point about what works of law can’t do (they can’t justify us because we’re guilty and therefore unjustifiable) so he presses on with his explanation of the only way a sinner can be justified.
Perhaps your misunderstanding arises because you begin in chapter 4 instead of chapter 1. Back up and follow Paul’s train of thought, and I think you’ll be able to understand his “works”.