To avoid bad experiences, limit the amount of personal information you share. Don't give out details like your home address or financial situation. Also, don't be too quick to believe everything the prison pen pal says. They might be trying to create a false image. And it's a good idea to have an exit strategy. If things start to go wrong, be prepared to end the pen pal relationship without hesitation. For instance, if they start asking for inappropriate things repeatedly, it's time to cut ties.
One way is to go through a reputable pen pal program. These programs usually screen the inmates to some extent. Another important thing is to keep your communication on a positive but superficial level in the beginning. Don't get too emotionally invested too soon. As the relationship progresses, you can gradually deepen the connection if you feel comfortable. But always be cautious and aware that you are dealing with someone who is in prison for a reason, and that reason might be something very serious.