well, pretty much depends on the special AI, the age you're in, the religious situation, the techs, the neighbours, the land, your civics ...
usually the easiest way to end a war when you've been the one who was attacked is either to
a) sue for peace, although many of the AI leaders tend to ask for a laughable price - for example tokugawa, even when he's techwise backwards and only has a slightly higher power rating (means: war with you would lead to nothing for him, only incredible amounts of lost troops and losing the techrace etc.) he still asks you to give him your best commerce city which has walls, castle, LBs for peace. still, there are several situation in which the leaders will refuse to talk for quite a long time, and therefor making fast peace this way impossible. note that sometimes AIs will ask for a laughable price for peace, but will give in another trade anyway - for example, you could buy yourself peace if you give the AI currency or another high-valuable-tech if your plan is to take him out anyway as soon as you've moved your troops to his/her border and especially as the enemies troops aren't on your capitals doorstep anymore
b) bribe neighbours into attacking the AI that attacked you. in most cases this will lead to a quick peacetreaty. maybe you've experienced this yourself: you're backstabbing a AI that's in war with another neighbour - the AI tends to make peace pretty quick even with it's worst enemy just to take care of the backstabber, means: you. still, be aware that bribed AIs may be not as good prepared, leading to a rather quick peace between the two nations - and then the backstabbed leader may come back to smack you.
c) just do enough damage to the attacker. sounds trivial, and you might think "oh, thank you mr. brilliant, that's something i'd have never thought of..." but quite some players tend to sit behind their city walls and let the enemy attack them. well, actually this makes quite some sense if you've citygarrison2 Longbows in a city on a hill, but note that you'll give up the chance for a) collateral damage b) flanking c) more experience (attacking gives more XP than defending) leading to more GG points especially as you fight on your land d) favorable odds, since cityraider promotions are completely useless if you attack THEM instead let them attack you in your city. defending mounted units dont receive defensive bonuses, suiciding them into the enemy just for flanking purposes might buy yourself time and / or might hurt the attackers significant.
also note: the AI wont take peace as long as it has the "feeling" (means: the numbers) it might take that city it's going for. the sooner you bring a horrible loss to the attacking SoD, the less time is needed for peace and, most important, the less time there is for the AI to build and send reinforcements and therefor "improving" their numbers regarding the "lust for war".
overall, the most important thing with "AIs hard to deal with" is to always be prepared... and don't rely on defense too much. rather use the citydefense to guard the offense troops you use to attack the enemys SoD, means: attack from the save city position and use the city defense as a "super stack defender".
another trick, if you're expecting the enemys attack on a specific city, not only amass defenders/offensive units there but also build a little stack to attack a city a little offside, maybe use your naval units for this - get like ~10 units into your boats, as as the enemy attacks, quickmove to a coastal city, take it instant and sue for peace, maybe even gift back the city instant - as you didn't declare, a rather save way to improve diplomatics and get a very quick peace. of course this only works in certain situations, but it'll get you at least some safety especially vs. backstabbers or AIs being bribed.
but, after all, it pretty much depends on the game situation, the leaders and everything.