Who should a contract be with?

This is one of a number of questions that I answered on a community for freelancers which is closing soon. I’m going to re-post my answers here, with any identifying details removed, in case they are useful to others.

My contracts are always with who is paying me. For example, if an agency asks me to do some work on one of their client’s websites, I contract with the agency and send my bill to them. What happens after that is not my concern, but I’d expect there to be a contract between the agency and client. The flow of money and work would therefore be:

Contract 1: Myself and agency. Agency pays me and the ownership of the work (or rights to use it) transfers to the agency.
Contract 2: Agency and client. Client pays agency and the ownership of the work (or rights to use it) transfers to the client.

So effectively the flow is Me -> Agency -> Client. I don’t have a contractual relationship with the client and if the agency wants to add a margin to my work, or give it to the client for free, that’s fine. If my invoice isn’t paid I pursue the agency, regardless of the reason (e.g. if they’re not paying me because the client hasn’t paid them, I’ll still go after the agency).