http://www.prima.com.pe/0/modulos/JER/J ... =0&JER=144
Here is a link to the rules. This is for AFP Prima, but the afp rules are government mandated so they are virtually the same for each company. My strongest suggestion, is find someone you trust who will be in Peru and give them limited power of attorney to manage this. The AFP's HATE to let the money go so they reject applications all the time for minor things, such as a missing coma in a translation or a missing middle initial and things like that. If you do it from abroad, you will have to fix and mail the documents back each time and you will have to get them legalized at your nearest embassy. If you have a local delegate, they can do the translations and legalizations locally and save lots of time and money.
Some of the tricks, the different AFP's read the rules a little differently from time to time, but generally they do not allow Social Security to count, they require 36 months VOLUNTARY retirement contributions.
Another trick, and this is just ignorance in the system they built, many Western retirement accounts are second party accounts, or trusts. The retirement account is really a trust with you as the beneficiary. That is not a problem for demonstrating your 36 months of contributions abroad, but the AFP's will only transfer the money to an account with your name on it. That leads to a problem because if you transfer it directly to your retirement account, depending on where in the world you are, you do not have to pay taxes. However, if you transfer it to a personal account, you might have a tax obligation.
It has been 3 years since I last did this, so some of the rules may have changed. I no longer do it as I am no longer in peru and apart from offering some advice from time to time on this board, I no longer take calls or emails for the service.
Jamie Tunstall is a good personal financial advisor in Peru servicing the expat community, if you have a fair amount, it may be worth contacting him.
It requires a lot of work, mostly resubmitting forms, that when I did it, I would not do it for less than $2k US. It can be done on your own and it really is not that hard, it is just detail oriented and tedious.
You can not do it from within the country yourself generally because you can not do it until you turn in your carnet, and get certification that you have left the country from your employer and had not obligations to SUNAT